|
@ -1,90 +1,90 @@
|
|
|
package com.yihu.wlyy.activemq;
|
|
|
|
|
|
import com.yihu.wlyy.util.SpringUtil;
|
|
|
import org.apache.activemq.ActiveMQSession;
|
|
|
import org.apache.activemq.command.ActiveMQQueue;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.jms.connection.CachingConnectionFactory;
|
|
|
import org.springframework.jms.listener.DefaultMessageListenerContainer;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.jms.MessageListener;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2017/9/9.
|
|
|
*/
|
|
|
@Component
|
|
|
public class StartListener {
|
|
|
private static Map<String, DefaultMessageListenerContainer> holder = new HashMap<String, DefaultMessageListenerContainer>();
|
|
|
private Logger logger = LoggerFactory.getLogger(StartListener.class);
|
|
|
|
|
|
@Value("${activemq.queue.healtHarticleQueue}")
|
|
|
private String healtHarticleQueue;
|
|
|
@Value("${activemq.consumers.count}")
|
|
|
private Integer count;
|
|
|
@Autowired
|
|
|
private HealthArtListener healthArtListener;
|
|
|
@Autowired
|
|
|
private CachingConnectionFactory cachingConnectionFactory;
|
|
|
|
|
|
@PostConstruct
|
|
|
public void startListener() {
|
|
|
startHealthArticQueueListener();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 启动健康教育消费
|
|
|
*/
|
|
|
private void startHealthArticQueueListener() {
|
|
|
try {
|
|
|
//启动监听
|
|
|
startReceiverByQueueName(healthArtListener, healtHarticleQueue);
|
|
|
logger.info("start HealthArtListener success");
|
|
|
} catch (Exception e) {
|
|
|
logger.error("start HealthArtListener error");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 启动
|
|
|
*
|
|
|
* @param receiver
|
|
|
* @param queueName
|
|
|
*/
|
|
|
private void startReceiverByQueueName(MessageListener receiver, String queueName) {
|
|
|
if (holder.get(queueName) != null) {
|
|
|
return;
|
|
|
}
|
|
|
ActiveMQQueue destination = new ActiveMQQueue(queueName);
|
|
|
|
|
|
DefaultMessageListenerContainer listenerContainer = new DefaultMessageListenerContainer();
|
|
|
// 监听容器属性的配置
|
|
|
listenerContainer.setConnectionFactory(cachingConnectionFactory);
|
|
|
// 设置目的地
|
|
|
listenerContainer.setDestination(destination);
|
|
|
// 设置监听器
|
|
|
listenerContainer.setMessageListener(receiver);
|
|
|
// 设置消费者集群数
|
|
|
int consumers = count;
|
|
|
listenerContainer.setConcurrentConsumers(2);
|
|
|
listenerContainer.setMaxConcurrentConsumers(consumers);
|
|
|
// 设置监听队列还是主题 默认是队列
|
|
|
listenerContainer.setPubSubNoLocal(false);
|
|
|
// listenerContainer.setAcceptMessagesWhileStopping(true);
|
|
|
// 设置应答模式 默认是4
|
|
|
listenerContainer.setSessionAcknowledgeMode(ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
|
|
|
// 设置是否启动事物 默认不开启
|
|
|
listenerContainer.setSessionTransacted(false);
|
|
|
// 将监听容器保存在holder中
|
|
|
holder.put(queueName, listenerContainer);
|
|
|
listenerContainer.initialize();
|
|
|
// 启动监听
|
|
|
listenerContainer.start();
|
|
|
}
|
|
|
}
|
|
|
//package com.yihu.wlyy.activemq;
|
|
|
//
|
|
|
//import com.yihu.wlyy.util.SpringUtil;
|
|
|
//import org.apache.activemq.ActiveMQSession;
|
|
|
//import org.apache.activemq.command.ActiveMQQueue;
|
|
|
//import org.slf4j.Logger;
|
|
|
//import org.slf4j.LoggerFactory;
|
|
|
//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
//import org.springframework.beans.factory.annotation.Value;
|
|
|
//import org.springframework.jms.connection.CachingConnectionFactory;
|
|
|
//import org.springframework.jms.listener.DefaultMessageListenerContainer;
|
|
|
//import org.springframework.stereotype.Component;
|
|
|
//
|
|
|
//import javax.annotation.PostConstruct;
|
|
|
//import javax.jms.MessageListener;
|
|
|
//import java.util.HashMap;
|
|
|
//import java.util.Map;
|
|
|
//
|
|
|
///**
|
|
|
// * Created by chenweida on 2017/9/9.
|
|
|
// */
|
|
|
//@Component
|
|
|
//public class StartListener {
|
|
|
// private static Map<String, DefaultMessageListenerContainer> holder = new HashMap<String, DefaultMessageListenerContainer>();
|
|
|
// private Logger logger = LoggerFactory.getLogger(StartListener.class);
|
|
|
//
|
|
|
// @Value("${activemq.queue.healtHarticleQueue}")
|
|
|
// private String healtHarticleQueue;
|
|
|
// @Value("${activemq.consumers.count}")
|
|
|
// private Integer count;
|
|
|
// @Autowired
|
|
|
// private HealthArtListener healthArtListener;
|
|
|
// @Autowired
|
|
|
// private CachingConnectionFactory cachingConnectionFactory;
|
|
|
//
|
|
|
// @PostConstruct
|
|
|
// public void startListener() {
|
|
|
// startHealthArticQueueListener();
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 启动健康教育消费
|
|
|
// */
|
|
|
// private void startHealthArticQueueListener() {
|
|
|
// try {
|
|
|
// //启动监听
|
|
|
// startReceiverByQueueName(healthArtListener, healtHarticleQueue);
|
|
|
// logger.info("start HealthArtListener success");
|
|
|
// } catch (Exception e) {
|
|
|
// logger.error("start HealthArtListener error");
|
|
|
// }
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 启动
|
|
|
// *
|
|
|
// * @param receiver
|
|
|
// * @param queueName
|
|
|
// */
|
|
|
// private void startReceiverByQueueName(MessageListener receiver, String queueName) {
|
|
|
// if (holder.get(queueName) != null) {
|
|
|
// return;
|
|
|
// }
|
|
|
// ActiveMQQueue destination = new ActiveMQQueue(queueName);
|
|
|
//
|
|
|
// DefaultMessageListenerContainer listenerContainer = new DefaultMessageListenerContainer();
|
|
|
// // 监听容器属性的配置
|
|
|
// listenerContainer.setConnectionFactory(cachingConnectionFactory);
|
|
|
// // 设置目的地
|
|
|
// listenerContainer.setDestination(destination);
|
|
|
// // 设置监听器
|
|
|
// listenerContainer.setMessageListener(receiver);
|
|
|
// // 设置消费者集群数
|
|
|
// int consumers = count;
|
|
|
// listenerContainer.setConcurrentConsumers(2);
|
|
|
// listenerContainer.setMaxConcurrentConsumers(consumers);
|
|
|
// // 设置监听队列还是主题 默认是队列
|
|
|
// listenerContainer.setPubSubNoLocal(false);
|
|
|
// // listenerContainer.setAcceptMessagesWhileStopping(true);
|
|
|
// // 设置应答模式 默认是4
|
|
|
// listenerContainer.setSessionAcknowledgeMode(ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);
|
|
|
// // 设置是否启动事物 默认不开启
|
|
|
// listenerContainer.setSessionTransacted(false);
|
|
|
// // 将监听容器保存在holder中
|
|
|
// holder.put(queueName, listenerContainer);
|
|
|
// listenerContainer.initialize();
|
|
|
// // 启动监听
|
|
|
// listenerContainer.start();
|
|
|
// }
|
|
|
//}
|