我们目前正在使用org.springframework.scheduling.quartz.SchedulerFactoryBean和org.springframework.scheduling.quartz.CronTriggerBean来实现特定cronExpressions的作业调度,我们现在需要触发某个动作的后台进程,即类似于JMS。
我正在寻找使用quartz调度程序的选项,我可以将任务添加为SchedulerFactoryBean中的作业(不指定cronexpression)并从Java类中调用它吗?探索此选项需要帮助。提前致谢
谢谢回复。我确实试过这个选项 这是spring xml配置
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="TEST_SCHEDULER" />
</list>
</property>
<property name="autoStartup"><value>true</value></property>
</bean>
<bean id="TEST_SCHEDULER" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="IN_TEST_SCHEDULER" />
<property name="cronExpression"><value>"00 00 00 ? * MON-FRI"</value></property>
</bean>
<bean name="IN_TEST_SCHEDULER" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.TestScheduler" />
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="20" />
</map>
</property>
</bean>
从java类调用这些语句
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
Scheduler scheduler = schedulerFactory.getScheduler(); //scheduler is null here ..
scheduler.triggerJob(jobName, jobGroup);
调度程序为null因此无法触发作业。
将org.springframework.scheduling.quartz.SchedulerFactoryBean
豆注入你的班级。在此bean上调用getScheduler()
方法以检索调度程序实例。使用调度程序为您所需的作业添加其他触发器(简单,cron等)。
在探索了几个网站后发布了解决方案,以防万一像我这样的人在将来寻找它:)
WebApplicationContext springCtx = WebApplicationContextUtils
.getWebApplicationContext(getServletContext());
scheduler = (Scheduler) springCtx.getBean("scheduler");
scheduler.triggerJob("IN_TEST_SCHEDULER", "DEFAULT");
这会触发所需的工作