package com.winhands.quartz;
创新互联建站专业为企业提供虎丘网站建设、虎丘做网站、虎丘网站设计、虎丘网站制作等企业网站建设、网页设计与制作、虎丘企业网站模板建站服务,10多年虎丘做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.winhands.base.service.BaseService;
import com.winhands.util.BaseConstant;
import com.winhands.web.dict.entity.DictCommon;
import com.winhands.web.xfhf.entity.SmsOutBox;
import com.winhands.web.xfhf.entity.SmsVisit;
import com.winhands.web.xfhf.entity.TaskInfo;
public class SmsSendSync {
public BaseService baseService;
public BaseService baseServiceMas;
public static final Logger logger = LoggerFactory.getLogger(SmsSendSync.class);
//同步短信到mas机待发短信表
public void work() {
// 任务ID 100001
// 首先查询任务是否正在执行
// 查询列表
TaskInfo taskInfo = new TaskInfo();
taskInfo.setTask_id("100001");
try {
taskInfo = (TaskInfo) baseService.queryForObject("sms_send.queryTaskStatus", taskInfo);
//如果状态是处室状态则执行,否则此次任务不执行
if (BaseConstant.task_status_cs.equals(taskInfo.getTask_status())) {
taskInfo.setTask_status(BaseConstant.task_status_zx);//设置任务执行状态为执行
taskInfo.setTask_last_start_time(new Date());//设置任务执行开始时间
baseService.update("sms_send.updateTaskStatus", taskInfo);//
//移动号码库
HashMapString,String hashMap = new HashMapString,String();
//134 、135、136、137、138、139、150、151、152、158、159、182、183、184。157、187、188
hashMap.put("134", "134");
hashMap.put("135", "135");
hashMap.put("136", "136");
hashMap.put("137", "137");
hashMap.put("138", "138");
hashMap.put("139", "139");
hashMap.put("150", "150");
hashMap.put("151", "151");
hashMap.put("152", "152");
hashMap.put("157", "157");
hashMap.put("158", "158");
hashMap.put("159", "159");
hashMap.put("182", "182");
hashMap.put("183", "183");
hashMap.put("184", "184");
hashMap.put("187", "187");
hashMap.put("188", "188");
SmsVisit smsSend = new SmsVisit();
smsSend.setSms_status(BaseConstant.sms_status_csh);
List list = baseService.queryForList("sms_send.queryForSmsTask", smsSend);
List smsMasList = new ArrayList();
List smsList = new ArrayList();
if (list == null||list.size()==0) return;// 如果列表为空,没有待办任务,则任务结束
for (int i = 0; i list.size(); i++) {
smsSend = (SmsVisit) list.get(i);
//String msg = messagecontent(smsSend);
String msg = smsSend.getSpare_2();
if(hashMap.get(smsSend.getVisit_phone().substring(0, 3))!=null){
SmsOutBox smsOutBox = new SmsOutBox();
smsOutBox.setSismsid(smsSend.getP_id());
smsOutBox.setReqdeliveryreport(BaseConstant.NoReport);
smsOutBox.setApplicationid(BaseConstant.APPLICATIONID);
smsOutBox.setDestaddr(smsSend.getVisit_phone());
smsOutBox.setMsgfmt(BaseConstant.MSGFMT);
smsOutBox.setRequesttime(new Date());
smsOutBox.setExtcode(smsSend.getExtccode());
smsOutBox.setSendmethod(BaseConstant.NoMsg);// 正常短信
smsOutBox.setMessagecontent(msg);
smsMasList.add(smsOutBox);
}else{
boolean con = true;
String lastMsg = msg;
int xh =0;
while(con){
lastMsg = msg.substring(0,msg.length() 55 ? 55 : msg.length());
msg = msg.substring(msg.length() 55 ? 55 : msg.length(),msg.length());
if (msg.equals("")) {
con = false;
}
SmsOutBox smsOutBox = new SmsOutBox();
smsOutBox.setSismsid(smsSend.getP_id()+(xh==0?"":xh));
xh++;
smsOutBox.setReqdeliveryreport(BaseConstant.NoReport);
smsOutBox.setApplicationid(BaseConstant.APPLICATIONID);
smsOutBox.setDestaddr(smsSend.getVisit_phone());
smsOutBox.setMsgfmt(BaseConstant.MSGFMT);
smsOutBox.setRequesttime(new Date());
smsOutBox.setExtcode(smsSend.getExtccode());
smsOutBox.setSendmethod(BaseConstant.NoMsg);// 正常短信
smsOutBox.setMessagecontent(lastMsg);
smsMasList.add(smsOutBox);
}
}
SmsVisit smsSendTemp = new SmsVisit();
smsSendTemp.setP_id(smsSend.getP_id());
//smsSendTemp.setSpare_2(msg);//短信内容
smsSendTemp.setSms_status_time(new Date());
smsSendTemp.setSms_status(BaseConstant.sms_status_yfswg);
smsList.add(smsSendTemp);
}
baseService.insertBatch("sms_send.insertSmsOutBox", smsMasList);//插入本地历史记录表
baseServiceMas.insertBatch("sms_mas.insertSmsOutBox", smsMasList);//插入mas机待发送表
baseService.updateBatch("sms_send.updateSmsVisit", smsList);//更新回访记录状态
}
} catch (Exception e) {
e.printStackTrace();
//baseService.saveLog("同步短信到mas机待发短信表", "100001", "同步短信到mas机待发短信表错误");
logger.error("同步短信到mas机待发短信表错误", getTrace(e));
}finally{
try {
taskInfo.setTask_status(BaseConstant.task_status_cs);//设置任务执行状态为初始
taskInfo.setTask_last_end_time(new Date());//设置任务执行结束时间
taskInfo.setTask_status_desc("任务执行结束");
baseService.update("sms_send.updateTaskStatus", taskInfo);//
} catch (Exception e2) {
e2.printStackTrace();
// TODO: handle exception
}
}
}
public String messagecontent(SmsVisit smsSend){
//XX,您好!您于XX月XX日办理XX业务(或XX,您好!XX大队于XX月XX日对你单位进行监督检查/行政处罚/火灾调查),现进行满意度回访,请回复数字:1为满意;2为基本满意;3为不满意。感谢您对消防工作的支持!回复免费。宿迁市公安消防支队
//首先根据业务类型来判断采用哪种模板 监察
StringBuffer msg = new StringBuffer("");
if(BaseConstant.jiancha.equals(smsSend.getVisit_deal_business_type())){
msg.append(smsSend.getVisit_name_cn())
.append(",您好!")
.append(smsSend.getVisit_deal_org_name())
.append("于")
.append(smsSend.getVisit_deal_time().substring(4, 6)).append("月")
.append(smsSend.getVisit_deal_time().substring(6, 8)).append("日")
.append("对你单位进行").append(DictCommon.getEntryMap(BaseConstant.jiancha).get(smsSend.getVisit_deal_business()))
.append(",现进行满意度回访,请回复数字:1为满意;2为基本满意;3为不满意。感谢您对消防工作的支持!");
}else if(BaseConstant.yewu.equals(smsSend.getVisit_deal_business_type())){
msg.append(smsSend.getVisit_name_cn())
.append(",您好!您于")
.append(smsSend.getVisit_deal_time().substring(4, 6)).append("月")
.append(smsSend.getVisit_deal_time().substring(6, 8)).append("日")
.append("办理").append(smsSend.getVisit_deal_business()).append("业务,")
.append("现进行满意度回访,请回复数字:1为满意;2为基本满意;3为不满意。感谢您对消防工作的支持!");
}
return msg.toString();
}
public BaseService getBaseService() {
return baseService;
}
public void setBaseService(BaseService baseService) {
this.baseService = baseService;
}
public BaseService getBaseServiceMas() {
return baseServiceMas;
}
public void setBaseServiceMas(BaseService baseServiceMas) {
this.baseServiceMas = baseServiceMas;
}
public static void main(String[] args){
List smsMasList = new ArrayList();
try {
String msg = "***,您好!*********于2015年5月2日对你单位进行监督检查,现进行满意度回访,请回复数字:1为满意;2为基本满意;3为不满意。感谢您对消防工作的支持!";
SmsOutBox smsOutBox = new SmsOutBox();
smsOutBox.setSismsid("12344");
smsOutBox.setReqdeliveryreport(BaseConstant.NoReport);
smsOutBox.setApplicationid(BaseConstant.APPLICATIONID);
smsOutBox.setDestaddr("111");
smsOutBox.setMsgfmt(BaseConstant.MSGFMT);
smsOutBox.setRequesttime(new Date());
smsOutBox.setExtcode("2222");
smsOutBox.setSendmethod(BaseConstant.NoMsg);// 正常短信
boolean con = true;
String lastMsg = msg;
while(con){
lastMsg = msg.substring(0,msg.length() 55 ? 55 : msg.length());
msg = msg.substring(msg.length() 55 ? 55 : msg.length(),msg.length());
if (msg.equals("")) {
con = false;
}
SmsOutBox smOutBox_temp = new SmsOutBox();
smOutBox_temp = smsOutBox;
smOutBox_temp.setMessagecontent(lastMsg);
smsMasList.add(smOutBox_temp);
}
for(Object o:smsMasList){
SmsOutBox smsOutBox2 = (SmsOutBox)o;
System.out.println(smsOutBox2.getMessagecontent());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getTrace(Throwable t) {
StringWriter stringWriter= new StringWriter();
PrintWriter writer= new PrintWriter(stringWriter);
t.printStackTrace(writer);
StringBuffer buffer= stringWriter.getBuffer();
return buffer.toString();
}
}
短信不是你想发 想发就能发,是收费的,我们公司是租用的移动MAS平台的短信发送接收平台,上面那是和一个和mas同步的一个小程序,租用别人的mas平台是收费的,平台有很多自己选择,有发送接口可以调,发送内容自己提供
这段代码本身只是利用java访问了一个url:(",后面带了一堆的参数:String data = "user_id=" + user_id + "password=" + password +
"mobile_phone=" + mobile_phone +
"msg=" + URLEncoder.encode(msg, "GBK") + "send_date=" + send_date +
"subcode=" + subcode;
,仅此而已,至于具体要怎么发,那就要看这个URL提供放的后台是如何定义参数的含义了。
那你要做的就是向这个URL发送数据就好,给个例子:
public class Httptest1 {
8
9 public static void main(String[] args) {
10
11 URL url = null ;
12 HttpURLConnection conn = null ;
13 OutputStream outStream = null ;
14 InputStream inStream = null ;
15
16 try {
17 url = new URL( " " );
18 conn = (HttpURLConnection) url.openConnection();
19 conn.setDoOutput( true );
20 conn.setDoInput( true );
21
22 String sendXml = " ?xml version=\ " 1.0 \ " encoding=\ " UTF - 16 \ " ?!DOCTYPE SigMailCommand SYSTEM \ " SigMail.dtd\ " " ; // XML数据
23 sendXml += " SigMailCommand Name=\ " checkuser\ " Param Name=\ " domainname\ " test.com.cn/Param " ;
24 sendXml += " Param Name=\ " username\ " admin/Param/SigMailCommand " ;
25
26 outStream = conn.getOutputStream();
27
28 // 准备通过CONN对象写入XML数据
29 BufferedWriter bw = new BufferedWriter( new java.io.OutputStreamWriter(outStream,
30 " UTF-16 " ));
31 bw.write(sendXml);
32 bw.flush();
33 bw.close();
34
35 // DataOutputStream dataOutStream = new DataOutputStream(outStream);
36 // dataOutStream.writeChars(xml);
37 // dataOutStream.flush();
38 // dataOutStream.close();
39
40
41 // 准备通过CONN对象读取返回的XML数据
42
43 inStream = conn.getInputStream();
44 StringBuffer returnXml = new StringBuffer( "" );
45 BufferedReader rd = new BufferedReader( new InputStreamReader(inStream,
46 " UTF-16 " ));
47 for (String line = null ; (line = rd.readLine()) != null ;) {
48 returnXml.append(line);
49 }
50 System.out.println(returnXml.toString());
51 rd.close();
52 } catch (IOException ex) {
53 ex.printStackTrace();
54 } finally {
55 try {
56 if (outStream != null )
57 outStream.close();
58 if (inStream != null )
59 inStream.close();
60 if (conn != null )
61 conn.disconnect();
62 } catch (IOException e) {
63 // TODO 自动生成 catch 块
64 e.printStackTrace();
65 }
66 }
67
68 }
69
70 }
方法一:
1.web.xml中配置listener
listener
listener-class
cn.com.jxlife.shs.web.action.csinquiry.surrender.MyListener
/listener-class
/listener
2.创建listener
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
private Timer timer = null;
@Override
public void contextInitialized(ServletContextEvent sce) {
timer = new Timer(true);
//设置任务计划,启动和间隔时间
timer.schedule(new MyTask(), 0, 3*60*1000);
//3分钟
//timer.schedule(new MyTask(), 0, 3*60*1000);
//在1秒后执行此任务,每次间隔2秒
//timer.schedule(new MyTask(), 1000, 2000);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
timer.cancel();
}
}
3.创建启动的类MyTask
import java.util.Date;
import java.util.TimerTask;
public class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("call at " + (new Date()));
}
}
方法二:
spring 下实现定时工作的调度框架quartz
· 1。从下载quartz的开源包
quartz-1.7.3.tar.gz
3.07M
· 2。从quartz-1.7.3.tar.gz 找到quartz-1.7.3.jar,放到项目的classpath下
(放到webroot/WEB-INF/lib)下
· 3。定义一个需要时间调度的程序
package com.machome.quartz;
public class CustomJob1 {
public void onTimeWork() {
System.out.println("数据备份任务启动");
System.out.println("...");
System.out.println("数据备份任务完成");
}
}
· 4。剩余的工作就都在spring xml文件里完成了
!-- 被调度的bean --
bean id="job1" class="com.machome.quartz.CustomJob1"/bean
!-- 定义任务对象 --
bean id="jobtask1"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
!-- 被调度的bean --
property name="targetObject"
ref bean="job1" /
/property
!-- 被调度的bean中的被调度的方法 --
property name="targetMethod"
valueonTimeWork/value
/property
/bean
!-- 定义触发对象 --
bean id="trigger1"
class="org.springframework.scheduling.quartz.CronTriggerBean"
!-- 任务对象 --
property name="jobDetail"
ref bean="jobtask1" /
/property
!-- cron表达式 --
property name="cronExpression"
value10,15,20,25,30,35,40,45,50,55 * * * * ?/value
/property
/bean
!-- 调度对象 --
!-- 如果将lazy-init='false'那么容器启动就会执行调度程序 --
bean id="scheduler" lazy-init="false"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
!-- 触发对象 --
property name="triggers"
list
ref bean="trigger1" /
/list
/property
/bean
· 5。 将spring 项目部署到TOMCAT或JBOSS服务器上
deploy项目
重启TOMCAT server
· 6。看结果:
cron表达式参考:Quartz的cron表达式