1. 程式人生 > >activemq+springboot,topic+queue模式

activemq+springboot,topic+queue模式

一.maven中新增activemq依賴:

        <!-- activemq -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

二.application.properties配置檔案中新增配置:

## activemq ##
spring.activemq.broker-url=tcp://127.0.0.1:61616 
spring.activemq.user=admin 
spring.activemq.password=admin

activemq.topicName1=firstTopic
activemq.topicName2=secondTopic
activemq.queueName1=firstQueue
activemq.queueName2=secondQueue

三.編寫配置類ActiveMQConfig:

package blog;

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.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.Queue;
import javax.jms.Topic;

/**
 * description:
 * author: 
 * date: 2018-12-13 11:46
 **/
@Configuration
@EnableJms
public class ActiveMQConfig {

    @Value("${activemq.topicName1}")
    public String TOPIC1;
    @Value("${activemq.topicName2}")
    public String TOPIC2;
    @Value("${activemq.queueName1}")
    public String QUEUE1;
    @Value("${activemq.queueName2}")
    public String QUEUE2;

    @Bean
    public Topic topic1() {
        return new ActiveMQTopic(TOPIC1);
    }

    @Bean
    public Topic topic2() {
        return new ActiveMQTopic(TOPIC2);
    }

    @Bean
    public Queue queue1() {
        return new ActiveMQQueue(QUEUE1);
    }

    @Bean
    public Queue queue2() {
        return new ActiveMQQueue(QUEUE2);
    }

    /**
     * topic模式的ListenerContainer
     *
     * @return
     */
    @Bean
    public JmsListenerContainerFactory<?> topicListenerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        ActiveMQConnectionFactory connectionFactor = new ActiveMQConnectionFactory();
        factory.setPubSubDomain(true);//topic模式
        factory.setConnectionFactory(connectionFactor);
        return factory;
    }

    /**
     * queue模式的ListenerContainer
     *
     * @return
     */
    @Bean
    public JmsListenerContainerFactory<?> queueListenerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        ActiveMQConnectionFactory connectionFactor = new ActiveMQConnectionFactory();
        factory.setPubSubDomain(false);//預設就是false
        factory.setConnectionFactory(connectionFactor);
        return factory;
    }


}

四.編寫客戶端生產者ActiveMQClientProducer:

package blog;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import javax.jms.Topic;

/**
 * description:
 * author: 
 * date: 2018-12-13 11:57
 **/
@Component
public class ActiveMQClientProducer {

    @Autowired
    JmsTemplate jmsTemplate;
    @Autowired
    Topic topic1;
    @Autowired
    Topic topic2;
    @Autowired
    Queue queue1;
    @Autowired
    Queue queue2;

    public void topic1Produce(String message) {
        this.jmsTemplate.convertAndSend(topic1, message);
    }

    public void topic2Produce(String message) {
        this.jmsTemplate.convertAndSend(topic2, message);
    }

    public void queue1Produce(String message) {
        this.jmsTemplate.convertAndSend(queue1, message);
    }

    public void queue2Produce(String message) {
        this.jmsTemplate.convertAndSend(queue2, message);
    }
    
}

五.編寫客戶端消費者ActiveMQClientConsumer:

package blog;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * description:
 * author: 
 * date: 2018-12-13 12:03
 **/
@Component
public class ActiveMQClientConsumer {

    @JmsListener(destination = "${activemq.topicName1}", containerFactory = "topicListenerFactory")
    public void receiveTopic1(String msg) {
        System.out.println("=====================topic1收到的訊息====================:" + msg);
    }

    @JmsListener(destination = "${activemq.topicName2}", containerFactory = "topicListenerFactory")
    public void receiveTopic2(String msg) {
        System.out.println("=====================topic2收到的訊息====================:" + msg);
    }

    @JmsListener(destination = "${activemq.queueName1}", containerFactory = "queueListenerFactory")
    public void receiveQueue1(String msg) {
        System.out.println("=====================queue1收到的訊息====================:" + msg);
    }

    @JmsListener(destination = "${activemq.queueName2}", containerFactory = "queueListenerFactory")
    public void receiveQueue2(String msg) {
        System.out.println("=====================queue2收到的訊息====================:" + msg);
    }


}

啟動測試類程式碼待補。。。