本文主要是介绍sping 定时任务,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、创建定时任务 配置文件
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<task:scheduled-tasks><task:scheduled ref="sendNotifyMsg" method="senMsg" cron="59 59 23 22 2 ?"/>
</task:scheduled-tasks>
</beans>
2、注入bean
<bean id="sendNotifyMsg" class="com.message.task.sendNotifyMsg"></bean>
3、定时任务类
public class sendNotifyMsg {@Resourceprivate MemoryUserService memoryUserServiceImpl;@Resourceprivate MemoryFootprintService memoryFootprintService;@Resourceprivate MemoryFormidService memoryFormidServiceImpl;@Resourceprivate MemoryUserService memoryUserService;ExecutorService pool = Executors.newFixedThreadPool(3);private final Logger logger = LoggerFactory.getLogger(sendNotifyMsg.class);public void senMsg() {MemoryUser mu = new MemoryUser();mu.setFabulous("1");mu.setIsDeleted("N");List<MemoryUser> ls = memoryUserServiceImpl.selectUsersByParam(mu);for (MemoryUser memoryUser : ls) {final PapaWxMsg papaWxMsg = new PapaWxMsg();//查询用户七天内的formidMemoryFormid mf = new MemoryFormid();mf.setUserId(memoryUser.getId() + "");final List<MemoryFormid> lss = memoryFormidServiceImpl.findFprmidListByTime(mf);if (lss != null && lss.size() > 0) {//设置openidpapaWxMsg.setTouser(memoryUser.getOpenid());papaWxMsg.setName("留下记忆,赢iPhone X");papaWxMsg.setContent("活动结束,已公布获奖名单");papaWxMsg.setTimeDate("2018年2月26日领奖截止");papaWxMsg.setForm_id(lss.get(0).getFormid());//开启多线程处理pool.execute(new Runnable() {@Overridepublic void run() {String rss = WeChatAppletUtil.activeEndMsg(papaWxMsg);if ("success".equals(rss)) {MemoryFormid mfd = new MemoryFormid();mfd.setId(lss.get(0).getId());mfd.setIsDeleted("Y");memoryFormidServiceImpl.updateFprmid(mfd);}}});}}}
<task:scheduled ref="sendNotifyMsg" method="senMsg" cron="59 59 23 22 2 ?"/>
ref:执行定时任务类 ;method:执行的方法 cron:时间 ,上文执行时间:2月22日23点59分59秒
这篇关于sping 定时任务的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!