程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> 增加線程異步發送消息的方法一(Tread),發送消息tread

增加線程異步發送消息的方法一(Tread),發送消息tread

編輯:JAVA綜合教程

增加線程異步發送消息的方法一(Tread),發送消息tread


	@RequestMapping(value="order/updateOrder.do")
	public String  updateOrder(HttpServletRequest request,HttpServletResponse response){
	    final String ordId =request.getParameter("ordId");
	    String proDesc =request.getParameter("proDesc");
	    final String logisticnum =request.getParameter("logisticnum");
	    final String logisticcompany =request.getParameter("logisticcompany");
	    String stats =request.getParameter("stats");
	    final String tranNum =request.getParameter("tranNum");
	    final String openId =request.getParameter("openId");
	    
	    System.out.println("訂單狀態stats:" + stats);
	    System.out.println("tranNum:" + tranNum);
	    System.out.println("openId:" + openId);
	    System.out.println("logisticnum:" + logisticnum);
	    System.out.println("logisticcompany:" + logisticcompany);
	    
	    
	    GoodsOrder order = new GoodsOrder();
	    order.setOrdId(ordId);
	    order.setProDesc(proDesc);
	    order.setLogisticnum(logisticnum);
	    order.setLogisticcompany(logisticcompany);
	    order.setrStatus(stats);
	    
	    //獲取當前時間:毫秒
	    long a = System.currentTimeMillis();
	    System.out.println("a :" +  a);
	    
	    try
	    {
            //更改訂單狀態
	        shopService.updateOrder(order);  
	        
	        //如果訂單狀態從 支付成功 改成 已發貨,則給用戶發送一條微信消息。
	        if (Integer.parseInt(stats) == 3)
	        {
	        	//異步發送微信消息
	        	new Thread()
	        	{
	        		public void run()
	        		{
	        			message.sendWxMessage(openId, tranNum, logisticcompany,logisticnum);
	        		}
	        	}.start();
	        	//同步發送微信消息
	        	//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));
	return "redirect:/order/orderlist.do?currPage=1";
	}
	

  

 

package com.starcloud.helpapp.wxMessage.service;

/**
 * 
 * 項目名稱:eduappweb    
 * 類名稱:
 * 類描述: 
 * 創建人:Administrator    
 * 創建時間:2016-5-28 下午02:53:01    
 * @version 1.0
 */
public interface Message
{
  
  
    /**
     * 
     * [簡要描述]:推送微信消息
     * [詳細描述]:    
     *
     * @param openId
     * @param proName
     * @param payfee
     * @param phoneNum
     * @param flag   1:購買成功提示消息 2:代言返利消息
     */
    public void sendWxMessage(String openId,String tranNum,String logisticcompany,String logisticnum);
}

  

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

import org.springframework.stereotype.Service;

import net.sf.json.JSONObject;

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


@Service("Message")
public class MessageImpl implements Message
{
	
    /**
     * 
     * [簡要描述]:推送微信消息
     * [詳細描述]:    1:訂單狀態改成已發貨
     *
     * @param parameter
     */
    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
//        {
//            //proName返利訂單
//            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 = "";//公司的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;
    }
}

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved