增加线程异步发送消息的方法二(Runnable)

//获取当前时间:毫秒
long a = System.currentTimeMillis();
System.out.println("a :" +  a);
 
try
{
    //更改订单状态
    shopService.updateOrder(order); 
     
    //如果订单状态从 支付成功 改成 已发货,则给用户发送一条微信消息。
    if (Integer.parseInt(stats) == 3){
     //异步发送微信消息
        MessageImpl messageImpl = new MessageImpl(openId, tranNum, logisticcompany, logisticnum);
        messageImpl.run();
       //同步发送微信消息 
    //message.sendWxMessage(openId, tranNum, logisticcompany,logisticnum); } } catch (Exception e) { e.printStackTrace(); } //计算 try 内语句的执行时间 long b = System.currentTimeMillis(); System.out.println("b :" + b); System.out.println(" b - a :" + (b - a));

  

package com.starcloud.helpapp.wxMessage.service.impl;

import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

import com..helpapp.common.Loger;
import com..helpapp.common.utils.Tools;
import com..helpapp.wxMessage.ConnectionUrlUtil;
import com..helpapp.wxMessage.beans.MsgTemplateBean;
import com..helpapp.wxMessage.common.Constant;
import com..helpapp.wxMessage.service.Message;



public class MessageImpl implements Runnable
{
	
    /**
     * 
     * [简要描述]:推送微信消息
     * [详细描述]:    1:订单状态改成已发货
     *
     * @param parameter
     */
    String openId;
    String tranNum;
    String logisticcompany;
    String logisticnum;

    public void MessageImpl(String openId,String tranNum,String logisticcompany,String logisticnum)
    {
	    this.openId = openId;
            this.tranNum = tranNum;
            this.logisticcompany = logisticcompany;
            this.logisticnum = logisticnum;

    }



    public void sendWxMessage(String openId,String tranNum,String logisticcompany,String logisticnum)
    {

;
            JSONObject parameters = new JSONObject();
	    MsgTemplateBean bean = MsgTemplateBean.getMsgTemplageBean(tranNum,logisticcompany,logisticnum);
	    bean.setTouser(openId);
	    parameters = JSONObject.fromObject(bean);
	    System.out.println("parameters: " + parameters.toString());
//        else
//        {
//            
//            MsgforDistributeBean bean = MsgforDistributeBean.getMsgTemplageBean(payfee,phoneNum,proName);
//            bean.setTouser(openId);
//            parameters = JSONObject.fromObject(bean);
//        }
        Loger.logtxt("WxMessage", "发送消息"+parameters);
        String access_token = getAccess_token();
        if(null != access_token)
        {
            String returnstr = ConnectionUrlUtil.sendPost(Constant.SEND_MESSAGEURL+"?access_token="+access_token,parameters.toString());
            try
            {
                JSONObject returnJson = JSONObject.fromObject(returnstr);
                
                if(0 == returnJson.getInt("errcode"))
                {
                    Loger.logtxt("WxMessage", "发送消息成功"+parameters);
                }
                else
                {
                    Loger.logtxt("WxMessage", "发送消息失败"+parameters);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
         
        }
        else
        {
            //发送失败,获取token失败
            Loger.logtxt("WxMessage", "发送消息失败,原因获取access_token失败  ");
        }
       
    }
    /**
     * 
     * [简要描述]:获取到access_token
     * [详细描述]:    
     *
     * @return
     */
    public String getAccess_token()
    {
        String access_token = "";
        String access_token_json = ConnectionUrlUtil.sendPost(Constant.TOKEN_URL, "grant_type=client_credential&appid=" + Constant.APP_ID + "&secret="
                        + Constant.APP_SECRET);
        if(Tools.isNotEmty(access_token_json))
        {
            JSONObject tokenJson = JSONObject.fromObject(access_token_json);
            if(null != tokenJson && null!=tokenJson.get("access_token"))
            {
                //ACCESS_TOKEN
                access_token = tokenJson.getString("access_token");
            }
        }
        return access_token;
    }
    @override 
    public void run(){
	sendWxMessage(openId,tranNum, logisticcompany, logisticnum);
    }
}

  

posted @ 2016-07-27 22:08  zhoudingzhao  阅读(304)  评论(0编辑  收藏  举报