|
|
@ -1,12 +1,13 @@
|
|
|
|
package com.foreverwin.mesnac.common.util;
|
|
|
|
package com.foreverwin.mesnac.common.util;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
|
|
|
import com.foreverwin.modular.core.exception.BaseException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
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.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
|
|
|
|
import org.springframework.jms.core.JmsMessagingTemplate;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.jms.*;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @Description TODO
|
|
|
|
* @Description TODO
|
|
|
@ -16,48 +17,27 @@ import javax.jms.*;
|
|
|
|
@Component
|
|
|
|
@Component
|
|
|
|
@ConditionalOnProperty(prefix = "activeMq", value = {"enabled"}, matchIfMissing = true)
|
|
|
|
@ConditionalOnProperty(prefix = "activeMq", value = {"enabled"}, matchIfMissing = true)
|
|
|
|
public class ActiveMQUtil {
|
|
|
|
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 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{
|
|
|
|
try{
|
|
|
|
if (producer != null) {
|
|
|
|
jsonObject.put("TO_USER", user);
|
|
|
|
producer.close();
|
|
|
|
jsonObject.put("CONTENT",text);
|
|
|
|
}
|
|
|
|
defaultJmsTemplate.getJmsTemplate().setReceiveTimeout(TimeUnit.SECONDS.toMillis(3));
|
|
|
|
if (session != null) {
|
|
|
|
String message = defaultJmsTemplate.convertSendAndReceive(queue, jsonObject.toString(), String.class);
|
|
|
|
session.close();
|
|
|
|
if (StringUtil.isBlank(message)) {
|
|
|
|
}
|
|
|
|
//记日志
|
|
|
|
if (connection != null) {
|
|
|
|
|
|
|
|
connection.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}catch (Exception ignored){
|
|
|
|
|
|
|
|
throw new BaseException("消息发送失败");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|