Leon 3 years ago
commit 57b74eddbd

@ -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,48 +17,27 @@ 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");
try{
jsonObject.put("TO_USER", user);
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();
}
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
}catch (Exception e){
e.printStackTrace();
defaultJmsTemplate.getJmsTemplate().setReceiveTimeout(TimeUnit.SECONDS.toMillis(3));
String message = defaultJmsTemplate.convertSendAndReceive(queue, jsonObject.toString(), String.class);
if (StringUtil.isBlank(message)) {
//记日志
}
}catch (Exception ignored){
throw new BaseException("消息发送失败");
}
}
}

Loading…
Cancel
Save