|
@ -1,82 +1,82 @@
|
|
|
package com.yihu.jw.basic.config;
|
|
|
|
|
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
import org.springframework.jms.annotation.EnableJms;
|
|
|
import org.springframework.jms.config.JmsListenerContainerFactory;
|
|
|
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
|
|
|
import org.springframework.jms.connection.CachingConnectionFactory;
|
|
|
import org.springframework.jms.core.JmsMessagingTemplate;
|
|
|
import org.springframework.jms.core.JmsTemplate;
|
|
|
|
|
|
import javax.jms.ConnectionFactory;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2017/9/9.
|
|
|
* 生产者配置
|
|
|
*/
|
|
|
@EnableJms
|
|
|
@Configuration
|
|
|
public class ActiveMQConfig {
|
|
|
@Value("${activemq.username}")
|
|
|
private String username;
|
|
|
@Value("${activemq.password}")
|
|
|
private String password;
|
|
|
@Value("${activemq.url}")
|
|
|
private String url;
|
|
|
|
|
|
@Bean
|
|
|
public ActiveMQConnectionFactory activeMQConnectionFactory() {
|
|
|
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(username, password, url);
|
|
|
//设置异步发送
|
|
|
activeMQConnectionFactory.setUseAsyncSend(true);
|
|
|
return activeMQConnectionFactory;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 缓存session链接
|
|
|
*/
|
|
|
@Bean
|
|
|
@Primary
|
|
|
public CachingConnectionFactory CachingConnectionFactory() {
|
|
|
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
|
|
|
//目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory
|
|
|
cachingConnectionFactory.setTargetConnectionFactory(activeMQConnectionFactory());
|
|
|
//Session缓存数量,这里属性也可以直接在这里配置
|
|
|
cachingConnectionFactory.setSessionCacheSize(100);
|
|
|
return cachingConnectionFactory;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public JmsTemplate jmsTemplate() {
|
|
|
JmsTemplate jmsTemplate = new JmsTemplate();
|
|
|
jmsTemplate.setConnectionFactory( CachingConnectionFactory());
|
|
|
return jmsTemplate;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public JmsMessagingTemplate jmsMessageTemplate(){
|
|
|
return new JmsMessagingTemplate(CachingConnectionFactory());
|
|
|
}
|
|
|
|
|
|
// 在Queue模式中,对消息的监听需要对containerFactory进行配置
|
|
|
@Bean("queueListener")
|
|
|
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
|
|
|
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
|
|
|
factory.setConnectionFactory(connectionFactory);
|
|
|
factory.setPubSubDomain(false);
|
|
|
return factory;
|
|
|
}
|
|
|
|
|
|
//在Topic模式中,对消息的监听需要对containerFactory进行配置
|
|
|
@Bean("topicListener")
|
|
|
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
|
|
|
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
|
|
|
factory.setConnectionFactory(connectionFactory);
|
|
|
factory.setPubSubDomain(true);
|
|
|
return factory;
|
|
|
}
|
|
|
}
|
|
|
//package com.yihu.jw.basic.config;
|
|
|
//
|
|
|
//import org.apache.activemq.ActiveMQConnectionFactory;
|
|
|
//import org.springframework.beans.factory.annotation.Value;
|
|
|
//import org.springframework.context.annotation.Bean;
|
|
|
//import org.springframework.context.annotation.Configuration;
|
|
|
//import org.springframework.context.annotation.Primary;
|
|
|
//import org.springframework.jms.annotation.EnableJms;
|
|
|
//import org.springframework.jms.config.JmsListenerContainerFactory;
|
|
|
//import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
|
|
|
//import org.springframework.jms.connection.CachingConnectionFactory;
|
|
|
//import org.springframework.jms.core.JmsMessagingTemplate;
|
|
|
//import org.springframework.jms.core.JmsTemplate;
|
|
|
//
|
|
|
//import javax.jms.ConnectionFactory;
|
|
|
//
|
|
|
///**
|
|
|
// * Created by chenweida on 2017/9/9.
|
|
|
// * 生产者配置
|
|
|
// */
|
|
|
//@EnableJms
|
|
|
//@Configuration
|
|
|
//public class ActiveMQConfig {
|
|
|
// @Value("${activemq.username}")
|
|
|
// private String username;
|
|
|
// @Value("${activemq.password}")
|
|
|
// private String password;
|
|
|
// @Value("${activemq.url}")
|
|
|
// private String url;
|
|
|
//
|
|
|
// @Bean
|
|
|
// public ActiveMQConnectionFactory activeMQConnectionFactory() {
|
|
|
// ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(username, password, url);
|
|
|
// //设置异步发送
|
|
|
// activeMQConnectionFactory.setUseAsyncSend(true);
|
|
|
// return activeMQConnectionFactory;
|
|
|
// }
|
|
|
//
|
|
|
// /**
|
|
|
// * 缓存session链接
|
|
|
// */
|
|
|
// @Bean
|
|
|
// @Primary
|
|
|
// public CachingConnectionFactory CachingConnectionFactory() {
|
|
|
// CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
|
|
|
// //目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory
|
|
|
// cachingConnectionFactory.setTargetConnectionFactory(activeMQConnectionFactory());
|
|
|
// //Session缓存数量,这里属性也可以直接在这里配置
|
|
|
// cachingConnectionFactory.setSessionCacheSize(100);
|
|
|
// return cachingConnectionFactory;
|
|
|
// }
|
|
|
//
|
|
|
// @Bean
|
|
|
// public JmsTemplate jmsTemplate() {
|
|
|
// JmsTemplate jmsTemplate = new JmsTemplate();
|
|
|
// jmsTemplate.setConnectionFactory( CachingConnectionFactory());
|
|
|
// return jmsTemplate;
|
|
|
// }
|
|
|
//
|
|
|
// @Bean
|
|
|
// public JmsMessagingTemplate jmsMessageTemplate(){
|
|
|
// return new JmsMessagingTemplate(CachingConnectionFactory());
|
|
|
// }
|
|
|
//
|
|
|
// // 在Queue模式中,对消息的监听需要对containerFactory进行配置
|
|
|
// @Bean("queueListener")
|
|
|
// public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
|
|
|
// SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
|
|
|
// factory.setConnectionFactory(connectionFactory);
|
|
|
// factory.setPubSubDomain(false);
|
|
|
// return factory;
|
|
|
// }
|
|
|
//
|
|
|
// //在Topic模式中,对消息的监听需要对containerFactory进行配置
|
|
|
// @Bean("topicListener")
|
|
|
// public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
|
|
|
// SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
|
|
|
// factory.setConnectionFactory(connectionFactory);
|
|
|
// factory.setPubSubDomain(true);
|
|
|
// return factory;
|
|
|
// }
|
|
|
//}
|