Java SpringBoot如何整合ActiveMQ

2024-03-14,,

Java SpringBoot如何整合ActiveMQ,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

一、 如果要想在项目之中去使用 ActiveMQ 组件,则应该为项目添加依赖支持库,修改 pom.xml 配置文件:

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

 二、修改 application.yml 配置文件进行 activemq 的配置;

server:
  port: 80
spring:
  messages:
    basename: i18n/Messages,i18n/Pages
  jms:
    pub-sub-domain: false   # 配置消息的类型,如果是true则表示为topic消息,如果为false表示Queue消息
  activemq:
    user: studyjava    # 连接用户名
    password: hello   # 连接密码
    broker-url: tcp://activemq-server:61616 # 消息组件的连接主机信息

三、 随后定义一个消息的消费者,消费者主要是进行一个监听控制,在 SpringBoot 里面可以直接利用注解@JmsListener进行监听:

package cn.study.microboot.consumer;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
@Service
public class MessageConsumerService {
    @JmsListener(destination="study.msg.queue")
    public void receiveMessage(String text) {    // 进行消息接收处理
        System.err.println("【*** 接收消息 ***】" + text);
    }
}

四、 随后建立消息的发送者服务,一般而言如果进行消息的发送往往会准备出一个业务接口来:

package cn.study.microboot.producer;
public interface IMessageProducerService {   
 public void sendMessage(String msg) ;
  }

 五、随后建立一个配置程序类,定义 ActiveMQ 的消息发送模版处理类:

package cn.study.microboot.config;
import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
@Configuration
@EnableJms
public class ActiveMQConfig {
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("study.msg.queue") ;
    }
}

六、创建消息发送的子类实现消息发送处理:

package cn.study.microboot.producer.impl;
import javax.annotation.Resource;
import javax.jms.Queue;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import cn.study.microboot.producer.IMessageProducerService;
@Service
public class MessageProducerServiceImpl implements IMessageProducerService {
    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Resource
    private Queue queue;
    @Override
    public void sendMessage(String msg) {
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }
}

七、编写测试类来观察消息的处理:

package cn.study.microboot.test;
import javax.annotation.Resource;
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 cn.study.microboot.StartSpringBootMain;
import cn.study.microboot.producer.IMessageProducerService;
@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {
    @Resource
    private IMessageProducerService messageProducer;
    @Test
    public void testSend() throws Exception {
        for (int x = 0; x < 10; x++) {
            this.messageProducer.sendMessage("study - " + x);
        }
    }
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注本站行业资讯频道,感谢您对本站的支持。

《Java SpringBoot如何整合ActiveMQ.doc》

下载本文的Word格式文档,以方便收藏与打印。