quartz 基於spring註解實現
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<task:annotation-driven /> <!-- 定時器開關-->
<!-- <bean id="jobDetail" class="com.wanke.manager.quartz.TestQuartz"></bean>
<task:scheduled-tasks>
這裡表示的是每隔五秒執行一次
<task:scheduled ref="jobDetail" method="show" cron="*/5 * * * * ?" />
<task:scheduled ref="jobDetail" method="print" cron="*/10 * * * * ?"/>
</task:scheduled-tasks> -->
<!-- 自動掃描的包名 -->
<context:component-scan base-package="com.wanke.manager.quartz" />
</beans>
後臺
package com.wanke.manager.quartz;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.wanke.common.third.express.ExpressData;
import com.wanke.common.third.sms.Sms;
import com.wanke.manager.service.ExpressService;
import com.wanke.manager.service.OrderService;
import com.wanke.model.Express;
import com.wanke.model.Order;
/**
* 基於註解的定時器
*/
@Component
public class UserQuartz {
private static final Logger LOGGER = LoggerFactory.getLogger(UserQuartz.class);
@Autowired
public ExpressService expressService;
@Autowired
public OrderService orderService;
// @Scheduled(cron = "0 0 1 * * ?")//每天凌晨1點整
// @Scheduled(cron = "0 30 0 * * ?")//每天凌晨0點30分
// @Scheduled(cron = "0 */60 * * * ?")//1小時處理一次
/**
* 定時計算。每天凌晨 08:00 執行一次
*/
@Scheduled(cron = "0 0 8 * * ?")
public void remindUser() {
// 每天早上8點給使用者傳送短息提示使用者吃藥.....
//傳送訂單成功簡訊
//單獨測試一個使用者是否能正常傳送短息
String mobile = "13655459823";
String name = "範女士";
String content = "尊敬的"+name+"您好!請按時服用為期一個月的高血壓精準管理產品,萬克醫學祝您體驗愉快!諮詢電話010-56021932";
Boolean bool = Sms.sendBase(mobile, content);
//張傑
Sms.sendBase("13552593242", content);
//巨集偉
Sms.sendBase("13260434372", content);
if(bool){
LOGGER.info(">>>>>>每天早上8:00傳送使用者吃藥提醒簡訊成功:mobile="+mobile);
}else{
LOGGER.error(">>>>>>每天早上8:00傳送使用者吃藥提醒簡訊失敗:mobile="+mobile);
}
//以下為正常邏輯
//type----寄送型別(0-取樣盒寄出,1-取樣盒寄回,2-產品寄出
//Status----0-未寄出,1-已寄出,2-已收貨
/*try {
List<Express> list = expressService.queryByTypeAndStatus(2,2);
for (Express express : list) {
//針對每個使用者傳送簡訊提醒
Order order = orderService.queryOrderById(express.getOrderId());
//定製不同使用者的傳送資訊
//String content = "您的檢測報告及方案已完成,請進入公眾號的個人中心檢視,客服會盡快聯絡您預約報告解讀, 諮詢熱線:010-56021932";
Boolean result = Sms.sendBase(order.getMobile(), content);
if(result){
LOGGER.info(">>>>>>每天早上8:00傳送使用者吃藥提醒簡訊成功:mobile="+mobile);
}else{
LOGGER.error(">>>>>>每天早上8:00傳送使用者吃藥提醒簡訊失敗:mobile="+mobile);
}
}
} catch (Exception e) {
e.printStackTrace();
LOGGER.error(">>>>>>每天早上8:00傳送使用者吃藥提醒簡訊失敗!---錯誤資訊="+e);
}*/
}
/**
* 啟動時執行一次,之後每隔1小時秒執行一次
*/
@Scheduled(cron = "0 */60 * * * ?")
public void updateExpressAndOrderStatus() {
// 定時查詢物流資訊更新物流表和訂單表中產品的狀態.....
//type----寄送型別(0-取樣盒寄出,1-取樣盒寄回,2-產品寄出
//Status----0-未寄出,1-已寄出,2-已收貨
/*try {
List<Express> list = expressService.queryByTypeAndStatus(2,1);
for (Express express : list) {
boolean result = ExpressData.isSuccess(express.getExpressCompany(), express.getExpressNo());
if(result){
//使用者收貨成功,則更新資料庫記錄(更新訂單表和物流表...)
expressService.updateExpressAndOrder(express);
//Order order = orderService.queryOrderById(express.getOrderId());
//express.setStatus(2);
//order.setExpressProductOutStatus(2);
//expressService.updateExpress(express);
//orderService.updateOrder(order)
}
}
} catch (Exception e) {
e.printStackTrace();
LOGGER.error(">>>>>>定時執行產品寄出物流資訊和訂單資訊狀態失敗!---錯誤資訊="+e);
}*/
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>暫未開通更新物流使用者收貨成功的標記");
}
}
pom
<!-- 定時任務 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
<exclusions>
<exclusion>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>${quartz.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
</dependencies>