资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

JavaSpringBoot如何整合ActiveMQ

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

目前创新互联公司已为超过千家的企业提供了网站建设、域名、虚拟空间、网站托管运营、企业网站设计、渝水网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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

 
            org.springframework.boot
            spring-boot-starter-activemq
        

 二、修改 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);
        }
    }
}

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


本文标题:JavaSpringBoot如何整合ActiveMQ
分享URL:http://cdkjz.cn/article/pisoii.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220