存放调度器(Job 和 Trigger)信息的xml配置文件:

这是某个指定的要实现的定时任务:
<!--  每天给项目经理发送短信避免短信服务挂了 定时每天08:30执行-->
<job>
<name>SendToManagerJob</name>
<job-class>com.xxx.cscns.sms.SendToManagerJob</job-class>
</job> <trigger>
<cron>
<name>SendToManagerJob</name>
<job-name>SendToManagerJob</job-name>
<cron-expression>0 30 8 * * ?</cron-expression>
</cron>
</trigger>

这是一个实时执行的任务服务,负责推送系统数据库短信信息表中的数据给运营商的接口完成发送短信操作:

<!-- 实时扫描短信数据表,没有发送的调用运营商给的接口推送,完成发送短信操作 -->
<job>
<name>ReceiveMessageJob</name>
<job-class>com.xxx.cscns.sms.ReceiveMessageJob</job-class>
</job> <trigger>
<simple>
<name>ReceiveMessageJob</name>
<job-name>ReceiveMessageJob</job-name>
<repeat-count>-1</repeat-count>
<repeat-interval>1</repeat-interval>
</simple>
</trigger>

上面配置的实时执行的意思就是间隔1毫秒执行无数次,由这个服务配合才能实现其他的任务服务,其他的任务就是完成插入指定的一些信息进入系统的数据表中,再由这个实时的推送服务第一时间推送给运营商接口完成发送操作,发送给指定的手机号;

所以发送短信必要要有合作的运营商的参数,ip,端口,账号和密码;

实时执行的任务服务job实现类:

package com.xxx.cscns.sms;

import java.util.Date;
import java.util.List; import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired; import com.xxx.cert.basic.certinfo.inter.ICertInfo;
import com.xxx.core.grammar.Record;
import com.xxx.core.utils.config.ConfigUtil;
import com.xxx.core.utils.container.ContainerFactory;
import com.xxx.core.utils.string.StringUtil;
import com.xxx.cscns.impl.SMSReceiveImpl;
import com.xxx.cscns.service.MessageService;
import com.xxx.cscns.utils.ConstValue;
import com.xxx.database.jdbc.connection.DataSourceConfig;
import com.xxx.frame.service.metadata.code.api.ICodeItemsService;
import com.xxx.frame.service.metadata.code.entity.CodeItems;
import com.esotericsoftware.minlog.Log;
import com.linkage.netmsg.NetMsgclient;
import com.linkage.netmsg.server.ReceiveMsg; /**
* 短信接收服务
* @author wmqiang
* @see [相关类/方法]
* @since [产品/模块版本]
*/
@DisallowConcurrentExecution
public class ReceiveMessageJob implements Job {

// 封装的获取运营商的参数,ip,端口,账号和密码
private static String ip = ConstValue.QXT_IP;
private static int port = Integer.parseInt(ConstValue.QXT_PORT);
private static String userName = ConstValue.QXT_USERNAME;
private static String pwd = ConstValue.QXT_PWD; //升级系统数据库连接配置信息
public static String sjUrl = ConstValue.sjUrl;
public static String sjUsername = ConstValue.sjUsername;
public static String sjPassword = ConstValue.sjPassword; @Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
try {
System.out.println("------------进入短信发送服务-----------\r\n");
MessageService messgeService = new MessageService();
MessageService sjmessgeService = new MessageService(sjUrl,sjUsername,sjPassword); NetMsgclient client = new NetMsgclient();
ReceiveMsg receiveMsg = new SMSReceiveImpl();
client = client.initParameters(ip, port, userName, pwd, receiveMsg);
/* 登录认证 */
boolean isLogin = client.anthenMsg(client);
Log.info("------------receive " + isLogin + " loginsucess------------\r\n");
if (isLogin) { String phone = "";
String content = "";
String rowguid = "";
//获取分表表名,找到数据表就行,不用管
ICodeItemsService iCodeItemsService = ContainerFactory.getContainInfo().getComponent(ICodeItemsService.class);
List<CodeItems> codeItemsList = iCodeItemsService.listCodeItemsByCodeName("短信分表");
if(codeItemsList != null){
for(CodeItems codeitem : codeItemsList){
//分表表名
String tablename = codeitem.getItemText();
// 调用方法找出系统数据表中没有发送的短信
List<Record> listInfo = messgeService.getSendMessage(tablename);
for (Record info : listInfo) {
phone = info.get("MessageTarget");
content = info.get("Content");
rowguid = info.get("MessageItemGuid");
if (StringUtil.isNotBlank(content) && StringUtil.isNotBlank(phone)) {
System.out.println(new Date() + "【xxx项目】短信发送服务已发送短信:" + phone + " 内容:" + content + "\r\n");
client.sendMsg(client, 0, phone, content, 1);
messgeService.updateMessageCenterByRowguid(rowguid,tablename);
}
}
//升级项目短信发送
List<Record> SJlistInfo = sjmessgeService.getSendMessage(tablename);
for (Record info : SJlistInfo) {
phone = info.get("MessageTarget");
content = info.get("Content");
rowguid = info.get("MessageItemGuid");
if (StringUtil.isNotBlank(content) && StringUtil.isNotBlank(phone)) {
client.sendMsg(client, 0, phone, content, 1);
sjmessgeService.updateMessageCenterByRowguid(rowguid,tablename);
System.out.println(new Date() + "【升级项目】短信发送服务已发送短信:" + phone + " 内容:" + content + "\r\n");
}
}
}
}
client.closeConn();
Thread.sleep(5000);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally { }
} catch (Exception e) {
e.printStackTrace();
} }
}

