自动任务类:
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class ReCodeBack implements Job {
private static final Logger LOGGER = LoggerFactory.getLogger(ReCodeBack.class);
@Autowired
ReCodeBackTag reCodeBackTag;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
reCodeBackTag.backupsReCode();
}
}
自动任务注解:
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
任务需求是需要添加事务
运用spring注解@Transactional注解
/**
* 添加(删除)表数据
* @param object
* @param modular
* @return
*/
@Transactional(rollbackFor = Exception.class)
public int tableOperation (Object object,String modular){
int count = 0;
try{
.......
}catch (Exception e) {
LOGGER.info("[添加(删除)表数据] ERROR :{}", e.getMessage());
//事务回滚
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//任务异常中断,立即重新运行
JobExecutionException e2 = new JobExecutionException(e);
e2.setRefireImmediately(true);
throw e2;
}finally {
return count;
}
}
注意:当作者在自动任务类进行使用@Transactional注解时,程序会进行报错(太懒……原因没搞清楚,后续会补上),所以作者分成了两个类来编写的,一个定时调用类,一个业务类,如上代码。
来源:oschina
链接:https://my.oschina.net/u/4265966/blog/3324557