springboot整合activemq ( mq )

weblog 1036 0 0
pom
  <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
	</parent>
  <dependencies>
  	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
  </dependencies>
application.yml配置文件
server:
  port: 8081
spring: 
  activemq:
    #ActiveMQ通讯地址
    broker-url: tcp://192.168.166.139:61616
    #用户名
    user: admin
    #密码
    password: admin
    #是否启用内存模式(就是不安装MQ,项目启动时同时启动一个MQ实例)
    in-memory: false
    packages:
      #信任所有的包
      trust-all: true
    pool:
      #是否替换默认的连接池,使用ActiveMQ的连接池需引入的依赖
      enabled: false
activemq配置类
import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
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;

@Configuration
@EnableJms
public class ActiveMQConfig {

    //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;
    }
    
    @Bean("Queue")
    public Queue queue() {
        return new ActiveMQQueue("springboot.queue") ;
    }
    
    @Bean("Queue2")
    public Queue queue2() {
        return new ActiveMQQueue("springboot.queue2") ;
    }

    @Bean("Topic")
    public Topic topic() {
        return new ActiveMQTopic("springboot.topic") ;
    }
}
生产者
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.Queue;
import javax.jms.Topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Producer {
    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Resource(name = "Queue")
    private Queue queue;
    
    @Resource(name = "Queue2")
    private Queue queue2;
    
    @Resource(name = "Topic")
    private Topic topic;

    //发送queue类型消息
    @GetMapping("/queue")
    public void sendQueueMsg(String msg){
        jmsTemplate.convertAndSend(queue, msg);
    }
    
    //发送queue2类型消息(对象)
    @GetMapping("/queue2")
    public void sendQueueMsg2(String msg){
    	List<String> list=new ArrayList<String>();
    	list.add("123");
    	list.add(msg);
        jmsTemplate.convertAndSend(queue2, list);
    }
    

    //发送topic类型消息
    @GetMapping("/topic")
    public void sendTopicMsg(String msg){
        jmsTemplate.convertAndSend(topic, msg);
    }
}
消费者:
import java.util.List;

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

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

@Component
public class Consumer {

    //接收queue类型消息
    //destination对应配置类中ActiveMQQueue("springboot.queue")设置的名字
    @JmsListener(destination="springboot.queue")
    public void ListenQueue(final TextMessage text) throws JMSException{
        System.out.println("接收到queue消息:" + text.getText());
    }
    
    //接收queue类型消息
    //destination对应配置类中ActiveMQQueue("springboot.queue2")设置的名字
	@JmsListener(destination="springboot.queue2")
    public void ListenQueue2(final List<String> list) throws JMSException{
        System.out.println("接收到queue2消息:" + list);
    }
	
    //接收topic类型消息
    //destination对应配置类中ActiveMQTopic("springboot.topic")设置的名字
    //containerFactory对应配置类中注册JmsListenerContainerFactory的bean名称
    @JmsListener(destination="springboot.topic", containerFactory = "jmsTopicListenerContainerFactory")
    public void ListenTopic(final TextMessage text) throws JMSException{
        System.out.println("接收到topic消息:" + text.getText());
    }
}

 


猜你喜欢
weblog 937 1.修改配置文件修改activemq安装目录下./bin目录下的配置文件activemq.xml在transportConnectors标签中添加nio协议
框架 2563 springbootmybatis1.创建maven项目2.sql文件SETNAMESutf8mb4;SETFOREIGN_KEY_CHECKS=0
框架 2671 1.配置springboot支持websocketpackagecom.example.demo.websocket
official 884 上一篇《(mq)rabbitmq安装延时队列插件实现延时消息1》文章中介绍了rabbitmq安装延时队列插件。本编将继续结代码来实现延时队列(基于springboot项目)。下方所有源代码均已上传
java框架 1378 springbootelasticsearch框架实现全文索引demo配置说明参考:http://www.jiajiajia.club/blog/artical/Ja4t7X/378
框架 2409 安装redis数据库参考:http://www.jiajiajia.club/blog/artical/166redis配置详解参考:http://www.jiajiajia.club/blog/artical/210安装完数据库以后如果不是本地连接记得修改密码requirepass。默认是没有密码需要后台运行修改daemonizeyes默认是noyml配置文件spring:redis:host:
框架 1483 1.pom文件dependencygroupIdorg.apache.shiro/groupIdartifactIdshiro-spring/artifactIdversion1.4.0/version/dependencydependency groupIdorg.apache.shiro/groupId artifactIdshiro-ehcache/artifactId vers
框架 2346 springboot视图层,官方推荐使用thymeleaf。thymeleaf只是渲染html的一种方式,是一种模板。第一步创建一个maven项目第二步:修改Jdk版本,添加thymeleaf
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。