增加线程异步发送消息的方法二(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);
    }
}

  

转载于:https://www.cnblogs.com/z360519549/p/5712816.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值