gpt4 book ai didi

java - Spring 中的 quartz 作业尚未启动

转载 作者:行者123 更新时间:2023-12-02 07:48:59 29 4
gpt4 key购买 nike

我正在使用 Spring 的 SchedulerFactoryBean 来动态安排作业。这是我的 Spring 配置:

@Configuration
public class SpringConfigBean {
@Bean
public DataSource dataSource() throws NamingException{
InitialContext ctx = new InitialContext();
DataSource source = (DataSource) ctx.lookup(dataSourceName);
return source;
}

@Bean
public SchedulerFactoryBean schedulerFactoryBean() throws NamingException {
SchedulerFactoryBean schedulerFactoryBean = null;
try {
schedulerFactoryBean = new SchedulerFactoryBean();
schedulerFactoryBean.setDataSource(dataSource());
schedulerFactoryBean.setQuartzProperties(com.orca.integration.plugins.vs.broadpeak.admin.utils.Configuration.getQuartzProperties());
schedulerFactoryBean.setAutoStartup(false);
} catch (SchedulingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return schedulerFactoryBean;
}

}

这是我的工作:

@Component
public class MyJob extends QuartzJobBean {

@Override
public void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("Start job...");
}
}

这是我动态安排任务的地方:

@Component
public class MyManager {
@Autowired
private SchedulerFactoryBean schedulerFactoryBean;

public void myMethod(String text, Calendar startDate) {
logger.debug("BroadpeakDistributionManager.startRecordingChannel(); Parameters: recording channel ID=" + recordingChannel.getExternalId());

// StartRecordJob.scheduleMe(text, startDate, "Start");

JobDetailBean jobDetailBean = new JobDetailBean();
jobDetailBean.setJobClass(MyJob.class);
jobDetailBean.setName("name");
jobDetailBean.afterPropertiesSet();

SimpleTriggerBean simpleTriggerBean = new SimpleTriggerBean();
simpleTriggerBean.setJobDetail(jobDetailBean);
simpleTriggerBean.setName("Name");
simpleTriggerBean.setStartTime(startDate.getTime());
try {
simpleTriggerBean.afterPropertiesSet();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
schedulerFactoryBean.setTriggers(new Trigger[] {simpleTriggerBean});
schedulerFactoryBean.start();
}
}

作业尚未开始。我做错了什么?

最佳答案

我没有任何使用 Spring 进行编程配置的经验。因此,我编写了这个 XML Spring 应用程序上下文配置,并将其与 Quartz 集成。

看一下:

<!-- You can inject other sping beans here -->
<bean id="myJob" class="your.package.here.MyJob "/>

<!-- Define your target method -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myJob" />
<property name="targetMethod" value="executeInternal" />
</bean>

<!-- trigger using unix expression -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail" />
<!-- run every morning at 6 AM -->
<property name="cronExpression" value="0 0 6 * * ?" />
</bean>

关于java - Spring 中的 quartz 作业尚未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11561939/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com