到底是什么在春季运行 quarz cron 作业?
好人。我正在尝试在一个由maven管理的非常简单的项目中使用quartz和spring。因此,在mycron作业类所在的模块中,我在其中包含了一个java主类,只是为了查看作业输出一些文本和新日期。 这是我的 spring 配置:
<!--Scheduling-->
<!--Job-->
<bean id="projUpdater" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.myproject.utilscheduling.quartz.ProjUpdaterCronImpl" />
</bean>
<!---End of Jobs-->
<!--Triggers-->
<bean id="regularUpdateTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="projUpdater"/>
<property name="cronExpression" value="30 1 * * * ?"/>
</bean>
<!--End ofTriggers-->
<!--Scheduler Factory-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="regularUpdateTrigger"/>
</list>
</property>
</bean>
<!--End of Scheduler Factory-->
<!--End of Scheduling-->
这是作业类,
public class ProjUpdaterCronImpl extends QuartzJobBean {
public ProjUpdaterCronImpl() {
}
protected void executeInternal (JobExecutionContext ctx) throws JobExecutionException {
System.out.println("[JOB] " + new Date() + "hello");
}
}
这是主类,
public class NewMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("starting job");
}
}
所以据我了解,该作业将在 100 秒后启动并发布在控制台上。我错了。我遇到了几个或我解决的错误,这样我就可以安全地假设 spring 配置文件中没有错误,因为构建和运行时没有错误。那么我做错了什么或者我忘记做什么?
第二个问题是,因为我强迫自己采用测试驱动的方式,所以我将如何测试 cron 作业类? 感谢您的阅读
good people. i'm trying to use quartz with spring in a very simple project managed by maven.So in the module in which the mycron job class is i included a java main class to it just to see the job output some text and new date.
here is my spring config:
<!--Scheduling-->
<!--Job-->
<bean id="projUpdater" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.myproject.utilscheduling.quartz.ProjUpdaterCronImpl" />
</bean>
<!---End of Jobs-->
<!--Triggers-->
<bean id="regularUpdateTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="projUpdater"/>
<property name="cronExpression" value="30 1 * * * ?"/>
</bean>
<!--End ofTriggers-->
<!--Scheduler Factory-->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="regularUpdateTrigger"/>
</list>
</property>
</bean>
<!--End of Scheduler Factory-->
<!--End of Scheduling-->
and here is the job class
public class ProjUpdaterCronImpl extends QuartzJobBean {
public ProjUpdaterCronImpl() {
}
protected void executeInternal (JobExecutionContext ctx) throws JobExecutionException {
System.out.println("[JOB] " + new Date() + "hello");
}
}
here is main class
public class NewMain {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("starting job");
}
}
so in my understanding the job will be started after 1mn30s and posted on the console.I'm wrong.i faced couple or errors that i solve so i can safely assume that there is no error in in spring configuration file since there is none when building and running.So what did i do wrong or what did i forget to do?
second concern since i'm forcing myself to go the test driven way how will i possibly test a the cron job class?
thanks for reading
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 cron 表达式
实际上将触发器配置为每小时在整点后 1 分 30 秒触发一次。
Zoidberg 建议使用
MethodInvokingJobDetailFactoryBean
允许您将 cron 作业编码为 POJO,使其易于测试。Your cron expression
actually configures the trigger to fire once every hour at 1 minute 30 seconds after the hour.
Zoidberg's suggestion to use
MethodInvokingJobDetailFactoryBean
allows you to code your cron job as a POJO, making it easy to test.为你的工作 bean 尝试一下这个。
其中您的 RegisteredObject 是在 Spring 配置中某处注册的 bean。方法是该对象中存在的方法。
Try this for your job bean.
Where your registeredObject is a bean registered in the spring config somewhere. And the method is a method that exists in that object.