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("${spring.activemq.broker-url}")
    String brokerUrl;
    @Value("${spring.activemq.user}")
    String username;
    @Value("${spring.activemq.password}")
    String password;
    @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();
        connectionFactor.setBrokerURL(brokerUrl);
        connectionFactor.setUserName(username);
        connectionFactor.setPassword(password);
        factory.setPubSubDomain(true);//topic模式
        factory.setConnectionFactory(connectionFactor);
        return factory;
    }

    /**
     * queue模式的ListenerContainer
     *
     * @return
     */
    @Bean
    public JmsListenerContainerFactory<?> queueListenerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        ActiveMQConnectionFactory connectionFactor = new ActiveMQConnectionFactory();
        connectionFactor.setBrokerURL(brokerUrl);
        connectionFactor.setUserName(username);
        connectionFactor.setPassword(password);
        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);
    }


}

六.测试类:

package demo;

import blog.ActiveMQClientProducer;
import blog.IProduce;
import blog.SpringBootApplicationBlog;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.Resource;


@SpringBootTest(classes = SpringBootApplicationBlog.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQBlog {

    @Autowired
    ActiveMQClientProducer activeMQClientProducer;

    @Test
    public void testProduce() throws Exception {
        for (int i = 0; i < 5; i++) {
            activeMQClientProducer.topic1Produce("the topic1 produce message haha" + i);
        }
        for (int i = 0; i < 5; i++) {
            activeMQClientProducer.topic2Produce("the topic2 produce message xixi" + i);
        }
        for (int i = 0; i < 5; i++) {
            activeMQClientProducer.queue1Produce("the queue1 produce message hehe" + i);
        }
        for (int i = 0; i < 5; i++) {
            activeMQClientProducer.queue2Produce("the queue2 produce message heihei" + i);
        }
    }
}

运行结果:

其中SpringBootApplicationBlog是springboot的启动类:

package blog;

import org.springframework.boot.SpringApplication;

/**
 * ClassName: SpringBootApplication
 * description:
 * author: 
 * date: 2018-09-30 09:15
 **/
@org.springframework.boot.autoconfigure.SpringBootApplication//@EnableAutoConfiguration @ComponentScan
public class SpringBootApplicationBlog {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApplicationBlog.class, args);
    }


}

补充:其中连接activemq的user、password用户密码的broker验证是我自定义的一个activemq的验证插件,默认应该是不需要验证的,插件代码见:activemq自定义安全验证插件,集成mysql

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值