|
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
|
|
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+import org.zillion.util.log.LogUtil;
|
|
|
|
|
|
import javax.jms.*;
|
|
|
import java.util.HashMap;
|
|
@@ -16,8 +17,6 @@ import java.util.Map;
|
|
|
**/
|
|
|
public class MessageUtil {
|
|
|
|
|
|
- public static final String QUEUE_NAME = "collection.queue.20220114";
|
|
|
-
|
|
|
private static MessageProducer producer;
|
|
|
|
|
|
private static Session session;
|
|
@@ -28,7 +27,8 @@ public class MessageUtil {
|
|
|
}
|
|
|
|
|
|
if (StringUtils.isBlank(Constant.activeMqBrokerUrl)) {
|
|
|
- throw new Exception("读取不到activeMq的brokerUrl!");
|
|
|
+ LogUtil.error("读取不到activeMq的brokerUrl!");
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER, ActiveMQConnectionFactory.DEFAULT_PASSWORD
|
|
@@ -41,22 +41,36 @@ public class MessageUtil {
|
|
|
return session;
|
|
|
}
|
|
|
|
|
|
- public synchronized static MessageProducer getMessageProducer() throws Exception {
|
|
|
+ private static final long FIVE_MINUTES = 5 * 60 * 1000;
|
|
|
+
|
|
|
+ public synchronized static MessageProducer getMessageProducer(Session session) throws Exception {
|
|
|
+ if (session == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
if (producer != null) {
|
|
|
return producer;
|
|
|
}
|
|
|
-
|
|
|
- Session session = getSession();
|
|
|
//创建queue
|
|
|
- Queue queue = session.createQueue(QUEUE_NAME);//Destination
|
|
|
+ Queue queue = session.createQueue(Constant.queueName);//Destination
|
|
|
//消息生产者
|
|
|
producer = session.createProducer(queue);
|
|
|
+ producer.setTimeToLive(FIVE_MINUTES);
|
|
|
return producer;
|
|
|
}
|
|
|
|
|
|
public static void sendMessage(String message) throws Exception {
|
|
|
- MessageProducer messageProducer = getMessageProducer();
|
|
|
- TextMessage textMessage = getSession().createTextMessage(message);
|
|
|
+ Session session = getSession();
|
|
|
+ if (session == null) {
|
|
|
+ LogUtil.error("未连接到ActiveMq服务!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ MessageProducer messageProducer = getMessageProducer(session);
|
|
|
+ if (messageProducer == null) {
|
|
|
+ LogUtil.error("未连接到ActiveMq服务!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ TextMessage textMessage = session.createTextMessage(message);
|
|
|
messageProducer.send(textMessage);
|
|
|
}
|
|
|
|