Spring quartz定时任务无法触发

pushme_pli 2011-07-19 11:17:08
今天用Spring的定时任务做了一个例子,但是始终无法触发,请大家帮忙:
1. applicationContext配置:test_spring.xml
<!-- 普通的业务Bean -->
<bean name="testService" class="test.quartzTest"/>

<!-- 作业 -->
<bean id="jobDetail_test" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="testService"/>
<property name="targetMethod" value="SayHello"/>

</bean>

<!-- 触发器 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobDetail_test"/>
</property>
<!-- 10点开始没5秒运行一次 -->
<property name="cronExpression">
<value>0/5 * 10 * * ?</value>
</property>
</bean>

<!-- 计划 -->
<bean id = "testScheduler" lazy-init = "true" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
2. 业务类
package test;
public class quartzTest {
public void SayHello() {
// TODO Auto-generated method stub
System.exit(1);
try {
System.out.println("处理任务开始>........");
// 业务逻辑代码调用
System.out.println("时间[" + new java.util.Date().toLocaleString()
+ "]----->大家好啊!");
System.out.println("处理任务结束!");
} catch (Exception e) {
e.printStackTrace();
}
}

public void hello(){
System.out.println("test");
}
}
3.测试类
package test;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

public class test {


public static void main(String[] args) throws IOException {
ApplicationContext ac = new ClassPathXmlApplicationContext("test_spring.xml");
System.out.println("test");
SchedulerFactoryBean test = (SchedulerFactoryBean) ac.getBean("testScheduler");


}

}

以上就是所有的代码了,在eclipes中运行,一直未成功啊,请各位大大帮忙!!!
...全文
622 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
pushme_pli 2011-07-21
  • 打赏
  • 举报
回复
找到原因了,spring中default-lazy-init属性设为true了,我的错,结贴
pushme_pli 2011-07-19
  • 打赏
  • 举报
回复
bean testScheduler 的一个参数lazy-init 如果设置成false则 main跑完成,但是任务没被调起来
如果设置成true则报错java.lang.NoClassDefFoundError: org/quartz/ObjectAlreadyExistsException
各位大大可以试下
ps:表达是应该是对的
zn85600301 2011-07-19
  • 打赏
  • 举报
回复
是不是你的表达式有问题 改成 0 0/2 * * * ? 两分钟执行一次

然后设断点看看 话说你是用main方法跑着测?

pushme_pli 2011-07-19
  • 打赏
  • 举报
回复
我觉得可能是我理论上出了什么问题
pushme_pli 2011-07-19
  • 打赏
  • 举报
回复
断点不用了吧,就那么几行代码啊。
HeiBoyYang 2011-07-19
  • 打赏
  • 举报
回复
设断点 看看 这样你能找到哪出问题
pushme_pli 2011-07-19
  • 打赏
  • 举报
回复
没有人用过吗

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