记一次Spring定时任务无法生效问题

因为业务需要,创建了两个定时任务,发布到线上,发现第一日凌晨执行后,便再也不执行,用到的Spring框架式Spring3.2,比较老了,因为线上环境问题(resin服务器3.1),不方便升级。
刚开始是使用的xml来配置定时任务,也就是没有用**@Scheduled(cron=“0 0 12 * * ?”)**注解,换成注解后还是不行。
代码如下:

public class TaskJob {
	@Autowired
	xxxDao dao;
	@Autowired
	xxxOrm orm;
	@Autowired
	CacheClient cacheClient;
	/**
	 * 重置每天奖品的派发数
	 */
	@Scheduled(cron="0 0 12 * * ?")
	@Async
	public void resetDayAward() throws MyException{
		System.out.println("baby181012重置每天奖品派发数..."+Thread.currentThread());
		Setting awardControl = Setting.findBykey("AwardControl");
		int rows = 0;
		boolean open = false;
		if (awardControl == null || org.springframework.util.StringUtils.isEmpty(awardControl)) {
			 open = true;
		} else {
			open =  Setting.findBykey("AwardControl").getValue().equalsIgnoreCase("open");
		}
		List<Award> awards = dao.list(Award.class,"SELECT * FROM "+orm.getTableName(Award.class));
		if (open && try2Lock("xxx_award_reset",1)) {
			if (awards!=null && awards.size()>0) {
				for (Award award : awards) {
					System.out.println(award.toString());
					award.setTodayOutNum(0);
					try {
						dao.update(award,"todayOutNum");
						rows++;
						//int i =10/0;
					} catch (DataAccessException e) {
						throw new MyException("xxxx更新奖品异常");
					} catch (Exception e) {
						//TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						throw new MyException(e.getMessage());
					} finally {
						cacheClient.delete("xxxxx_award_reset");
					}
				}
			}
			System.out.println("xxxx重置每天奖品派发数执行完毕,受影响的行:"+ rows);
		}
		
    }  
	
	@Scheduled(cron="0 0 12 * * ?")
	@Async
	public void resetLotteryChance() throws MyException{
		System.out.println("xxxx重置抽奖次数Begin...."+Thread.currentThread());
		int rows = 0;
		try {
			if ( try2Lock("xxxx_lotterychance_reset",1)) {
				 dao.getJdbcTemplate().update("UPDATE "+orm.getTableName(User.class)+" SET remain = 0");
				List<User> users = dao.list(User.class, "SELECT * FROM "+orm.getTableName(User.class));
				if (users!=null && users.size()>0) {
					for(User user :users) {
						dao.deleteCache(User.class, user.getId());
						rows++;
					}
				}
				System.out.println("xxxx重置抽奖次数End,受影响的行:"+rows);
			}
		} catch (DataAccessException e) {
			e.printStackTrace();
			throw new MyException("xxxx重置抽奖DataAccessException");
		} catch (Exception e) {
			throw new MyException(e.getMessage());
		} finally {
			cacheClient.delete("xxxx_lotterychance_reset");
		}
		
	}
	
	private boolean try2Lock (String key ,Object value) {
		if (!cacheClient.add(key, value)) {
			return false;
		}
		return true;
	}
}

xml配置如下:

<bean id="taskJob" class="cn.xxx.activity.task.TaskJob" lazy-init="false">
	</bean>
	
	<!-- 开启Schedule注解支持 -->
	<task:annotation-driven executor="taskExecutor" proxy-target-class="true" scheduler="myScheduler" />
	<task:scheduler id="myScheduler" pool-size="10"/>
	
	<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <!-- 核心线程数 -->
        <property name="corePoolSize" value="5" />
        <!-- 最大线程数 -->
        <property name="maxPoolSize" value="10" />
        <!-- 队列最大长度 -->
        <property name="queueCapacity" value="25" />
        <!-- 线程池维护线程所允许的空闲时间,默认为60s -->
        <property name="keepAliveSeconds" value="300" />
         <!-- 线程池对拒绝任务(无线程可用)的处理策略 ThreadPoolExecutor.CallerRunsPolicy策略 ,调用者的线程会执行该任务,如果执行器已关闭,则丢弃.  -->
        <property name="rejectedExecutionHandler">
            <bean class="java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy" />
        </property>
	</bean>

我初步怀疑是Spring3.2版本太低,可能存在bug。如果有哪位大佬指导为什么,麻烦告知一下。感谢!!!

补充:2019/11/29

  • Async 注解可以指定具体的调度器Executor
  • 方法的返回值类型必须是空或者是Future
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值