本文共 5419 字,大约阅读时间需要 18 分钟。
--------------------------------------------------quartz定时框架---------------------------------------------------------- 一、quartz定时框架(和spring整合会用) 1、官网:http://www.quartz-scheduler.org/ 2、导入坐标 <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency> 3、JobDetail 工作对象,指具体做什么事 Trigger 触发器,指什么时间做事,事情做多少次,侧重做事的频率 Scheduler 定时器对象,关联前两个对象,开启定时任务 4、Cron表达式 1)字段取值及字符 字段名 允许的值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日 1-31 , - * ? / L W C 月 1-12 or JAN-DEC , - * / 周几 1-7 or SUN-SAT , - * ? / L C # 年 (可选字段) empty, 1970-2099 , - * / 2)具体说明 重点掌握: Day-of-month 和 Day-of-week必须有一个指定为? "?"字符:表示不确定的值 ","字符:指定数个值 "-"字符:指定一个值的范围"/"字符:指定一个值的增加幅度。n/m表示从n开始,每次增加m
------------------------------------------------定时发短信的具体做法---------------------------------------------------------
1.建立war工程
2、导入坐标
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency>以及spring坐标
3.配置
web.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- spring核心监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
applicationContext-quartz.xml文件:
<bean id="myJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="myBean"></property> <property name="targetMethod" value="sendMessage"></property> </bean> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="myJob" /> <property name="cronExpression" value="*/5 * * ? * *"></property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger" /> <!-- <ref bean="wayBillIndexSyncTrigger" /> --> </list> </property> </bean>4.写myBean类,以及类中sendMessage的方法
package com.sun.service; import org.springframework.stereotype.Service; import com.aliyuncs.exceptions.ClientException; import com.sun.util.AliSmsUtils; @Service public class MyBean { public void sendMessage() throws ClientException{ System.out.println("00000000000"); AliSmsUtils.sendSms("17786195084", null, "1314"); } }
5.阿里大于发短信的工具类
public class AliSmsUtils { //产品名称:云通信短信API产品,开发者无需替换 static final String product = "Dysmsapi"; //产品域名,开发者无需替换 static final String domain = "dysmsapi.aliyuncs.com"; // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找) static final String accessKeyId = " "; static final String accessKeySecret = " "; public static SendSmsResponse sendSms(String telephone,String username,String randomCode) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象-具体描述见控制台-文档部分内容 SendSmsRequest request = new SendSmsRequest(); //必填:待发送手机号 request.setPhoneNumbers(telephone); //必填:短信签名-可在短信控制台中找到 request.setSignName("孙先生"); //云通信 //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(" "); // SMS_74285007 //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 String templateJson1 = "{ \"code\":\""+randomCode+"\"}"; // String templateJson2 = "{\"name\":\""+username+"\", \"code\":\""+randomCode+"\", \"other\":\""+other+"\"}"; request.setTemplateParam(templateJson1); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 // request.setOutId("yourOutId"); //hint 此处可能会抛出异常,注意catch SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); return sendSmsResponse; } public static QuerySendDetailsResponse querySendDetails(String telephone,String bizId) throws ClientException { //可自助调整超时时间 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient,暂不支持region化 IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象 QuerySendDetailsRequest request = new QuerySendDetailsRequest(); //必填-号码 request.setPhoneNumber(telephone); //可选-流水号 request.setBizId(bizId); //必填-发送日期 支持30天内记录查询,格式yyyyMMdd SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd"); request.setSendDate(ft.format(new Date())); //必填-页大小 request.setPageSize(10L); //必填-当前页码从1开始计数 request.setCurrentPage(1L); //hint 此处可能会抛出异常,注意catch QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request); return querySendDetailsResponse; }
}
转载地址:http://valti.baihongyu.com/