|
| 1 | +package cn.iocoder.springboot.lab32.activemqdemo.config; |
| 2 | + |
| 3 | +import cn.iocoder.springboot.lab32.activemqdemo.message.BroadcastMessage; |
| 4 | +import cn.iocoder.springboot.lab32.activemqdemo.message.ClusteringMessage; |
| 5 | +import org.apache.activemq.command.ActiveMQQueue; |
| 6 | +import org.apache.activemq.command.ActiveMQTopic; |
| 7 | +import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer; |
| 8 | +import org.springframework.context.annotation.Bean; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.jms.config.DefaultJmsListenerContainerFactory; |
| 11 | +import org.springframework.jms.core.JmsMessagingTemplate; |
| 12 | +import org.springframework.jms.core.JmsTemplate; |
| 13 | + |
| 14 | +import javax.jms.ConnectionFactory; |
| 15 | +import javax.jms.Queue; |
| 16 | +import javax.jms.Topic; |
| 17 | + |
| 18 | +@Configuration |
| 19 | +public class RabbitConfig { |
| 20 | + |
| 21 | + public static final String CLUSTERING_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME = "clusteringJmsListenerContainerFactory"; |
| 22 | + public static final String BROADCAST_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME = "broadcastJmsListenerContainerFactory"; |
| 23 | + |
| 24 | + public static final String CLUSTERING_JMS_TEMPLATE_BEAN_NAME = "clusteringJmsTemplate"; |
| 25 | + public static final String BROADCAST_JMS_TEMPLATE_BEAN_NAME = "broadcastJmsTemplate"; |
| 26 | + |
| 27 | + // ========== 集群消费 ========== |
| 28 | + |
| 29 | + @Bean |
| 30 | + public Queue clusteringQueue() { |
| 31 | + return new ActiveMQQueue(ClusteringMessage.QUEUE); |
| 32 | + } |
| 33 | + |
| 34 | + @Bean(CLUSTERING_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME) |
| 35 | + public DefaultJmsListenerContainerFactory clusteringJmsListenerContainerFactory( |
| 36 | + DefaultJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { |
| 37 | + DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); |
| 38 | + configurer.configure(factory, connectionFactory); |
| 39 | + factory.setPubSubDomain(false); |
| 40 | + return factory; |
| 41 | + } |
| 42 | + |
| 43 | + @Bean(CLUSTERING_JMS_TEMPLATE_BEAN_NAME) |
| 44 | + public JmsMessagingTemplate clusteringJmsTemplate(ConnectionFactory connectionFactory) { |
| 45 | + // 创建 JmsTemplate 对象 |
| 46 | + JmsTemplate template = new JmsTemplate(connectionFactory); |
| 47 | + template.setPubSubDomain(false); |
| 48 | + // 创建 JmsMessageTemplate |
| 49 | + return new JmsMessagingTemplate(template); |
| 50 | + } |
| 51 | + |
| 52 | + // ========== 广播消费 ========== |
| 53 | + |
| 54 | + @Bean |
| 55 | + public Topic broadcastTopic() { |
| 56 | + return new ActiveMQTopic(BroadcastMessage.TOPIC); |
| 57 | + } |
| 58 | + |
| 59 | + @Bean(BROADCAST_JMS_LISTENER_CONTAINER_FACTORY_BEAN_NAME) |
| 60 | + public DefaultJmsListenerContainerFactory broadcastJmsListenerContainerFactory( |
| 61 | + DefaultJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) { |
| 62 | + DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); |
| 63 | + configurer.configure(factory, connectionFactory); |
| 64 | + factory.setPubSubDomain(true); |
| 65 | + return factory; |
| 66 | + } |
| 67 | + |
| 68 | + @Bean(BROADCAST_JMS_TEMPLATE_BEAN_NAME) |
| 69 | + public JmsMessagingTemplate broadcastJmsTemplate(ConnectionFactory connectionFactory) { |
| 70 | + // 创建 JmsTemplate 对象 |
| 71 | + JmsTemplate template = new JmsTemplate(connectionFactory); |
| 72 | + template.setPubSubDomain(true); |
| 73 | + // 创建 JmsMessageTemplate |
| 74 | + return new JmsMessagingTemplate(template); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments