SpringBoot学习笔记二十五、整合ActiveMQ消息队列

  • 添加pom依赖

        <!-- ActiveMQ -->
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-activemq</artifactId>  
        </dependency>  
        
        <!-- ActiveMQ线程池 -->
        <dependency>  
            <groupId>org.apache.activemq</groupId>  
            <artifactId>activemq-pool</artifactId>  
        </dependency>

  • 添加application.properties相关配置

 #整合ActiveMQ
spring.activemq.broker-url=tcp://127.0.0.1:61616
#集群配置
#spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)
spring.activemq.user=admin
spring.activemq.password=admin
#ActiveMQ线程池配置
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100

#开启发布订阅模式,默认点对点
#spring.jms.pub-sub-domain=true

  •  修改Application类

 

package com.example.demo;

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.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

@SpringBootApplication
@EnableJms //开启支持JMS
public class DemoApplication extends SpringBootServletInitializer{
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DemoApplication.class);
    }
    
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }
    
    @Bean
    public Queue queue(){
        return new ActiveMQQueue("common.queue");
    }
    
    @Bean
    public Topic topic(){
        return new ActiveMQTopic("video.topic");
    }

}
 

  •  消息发送或发布Service接口

 

package com.example.demo.service;

import javax.jms.Destination;

public interface ProducerService {

    /**
     * 功能描述: 发送消息到指定队列
     * @param destination
     * @param message
     */
    public void sendMessage(Destination destination, final String message);
    
    /**
     * 功能描述: 发送消息到公共队列
     * @param message
     */
    public void sendMessage(final String message);
    
    /**
     * 功能描述: 发布消息
     * @param message
     */
    public void pushMessage(final String message);
    
}
 

  •   消息发送或发布Service接口实现

package com.example.demo.service.impl;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;

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

import com.example.demo.service.ProducerService;

@Service
public class ProducerServiceImpl implements ProducerService{

    @Autowired
    private JmsMessagingTemplate jmsTemplate;
    
    @Autowired
    private Queue queue;
    
    @Autowired
    private Topic topic;
    
    @Override
    public void sendMessage(Destination destination, final String message) {
        jmsTemplate.convertAndSend(destination,message);
    }

    @Override
    public void sendMessage(String message) {
        jmsTemplate.convertAndSend(queue,message);
    }

    @Override
    public void pushMessage(String message) {
        jmsTemplate.convertAndSend(topic,message);
    }
    

}

  •  消费者1

package com.example.demo.jms;

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

@Component
public class TestConsumer {

    @JmsListener(destination="test.queue")
    public void receiveQueue(String message){
        System.out.println("TestConsumer接收报文:"+message);
    }
    
}

  •  消费者2

package com.example.demo.jms;

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

@Component
public class CommonConsumer {

    @JmsListener(destination="common.queue")
    public void receiveQueue(String message){
        System.out.println("CommonConsumer接收报文:"+message);
    }
    
}

  • 订阅者 

package com.example.demo.jms;

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

@Component
public class TopicSub {

    @JmsListener(destination="video.topic",containerFactory="jmsListenerContainerTopic")
    public void receive1(String message){
        System.out.println("receive1接收报文:"+message);
    }
    
    @JmsListener(destination="video.topic",containerFactory="jmsListenerContainerTopic")
    public void receive2(String message){
        System.out.println("receive2接收报文:"+message);
    }
    
    @JmsListener(destination="video.topic",containerFactory="jmsListenerContainerTopic")
    public void receive3(String message){
        System.out.println("receive3接收报文:"+message);
    }
    
}

  •  controller类

package com.example.demo.controller;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.domain.JsonData;
import com.example.demo.service.ProducerService;

@RestController
@RequestMapping("/activemq")
public class ActiveMQController {
    
    @Autowired
    private ProducerService messageProducerService;

    /**
     * 功能描述: 发送信息到指定队列
     * @param message
     * @return
     */
    @RequestMapping("/test")
    public Object test(String message){
        Destination destination = new ActiveMQQueue("test.queue");
        messageProducerService.sendMessage(destination, message);
        return JsonData.buildSuccess();
    }
    
    /**
     * 功能描述: 发送信息到公用队列
     * @param message
     * @return
     */
    @RequestMapping("/common")
    public Object common(String message){
        messageProducerService.sendMessage(message);
        return JsonData.buildSuccess();
    }
    
    /**
     * 功能描述: 发布消息
     * @param message
     * @return
     */
    @RequestMapping("/topic")
    public Object topic(String message){
        messageProducerService.pushMessage(message);
        return JsonData.buildSuccess();
    }
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值