SpringBoot整合ActiveMQ

丶灬走出姿态 提交于 2020-03-01 19:41:01

SpringBoot整合ActiveMQ

点对点(P2P)

  创建springboot项目

    

  导入依赖

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

  生产者

    步骤一:applicationContext.properties文件

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8080

    步骤二:创建生产者

复制代码
package com.wn.p2p;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class P2Pprovider {
    //注入JMSTemplate
    @Resource
    private JmsTemplate jmsTemplate;

    public void setMessage(){
        //点对点创建队列
        ActiveMQQueue queue=new ActiveMQQueue("boot_queue");
        jmsTemplate.convertAndSend(queue,"消息123");
    }
}
复制代码

    步骤三:创建controller

复制代码
package com.wn.p2p;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class P2PController {
    @Resource
    private P2Pprovider p2Pprovider;

    @RequestMapping("/setMessage")
    public String setMessage(){
        p2Pprovider.setMessage();
        return "success";
    }
}
复制代码

    步骤四:启动

      

       

  消费者  

    步骤一:application.properties文件

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8081

    步骤二:创建消费者

复制代码
package com.wn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.JmsListener;

import javax.jms.JMSException;
import javax.jms.TextMessage;

@SpringBootApplication
public class P2pConsumerApplication {

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

    @JmsListener(destination = "boot_queue")
    public void getMessage(TextMessage message) throws JMSException {
        System.out.println("接收到的信息:"+message.getText());
    }
}
复制代码

    步骤三:启动

      

      

发布/订阅(Pub/Sub)

  结构

    

  导入依赖  

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

  消费者

    步骤一:applicationContext.properties文件

复制代码
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8082

#topic配置
spring.jms.pub-sub-domain=true
复制代码

    步骤二:创建消费者

复制代码
package com.wn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.TextMessage;

@SpringBootApplication
public class PubSubConsumerApplication {

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

    //springboot默认只配置queue类型消息,如果要使用topic类型的消息,则需要配置该bean
   /* @Bean
    public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        //这里必须设置为true,false则表示是queue类型
        factory.setPubSubDomain(true);
        return factory;
    }*/

    //消费者消费消息
    @JmsListener(destination = "boot_topic")
    public void getMessage(TextMessage message) throws JMSException {
        System.out.println("接收到消息:"+message.getText());
    }
}
复制代码

    步骤三:启动

       

  生产者

    步骤一:applicationContext.properties文件

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8083

    步骤二:创建生产者

复制代码
package com.wn.pub_sub;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class Pub_Sub_Provider {

    @Resource
    private JmsTemplate jmsTemplate;

    public void setMessage(){
        //创建主题
        ActiveMQTopic topic=new ActiveMQTopic("boot_topic");
        //springboot默认是队列,如果需要如下设置
        jmsTemplate.setPubSubDomain(true);
        //发送消息
        jmsTemplate.convertAndSend(topic,"消息456");
    }

}
复制代码

    步骤三:创建controller

复制代码
package com.wn.pub_sub;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class Pub_Sub_Controller {
    @Resource
    private Pub_Sub_Provider provider;

    @RequestMapping("/setMessage")
    public String setMessage(){
        provider.setMessage();
        return "success";
    }
}
复制代码

    步骤四:启动

      

      

      此时可以查看消费者是否成功接收到消息

      

点对点(P2P)

  创建springboot项目

    

  导入依赖

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

  生产者

    步骤一:applicationContext.properties文件

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8080

    步骤二:创建生产者

复制代码
package com.wn.p2p;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class P2Pprovider {
    //注入JMSTemplate
    @Resource
    private JmsTemplate jmsTemplate;

    public void setMessage(){
        //点对点创建队列
        ActiveMQQueue queue=new ActiveMQQueue("boot_queue");
        jmsTemplate.convertAndSend(queue,"消息123");
    }
}
复制代码

    步骤三:创建controller

复制代码
package com.wn.p2p;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class P2PController {
    @Resource
    private P2Pprovider p2Pprovider;

    @RequestMapping("/setMessage")
    public String setMessage(){
        p2Pprovider.setMessage();
        return "success";
    }
}
复制代码

    步骤四:启动

      

       

  消费者  

    步骤一:application.properties文件

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8081

    步骤二:创建消费者

复制代码
package com.wn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.JmsListener;

import javax.jms.JMSException;
import javax.jms.TextMessage;

@SpringBootApplication
public class P2pConsumerApplication {

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

    @JmsListener(destination = "boot_queue")
    public void getMessage(TextMessage message) throws JMSException {
        System.out.println("接收到的信息:"+message.getText());
    }
}
复制代码

    步骤三:启动

      

      

发布/订阅(Pub/Sub)

  结构

    

  导入依赖  

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

  消费者

    步骤一:applicationContext.properties文件

复制代码
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8082

#topic配置
spring.jms.pub-sub-domain=true
复制代码

    步骤二:创建消费者

复制代码
package com.wn;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.TextMessage;

@SpringBootApplication
public class PubSubConsumerApplication {

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

    //springboot默认只配置queue类型消息,如果要使用topic类型的消息,则需要配置该bean
   /* @Bean
    public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        //这里必须设置为true,false则表示是queue类型
        factory.setPubSubDomain(true);
        return factory;
    }*/

    //消费者消费消息
    @JmsListener(destination = "boot_topic")
    public void getMessage(TextMessage message) throws JMSException {
        System.out.println("接收到消息:"+message.getText());
    }
}
复制代码

    步骤三:启动

       

  生产者

    步骤一:applicationContext.properties文件

spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.user=admin
spring.activemq.password=admin
server.port=8083

    步骤二:创建生产者

复制代码
package com.wn.pub_sub;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class Pub_Sub_Provider {

    @Resource
    private JmsTemplate jmsTemplate;

    public void setMessage(){
        //创建主题
        ActiveMQTopic topic=new ActiveMQTopic("boot_topic");
        //springboot默认是队列,如果需要如下设置
        jmsTemplate.setPubSubDomain(true);
        //发送消息
        jmsTemplate.convertAndSend(topic,"消息456");
    }

}
复制代码

    步骤三:创建controller

复制代码
package com.wn.pub_sub;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class Pub_Sub_Controller {
    @Resource
    private Pub_Sub_Provider provider;

    @RequestMapping("/setMessage")
    public String setMessage(){
        provider.setMessage();
        return "success";
    }
}
复制代码

    步骤四:启动

      

      

      此时可以查看消费者是否成功接收到消息

      

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!