diff --git a/common/src/main/java/com/foreverwin/mesnac/common/util/ActiveMQUtil.java b/common/src/main/java/com/foreverwin/mesnac/common/util/ActiveMQUtil.java index b351f462..67041ba3 100644 --- a/common/src/main/java/com/foreverwin/mesnac/common/util/ActiveMQUtil.java +++ b/common/src/main/java/com/foreverwin/mesnac/common/util/ActiveMQUtil.java @@ -1,12 +1,13 @@ package com.foreverwin.mesnac.common.util; -import org.apache.activemq.ActiveMQConnectionFactory; +import com.foreverwin.modular.core.exception.BaseException; import org.json.JSONObject; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Component; -import javax.jms.*; +import java.util.concurrent.TimeUnit; /** * @Description TODO @@ -16,47 +17,26 @@ import javax.jms.*; @Component @ConditionalOnProperty(prefix = "activeMq", value = {"enabled"}, matchIfMissing = true) public class ActiveMQUtil { - @Value("${activeMq.sendWeChatMessage}") - private String weChatUrl; - @Value("${activeMq.queue}") - private String queue; - public void wechatSendMessage(String user, String text) { - ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(weChatUrl); - Connection connection = null; - Session session = null; - MessageProducer producer = null; - try{ - connection = factory.createConnection(); - connection.start(); - session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - Destination destination = session.createQueue(this.queue); - producer = session.createProducer(destination); + @Autowired + private JmsMessagingTemplate defaultJmsTemplate; + + public void wechatSendMessage(String user, String text) { + String queue = "send.weChat.notice"; JSONObject jsonObject = new JSONObject(); - //jsonObject.put("TO_USER", user); - jsonObject.put("TO_USER", "@all"); - jsonObject.put("CONTENT",text); - String sendMessage = jsonObject.toString(); - TextMessage textMessage = session.createTextMessage(sendMessage); - producer.send(textMessage); - }catch (Exception e){ - // throw new BaseException("发送消息失败"+e.getMessage()); - }finally { - try { - if (producer != null) { - producer.close(); + try{ + jsonObject.put("TO_USER", user); + jsonObject.put("CONTENT",text); + defaultJmsTemplate.getJmsTemplate().setReceiveTimeout(TimeUnit.SECONDS.toMillis(3)); + String message = defaultJmsTemplate.convertSendAndReceive(queue, jsonObject.toString(), String.class); + if (StringUtil.isBlank(message)) { + //记日志 } - if (session != null) { - session.close(); - } - if (connection != null) { - connection.close(); - } - }catch (Exception e){ - e.printStackTrace(); + }catch (Exception ignored){ + throw new BaseException("消息发送失败"); } - } + }