Spring+Quartz实现定时任务

MessageMgr.java

package com.uyao.bid.common.message;



import com.pominfo.framework.exception.PomInfoException;



public interface MessageMgr {

    /**

     * 发送短消息

     * @param message

     * @return

     * @throws PomInfoException

     */

    public String sendMessage(Message message )  throws PomInfoException;

}

MessageMgrImpl.java

package com.uyao.bid.common.message;



import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLEncoder;



import com.uyao.bid.common.constant.SysCodeConstant;



public class MessageMgrImpl implements MessageMgr {

    /**

     * 发送短消息

     */

    public String sendMessage(Message message)  {

        String result="";

        try {

            StringBuffer sb = new StringBuffer(SysCodeConstant.SEND_MESSAGE_URL);

            sb.append("username=").append(message.getUserName());

            sb.append("&password=").append(message.getPassWord());

            sb.append("&receiver=").append(message.getReceiver());

            sb.append("&content="+URLEncoder.encode(message.getContent()));

            

            URL url = new URL(sb.toString());

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            connection.setRequestMethod(message.getRequestMethod());

            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

            String inputline = in.readLine();

            result =inputline;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result;

    }

}

MessageAction.java

package com.uyao.bid.common.message;



import java.util.ArrayList;

import java.util.Date;

import java.util.List;



import com.pominfo.framework.struts.PomInfoAction;

import com.uyao.bid.common.constant.SysCodeConstant;

import com.uyao.bid.common.util.StringUtil;

import com.uyao.bid.jk.biz.RemindDetailDrugMgr;

import com.uyao.bid.jk.biz.RemindDetailMgr;

import com.uyao.bid.jk.model.RemindDetail;

import com.uyao.bid.jk.model.RemindDetailDrug;



public class MessageAction extends PomInfoAction {

    private RemindDetailMgr remindDetailMgr;

    private RemindDetailDrugMgr remindDetailDrugMgr;

    public RemindDetailDrugMgr getRemindDetailDrugMgr() {

        return remindDetailDrugMgr;

    }

    public void setRemindDetailDrugMgr(RemindDetailDrugMgr remindDetailDrugMgr) {

        this.remindDetailDrugMgr = remindDetailDrugMgr;

    }

    public RemindDetailMgr getRemindDetailMgr() {

        return remindDetailMgr;

    }

    public void setRemindDetailMgr(RemindDetailMgr remindDetailMgr) {

        this.remindDetailMgr = remindDetailMgr;

    }

    

    /**

     * 用药提醒自动短信发送

     * @return

     */

    public void sendRemindMessage(){

        try {

            MessageMgr messageMgr = new MessageMgrImpl();

            List<RemindDetail> remindDetails =new ArrayList<RemindDetail>();

            remindDetails = remindDetailMgr.getRemindDetailMessage(null);

            for(RemindDetail rd:remindDetails){

                if(rd.getRemind().getPhoneNum()!=null && !rd.getRemind().getPhoneNum().isEmpty()){

                    String content =SysCodeConstant.MessageModel.REMIND_CONTENT_TEMPLATES;//消息内容模板

                    List<RemindDetailDrug> remindDetailDrugs =new ArrayList<RemindDetailDrug>();

                    RemindDetailDrug remindDetailDrug =new RemindDetailDrug();

                    remindDetailDrug.setRemindDetail(rd);

                    remindDetailDrugs = remindDetailDrugMgr.getRemindDetailDrugList(remindDetailDrug);

                    StringBuffer sb =new StringBuffer("");

                    for(RemindDetailDrug rdd:remindDetailDrugs){

                        sb.append(rdd.getDrug().getDrugName()).append("、");

                    }

                    content = content.replace("@drugs",sb.substring(0, sb.length() - 1));

                    Message message =new Message();

                    message.setUserName(SysCodeConstant.MessageModel.USERNAME);

                    message.setPassWord(SysCodeConstant.MessageModel.PASSWORD);

                    message.setReceiver(rd.getRemind().getPhoneNum());

                    message.setContent(content);

                    message.setRequestMethod("POST");

                    messageMgr.sendMessage(message);

                }

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

spring-task.xml

<?xml version="1.0" encoding="UTF-8"?>  

<beans  

    xmlns="http://www.springframework.org/schema/beans"  

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  

  

      <bean id="messageJob" class="com.uyao.bid.common.message.MessageAction">

        <property name="remindDetailMgr">

            <ref bean="remindDetailMgr" />

        </property>

        <property name="remindDetailDrugMgr">

            <ref bean="remindDetailDrugMgr"/>

        </property>

      </bean>  

      

    <!-- 定时器执行 -->

    <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  

        <property name="triggers">  

            <list>

                <ref bean="autoSendSmlogTriggers" />

            </list>  

        </property>  

    </bean>  

    <!-- 定时器时间与方法的设定  -->  

    <bean id="autoSendSmlogTriggers" class="org.springframework.scheduling.quartz.CronTriggerBean">  

        <property name="jobDetail"> 

            <ref bean="sendRemindMessage" /> <!-- 每分钟时发送一次 -->

        </property>  

        <property name="cronExpression"> 

            <!-- 秒 分 小时 ? 日期  月份 --> 

             <value>0 0/1 * *  * ?</value> 

        </property>  

    </bean>   

    

    <!-- 设定执行代码中的具休方法 --> 

    <bean id="sendRemindMessage" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  

        <property name="targetObject">  

            <ref bean="messageJob" />  

        </property>  

        <property name="targetMethod">  

            <value>sendRemindMessage</value>  

        </property>  

    </bean> 

</beans> 

 

 

你可能感兴趣的:(spring)