71 lines
2.1 KiB
Java
71 lines
2.1 KiB
Java
|
package com.xkrs.common.config;
|
|||
|
|
|||
|
/**
|
|||
|
* @Author: XinYi Song
|
|||
|
* @Date: 2022/2/8 14:16
|
|||
|
*/
|
|||
|
import org.apache.activemq.ActiveMQConnectionFactory;
|
|||
|
import org.apache.activemq.command.ActiveMQQueue;
|
|||
|
import org.apache.activemq.command.ActiveMQTopic;
|
|||
|
import org.springframework.beans.factory.annotation.Value;
|
|||
|
import org.springframework.context.annotation.Bean;
|
|||
|
import org.springframework.context.annotation.Configuration;
|
|||
|
import org.springframework.jms.config.JmsListenerContainerFactory;
|
|||
|
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
|
|||
|
import org.springframework.jms.core.JmsMessagingTemplate;
|
|||
|
import javax.jms.ConnectionFactory;
|
|||
|
import javax.jms.Queue;
|
|||
|
import javax.jms.Topic;
|
|||
|
|
|||
|
@Configuration
|
|||
|
public class BeanConfig {
|
|||
|
|
|||
|
@Value("${spring.activemq.broker-url}")
|
|||
|
private String brokerUrl;
|
|||
|
|
|||
|
@Value("${spring.activemq.user}")
|
|||
|
private String username;
|
|||
|
|
|||
|
@Value("${spring.activemq.topic-name}")
|
|||
|
private String password;
|
|||
|
|
|||
|
@Value("${spring.activemq.topic-name}")
|
|||
|
private String topicName;
|
|||
|
|
|||
|
@Value("${spring.activemq.topic-name1}")
|
|||
|
private String topicName1;
|
|||
|
|
|||
|
@Bean(name = "topic")
|
|||
|
public Topic topic() {
|
|||
|
return new ActiveMQTopic(topicName);
|
|||
|
}
|
|||
|
|
|||
|
@Bean(name = "topic1")
|
|||
|
public Topic topic1() {
|
|||
|
return new ActiveMQTopic(topicName1);
|
|||
|
}
|
|||
|
|
|||
|
@Bean
|
|||
|
public ConnectionFactory connectionFactory(){
|
|||
|
return new ActiveMQConnectionFactory(username, password, brokerUrl);
|
|||
|
}
|
|||
|
|
|||
|
@Bean
|
|||
|
public JmsMessagingTemplate jmsMessageTemplate(){
|
|||
|
return new JmsMessagingTemplate(connectionFactory());
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* 在Topic模式中,对消息的监听需要对containerFactory进行配置
|
|||
|
* @param connectionFactory
|
|||
|
* @return
|
|||
|
*/
|
|||
|
@Bean("topicListener")
|
|||
|
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
|
|||
|
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
|
|||
|
factory.setConnectionFactory(connectionFactory);
|
|||
|
factory.setPubSubDomain(true);
|
|||
|
return factory;
|
|||
|
}
|
|||
|
}
|