ActiveMQ认识和深入(四),ActiveMQ+SpringBoot编写测试用例

Springboot整合 ActiveMQ


生产者(Queue)


引入依赖,编写pom.xml

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
编写application.yml

server:
  port: 8089

spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: false    #false  = Queue  true = Topic 不写,默认是Queue

#自定义队列名称
myqueue: boot-activemq-queue
编写configbean

package com.kelecc.activemq.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

import javax.jms.Queue;


/**
 * 功能描述: 依赖注入队列到spring
 *
 * @author: keLeCc
 */
@Component
@EnableJms
public class ConfigBean {

    @Value("${myqueue}")
    private String myQueue;

    @Bean
    public Queue queue(){
        return  new ActiveMQQueue(myQueue);
    }
}
编写生产者代码

package com.kelecc.activemq.produce;

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

import javax.jms.Queue;
import java.util.UUID;


/**
 * 功能描述: 生产者
 *
 * @author: keLeCc
 */
@Component
public class Produce {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    public void produceMsg(){
        jmsMessagingTemplate.convertAndSend(queue,"*****:"+ UUID.randomUUID().toString().substring(0,6));
        System.out.println("生产成功:"+UUID.randomUUID().toString().substring(0,6));
    }

}
编写测试用例

package com.kelecc.activemq;

import com.kelecc.activemq.produce.Produce;
import org.junit.Test;
import org.junit.runner.RunWith;
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;

/**
 * 功能描述:
 *
 * @author: keLeCc
 */
@SpringBootTest(classes = KeleccActivemqApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {

    @Resource
    private Produce produce;

    @Test
    public void testSendMessage() throws Exception{

        produce.produceMsg();
    }
}
运行结果

在这里插入图片描述

在这里插入图片描述

定时生产队列消息

package com.kelecc.activemq.produce;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import java.util.UUID;


/**
 * 功能描述: 生产者
 *
 * @author: keLeCc
 */
@Component
public class Produce {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    /**
     * 生产消息进入队列
     */
    public void produceMsg(){
        jmsMessagingTemplate.convertAndSend(queue,"*****:"+ UUID.randomUUID().toString().substring(0,6));
        System.out.println("生产成功:"+UUID.randomUUID().toString().substring(0,6));
    }

    /**
     * 间隔3秒定时生产消息
     */
    @Scheduled(fixedDelay = 3000)
    public void producerMsgScheduled(){
        jmsMessagingTemplate.convertAndSend(queue,"*****Scheduled:"+ UUID.randomUUID().toString().substring(0,6));
        System.out.println("Scheduled生产成功:"+UUID.randomUUID().toString().substring(0,6));
    }
}

启动类

package com.kelecc.activemq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class KeleccActivemqApplication {

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

}

运行结果

在这里插入图片描述

消费者(Queue)


编写消费者

package com.kelecc.activemq.cousumer;

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

import javax.jms.TextMessage;

/**
 * 功能描述:
 *
 * @author: keLeCc
 */
@Component
public class Cousumer {

    /**
     * 消费消息, 监听目的地 myqueue
     * @param textMessage 消息类型
     * @throws Exception
     */
    @JmsListener(destination = "${myqueue}")
    public void receive(TextMessage textMessage) throws Exception{
        System.out.println("======消费者收到消息=====:"+textMessage.getText());
    }
}

启动,运行结果

package com.kelecc.activemq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class KeleccActivemqApplication {

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

}

在这里插入图片描述
在这里插入图片描述

生产者(Topic)


server:
  port: 8089

spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: true    #false  = Queue  true = Topic 不写,默认是Queue

#自定义队列名称
#myqueue: boot-activemq-queue
mytopic: boot-activemq-topic
package com.kelecc.activemq.config.topic;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

import javax.jms.Topic;

/**
 * 功能描述:
 *
 * @author: keLeCc
 */
@Component
@EnableJms
public class TopicConfigBean {

    @Value("${mytopic}")
    private String topicName;

    @Bean
    public Topic topic(){
        return  new ActiveMQTopic(topicName);
    }
}

package com.kelecc.activemq.produce.topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Topic;
import java.util.UUID;

/**
 * 功能描述:
 *
 * @author: keLeCc
 */
@Component
public class TopicProduce {
    @Autowired
    private  JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Topic topic;

    /**
     * 间隔3秒发布一次
     */
    @Scheduled(fixedDelay = 3000)
    public  void produceTopic(){
        jmsMessagingTemplate.convertAndSend(topic,"发布主题消息:"+ UUID.randomUUID().toString().substring(0,6));
    }
}

消费者(Topic)


package com.kelecc.activemq.cousumer.topic;

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

import javax.jms.TextMessage;

/**
 * 功能描述:消费消息, 监听目的地 mytopic
 *
 * @author: keLeCc
 */
@Component
public class TopicCousumer {
    /**
     * 消费消息, 监听目的地 mytopic
     * @param textMessage 消息类型
     * @throws Exception
     */
    @JmsListener(destination = "${mytopic}")
    public void receive(TextMessage textMessage) throws Exception{
        System.out.println("======消费者收到消息=====:"+textMessage.getText());
    }
}

topic先注册消费者订阅,然后启动生产者

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可乐cc呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值