一些常量定义:
代码语言:javascript复制/**
* @Auther: ZhangShenao
* @Date: 2019/1/6 16:26
* @Description:
*/
public interface Constants {
String BROKER_URL = "nio://localhost:61617";
String QUEUE_NAME = "Test-Queue";
String TOPIC_NAME = "Test-Topic";
String CONSUMER_CLIENT_ID = "Client-1";
String DURABLE_SUBSCRIBER_NAME = "Durable-Subscriber";
int MESSAGE_NUM = 5;
String JMS_TOPIC_TEMPLATE = "jmsTopicTemplate";
String JMS_QUEUE_TEMPLATE = "jmsQueueTemplate";
String QUEUE_LISTENER_CONTAINER_FACTORY = "queueListenerContainerFactory";
String TOPIC_LISTENER_CONTAINER_FACTORY = "topicListenerContainerFactory";
interface QueueNames{
String REQUEST_QUEUE = "Reply-Queue";
String RESPONSE_QUEUE = "Response-Queue";
}
}
ActiveMQ核心配置类:
代码语言:javascript复制/**
* @Auther: ZhangShenao
* @Date: 2019/1/8 18:20
* @Description:ActiveMQ核心配置类
*/
@Configuration
@EnableJms
@ComponentScan
public class ActiveMQConfiguration {
@ConditionalOnClass(ActiveMQConnectionFactory.class)
@Bean
public ConnectionFactory activeMQConnectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(Constants.BROKER_URL);
connectionFactory.setUserName("admin");
connectionFactory.setPassword("admin");
return connectionFactory;
}
@Bean
public ConnectionFactory connectionFactory(){
CachingConnectionFactory factory = new CachingConnectionFactory();
factory.setTargetConnectionFactory(activeMQConnectionFactory());
factory.setSessionCacheSize(100);
return factory;
}
@Bean
public MessageConverter jacksonJmsMessageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
@Bean(Constants.JMS_TOPIC_TEMPLATE)
public JmsTemplate jmsTopicTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(connectionFactory());
jmsTemplate.setPubSubDomain(true);
jmsTemplate.setMessageConverter(jacksonJmsMessageConverter());
return jmsTemplate;
}
@Bean(Constants.JMS_QUEUE_TEMPLATE)
public JmsTemplate jmsQueueTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(connectionFactory());
jmsTemplate.setPubSubDomain(false);
jmsTemplate.setMessageConverter(jacksonJmsMessageConverter());
return jmsTemplate;
}
@ConditionalOnClass(ActiveMQConnectionFactory.class)
@Bean(Constants.QUEUE_LISTENER_CONTAINER_FACTORY)
public JmsListenerContainerFactory<?> queueListenerContainerFactory(DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory());
factory.setMessageConverter(jacksonJmsMessageConverter());
factory.setPubSubDomain(false);
return factory;
}
@ConditionalOnClass(ActiveMQConnectionFactory.class)
@Bean(Constants.TOPIC_LISTENER_CONTAINER_FACTORY)
public JmsListenerContainerFactory<?> topicListenerContainerFactory(DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, connectionFactory());
factory.setMessageConverter(jacksonJmsMessageConverter());
factory.setPubSubDomain(true);
return factory;
}
}