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