其中// 封装的获取运营商的参数,ip,端口,账号和密码,在一个配置文件中配置参数,

// 调用方法找出系统数据表中没有发送的短信messgeService.getSendMessage(tablename):
public class MessageService {
/**
* 找出未发送短信
* @return
* @exception/throws [违例类型] [违例说明]
* @see [类、类#方法、类#成员]
*/
public List<Record> getSendMessage(String tablename) {
List<Record> list = new ArrayList<>();
// 获取到短信表中需要发送短信的信息即可
/*String strSql = "SELECT * FROM Messages_Center WHERE SendMode=1 and ((IsSchedule=1 and ScheduleSendDate< NOW() ) or ( IsSchedule=0) ) ";*/
String strSql = "SELECT * FROM "+tablename+" WHERE SendMode=1 and ((IsSchedule=1 and ScheduleSendDate< NOW() )"
+ " or ( IsSchedule=0) ) "
+ " and GENERATEDATE >= (select date_sub(now(), interval 2 hour)) ";
try {
list = dao.findList(strSql, Record.class);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dao != null){
dao.close();
}
}
return list;
} }

以上后台代码就是实现了实时发送短信的任务;

接下来有需要发送特定短信内容的业务场景中,只要往存储短信的这张数据表中插数据即可;

有需要发送特定短信内容的定时任务实现,也就是配置特定的调度完成往存储短信的这张数据表中插数据操作就行;

如上面的那个配置

每天给项目经理发送短信避免短信服务挂了 定时每天08:30执行

实例后台代码:

根据配置的调度信息,找到job的实现类和其业务逻辑实施层方法,如下实例:
job实现类的代码:
package com.xxx.cscns.sms;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import com.xxx.core.xxxFrameDsManager;
import com.xxx.cscns.service.MessageService; @DisallowConcurrentExecution
public class SendToManagerJob implements Job{
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
xxxFrameDsManager.begin(null);
MessageService messageService = new MessageService();
// 调用业务逻辑实施层方法,实现发短信操作
messageService.sendToManger();
System.out.println("上班前半小时发送一次短信给项目经理成功!");
}
catch (Exception e) {
xxxFrameDsManager.rollback();
e.printStackTrace();
System.out.println("上班前半小时发送一次短信给项目经理异常:"+e.toString());
}
finally {
xxxFrameDsManager.close();
}
}
}
封装的业务逻辑实施层的方法:
   /*
*
* 每天给项目经理发送短信避免短信服务挂了
*/
public void sendToManger() {
try {
//系统参数配置项目经理手机号码,这儿是封装的方法,只要能获取到项目经理手机号就行
String messageTarget = new FrameConfigService9().getFrameConfigValue("ASP_MESSAGE_TEST_TEL");
//时间格式化
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sql = "insert into Messages_center ( MessageItemGuid, Content, GenerateDate, IsSchedule, MessageTarget, sendMode,messagetype)";
sql += "values( '" + UUID.randomUUID().toString() + "', '【XXX项目】短信服务正常。', to_date('" + sdf2.format(new Date())+ "', 'yyyy-mm-dd hh24:mi:ss'), 0, '" + messageTarget + "', 1,'短信')";
if(StringUtil.isNotBlank(messageTarget)){
dao.execute(sql);
}
}
catch (Exception e) {
e.printStackTrace();
dao.rollBackTransaction();
}
finally {
if (dao != null){
dao.close();
}
}
}

其中最后的业务逻辑实施层的方法中,就是完成了定时往特定的存储短信内容的数据表中,插入相应的数据的操作;

												

quartz-job实现实时或定时发送短信任务的更多相关文章

  1. python每天定时发送短信脚本

    最近业务上需要每天解析txt文本或者excel文件,读取内容发送短信,发送的时间段可控,用python实现 安装pip依赖 pip install -r requirement.txt xlrd Py ...

  2. JAVA实现多线程处理批量发送短信、APP推送

    /** * 推送消息 APP.短信 * @param message * @throws Exception */ public void sendMsg(Message message) throw ...

  3. 使用 Python 发送短信?

    上回食行生鲜签到,我们说到怎么把签到结果发出来,于是就找到了 Twilio. Twilio 是一个位于加利福尼亚的云通信(PaaS)公司,致力于为开发者提供通讯模块的 API.由于 Twilio 为试 ...

  4. PHP发送短信功能

    发送短信的功能主要在于获得短信接口后,在函数中模仿用户行为,例如浏览器跳转输出短信接口的链接. 需要运用的函数为 curl_init(); curl_setopt(); curl_exec(); cu ...

  5. WPF MVVM下做发送短信小按钮

    最近做一个项目,因为涉及到注册,因此需要发送短信,一般发送短信都有一个倒计时的小按钮,因此,就做了一个,在此做个记录. 一.发送消息 没有调用公司的短信平台,只是模拟前台生成一串数字,将此串数字输出一 ...

  6. NetCore 阿里大于发送短信

    使用阿里大于API发送短信,但阿里没有提供NetCore 的API,自己看了下源码重写了发短信这个部分 public class MessageSender { private readonly st ...

  7. android 中调用接口发送短信

    android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...

  8. Android 学习第13课,android 实现发送短信的功能

    1. 界面布局 界面代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  9. ios调用本地拨打电话,发送短信

    电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话   [[UIApplication sharedApplicat ...

随机推荐

  1. 【CF873F】Forbidden Indices 后缀自动机

    [CF873F]Forbidden Indices 题意:给你一个串s,其中一些位置是危险的.定义一个子串的出现次数为:它的所有出现位置中,不是危险位置的个数.求s的所有子串中,长度*出现次数的最大值 ...

  2. Office2010安装需要MSXML版本6.10.1129.0的方法

    今天给朋友装Office2010,由于朋友之前使用的是绿化版的0ffice2007,所以卸载后安装Office遇到了若要安装Office2010,需要在计算机上安装MSXML版本6.10.1129.0 ...

  3. html 自动弹出框

    1.点击div外部隐藏, //*代表tip_box所包含的子元素 $('body').click(function(e) { var target = $(e.target); if(!target. ...

  4. Ubuntu安装配置MySQL数据库,Apache,PHP

    MySQL安装 第一步:首先检查是否安装: sudo netstat -tap| grep mysql 如果没有任何反应则没有安装 第二步:执行命令安装mysql: sudo apt-get inst ...

  5. php下载文件,解压文件,读取并写入新文件

    以下代码都是本人在工作中遇到的问题,并完成的具体代码和注释,不多说,直接上代码: <?php      //组织链接      $dataurl = "http://118.194.2 ...

  6. 三维重建项目:Photo Tourism: Exploring Photo Collections in 3D

    项目地址:http://phototour.cs.washington.edu/ Photo Tourism是华盛顿大学的SFM重建的过程 Paper:Photo Tourism: Exploring ...

  7. POJ 2318 - TOYS - [计算几何基础题]

    题目链接:http://poj.org/problem?id=2318 Time Limit: 2000MS Memory Limit: 65536K Description Calculate th ...

  8. POJ 1637 - Sightseeing tour - [最大流解决混合图欧拉回路]

    嗯,这是我上一篇文章说的那本宝典的第二题,我只想说,真TM是本宝典……做的我又痛苦又激动……(我感觉ACM的日常尽在这张表情中了) 题目链接:http://poj.org/problem?id=163 ...

  9. gis 相关资料

    --gis原理学习 http://group.cnblogs.com/GIS/best-1.html http://www.cnblogs.com/SuperXJ/tag/移动GIS/ --gis坐标 ...

  10. Oracle故障排查之oracle解决锁表问题

    --step 1:查看被阻塞会话等待事件 select sid, event, username, lockwait, sql.sql_text  from v$session s, v$sql sq ...