博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
定时发短信(quartz框架,阿里大于)
阅读量:4151 次
发布时间:2019-05-25

本文共 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/

你可能感兴趣的文章
码农:很多人称我“技术”,感觉这是不尊重!纠正无果后果断辞职
查看>>
php程序员看过来,这老外是在吐糟你吗?看看你中了几点!
查看>>
为什么说程序员是“培训班出来的”就是鄙视呢?
查看>>
码农吐糟同事:写代码低调点不行么?空格回车键与你有仇吗?
查看>>
阿里p8程序员四年提交6000次代码的确有功,但一次错误让人唏嘘!
查看>>
一道技术问题引起的遐想,最后得出结论技术的本质是多么的朴实!
查看>>
985硕士:非科班自学编程感觉还不如培训班出来的,硕士白读了?
查看>>
你准备写代码到多少岁?程序员们是这么回答的!
查看>>
码农:和产品对一天需求,产品经理的需求是对完了,可我代码呢?
查看>>
程序员过年回家该怎么给亲戚朋友解释自己的职业?
查看>>
技术架构师的日常工作是什么?网友:搭框架,写公共方法?
查看>>
第四章 微信飞机大战
查看>>
九度:题目1008:最短路径问题
查看>>
九度Online Judge
查看>>
九度:题目1027:欧拉回路
查看>>
九度:题目1012:畅通工程
查看>>
九度:题目1017:还是畅通工程
查看>>
九度:题目1034:寻找大富翁
查看>>
第六章 背包问题——01背包
查看>>
51nod 分类
查看>>