Spring使用@Component执行计划任务的问题

cdkx945 2014-02-09 11:42:32



如图所示,我使用了@Component标签来做定时任务,但是我发现,如果我在这个类中写了2个定时方法,第二个就不会被执行。我想知道为什么
ps:2个定时方法的时间不一样,除了时间不一样,其余的都一样不存在方法冲突什么的....请忽略方法执行的内容,权当是2个定时任务各打印1句话,但是同一个类的第二个方法没有被执行.......
但是如果如果新建一个类, 把第二个方法cp过去,就也能执行了,不论是同一时间还是不在同一时间



不好意思,就这么点分了....
...全文
857 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
绝对在乎妮 2014-02-19
  • 打赏
  • 举报
回复
引用 9 楼 cdkx945 的回复:
[quote=引用 7 楼 ljl434841 的回复:] [quote=引用 6 楼 cdkx945 的回复:] [quote=引用 2 楼 ljl434841 的回复:] 怎么可能~我刚刚弄完这个spring的定时器demo,完全可以的啊
我直接导入的org.springframework.scheduling.annotation.Scheduled 一会儿我试试你那个····[/quote] 这与你导入什么东西无关,只要你有类似于spring-context-3.2.0.M1.jar这样的包就行,当然,我这个是3.2,你的可能是其它版本也不一定。然后就打开定时任务的配置,大概如下:
<task:scheduler id="scheduler" pool-size="10" />  
    <task:executor id="executor" keep-alive="3600" pool-size="100-200" 
    queue-capacity="500" rejection-policy="CALLER_RUNS" /> 
    <task:annotation-driven executor="executor" scheduler="scheduler" />
然后,你就可以在你的类里用@Scheduled注解来做定时任务了[/quote] 我现在的情况属于偷了个懒 没有做任何配置,直接使用的@Scheduled[/quote]能跑任务就OK了
cdkx945 2014-02-18
  • 打赏
  • 举报
回复
引用 7 楼 ljl434841 的回复:
[quote=引用 6 楼 cdkx945 的回复:] [quote=引用 2 楼 ljl434841 的回复:] 怎么可能~我刚刚弄完这个spring的定时器demo,完全可以的啊
我直接导入的org.springframework.scheduling.annotation.Scheduled 一会儿我试试你那个····[/quote] 这与你导入什么东西无关,只要你有类似于spring-context-3.2.0.M1.jar这样的包就行,当然,我这个是3.2,你的可能是其它版本也不一定。然后就打开定时任务的配置,大概如下:
<task:scheduler id="scheduler" pool-size="10" />  
    <task:executor id="executor" keep-alive="3600" pool-size="100-200" 
    queue-capacity="500" rejection-policy="CALLER_RUNS" /> 
    <task:annotation-driven executor="executor" scheduler="scheduler" />
然后,你就可以在你的类里用@Scheduled注解来做定时任务了[/quote] 我现在的情况属于偷了个懒 没有做任何配置,直接使用的@Scheduled
sunaer 2014-02-15
  • 打赏
  • 举报
回复
@Configuration @EnableScheduling public class Tasker { }
绝对在乎妮 2014-02-14
  • 打赏
  • 举报
回复
引用 6 楼 cdkx945 的回复:
[quote=引用 2 楼 ljl434841 的回复:] 怎么可能~我刚刚弄完这个spring的定时器demo,完全可以的啊
我直接导入的org.springframework.scheduling.annotation.Scheduled 一会儿我试试你那个····[/quote] 这与你导入什么东西无关,只要你有类似于spring-context-3.2.0.M1.jar这样的包就行,当然,我这个是3.2,你的可能是其它版本也不一定。然后就打开定时任务的配置,大概如下:
<task:scheduler id="scheduler" pool-size="10" />  
    <task:executor id="executor" keep-alive="3600" pool-size="100-200" 
    queue-capacity="500" rejection-policy="CALLER_RUNS" /> 
    <task:annotation-driven executor="executor" scheduler="scheduler" />
然后,你就可以在你的类里用@Scheduled注解来做定时任务了
cdkx945 2014-02-13
  • 打赏
  • 举报
回复
引用 2 楼 ljl434841 的回复:
怎么可能~我刚刚弄完这个spring的定时器demo,完全可以的啊
我直接导入的org.springframework.scheduling.annotation.Scheduled 一会儿我试试你那个····
cdkx945 2014-02-13
  • 打赏
  • 举报
回复
引用 2 楼 ljl434841 的回复:
怎么可能~我刚刚弄完这个spring的定时器demo,完全可以的啊
我现在写的木有用配置文件.....也或者配置了我没有找到吧!正在搜索中....一会儿我试试你这个···我刚接手的项目~~~
绝对在乎妮 2014-02-13
  • 打赏
  • 举报
回复
绝对在乎妮 2014-02-13
  • 打赏
  • 举报
回复
代码:
@Component
public class Job {
	
	
	@Scheduled(cron="0 0/1 * * * ?")//一分钟更新一次
	public void test0(){
		System.out.println("test0->"+new Date());
	}
	
	@Scheduled(cron="0/30 * * * * ?")//30秒更新一次
	public void test1(){
		System.out.println("test1->"+new Date());
	}
	
	@Scheduled(fixedDelay=30000)
	public void test2(){
		System.out.println("test2->doing fixedDelay->"+new Date());
	}
	
	@Scheduled(fixedRate=5000)
	public void test3(){
		System.out.println("test3->doing fixedRate->"+new Date());
	}
	
	@Scheduled(cron="0/5 * * * * ?", initialDelay=1000)
	public void test4(){
		System.out.println("test4->doing initialDelay->"+new Date());
	}

}
spring-*.xml

<task:scheduler id="scheduler" pool-size="10" />  
    <task:executor id="executor" keep-alive="3600" pool-size="100-200" 
    queue-capacity="500" rejection-policy="CALLER_RUNS" /> 
    <task:annotation-driven executor="executor" scheduler="scheduler" />
绝对在乎妮 2014-02-13
  • 打赏
  • 举报
回复
怎么可能~我刚刚弄完这个spring的定时器demo,完全可以的啊
cdkx945 2014-02-13
  • 打赏
  • 举报
回复
看来CSDN已经不行了!发个帖几天都没人回.......连灌水的都没了.....

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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