springboot环境下使用activemq消息队列

一、activemq安装简单说明

activemq下载地址:http://activemq.apache.org

将tar包下载使用root用户拷贝到服务器上并解压缩,执行相应脚本启动服务。

tar -zxvf apache-activemq-5.15.8-bin.tar.gz

cd apache-activemq-5.15.8
cd bin
//启动服务
./activemq start

缺省端口:
8161  web管理页面端口
61616 activemq服务监控端口

缺省监控页面
http://IP:8161/admin
默认用户名密码 admin/admin

build.gradle

buildscript {
    ext {
        springBootVersion = '2.1.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-activemq'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

application.properties

server.port=9090
spring.activemq.broker-url=tcp://192.168.109.102:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false

消息的发送端

ProducerService.class

package com.example.activemq;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

/**
 * @className: ProducerService
 * @description: ProducerService
 * @author: houwei
 * @date: 2019/1/31 15:04
 */
@Service
public class ProducerService {
    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    /**
     * 一对一发送消息
     * @param who 目标方标识
     * @param message 消息内容
     */
    public void sendMessageToOtherOne(final String who,final String message){
        Destination destination = new ActiveMQQueue(who);
        jmsTemplate.convertAndSend(destination, message);
    }

    /**
     * 一对多发布话题
     * @param topic 话题
     * @param message 消息内容
     */
    public void publishTopic(final String topic,final String message){
        Destination destination = new ActiveMQTopic(topic);
        jmsTemplate.convertAndSend(destination, message);
    }
}
ProducerController.class
package com.example.activemq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @className: ProducerController
 * @description: 队列消息控制器
 * @author: houwei
 * @date: 2019/1/31 14:53
 */
@RestController
@RequestMapping("/api/producer")
public class ProducerController {
    @Autowired
    ProducerService service;

    @RequestMapping("/send")
    public void sendToOne(String who,String msg) {
        service.sendMessageToOtherOne(who, msg);
    }

    @RequestMapping("/publish")
    public void publishTopic(String topic,String msg) {
        service.publishTopic(topic, msg);
    }
}

消息接收方

package com.example.activemq;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @className: Consumer
 * @description: Consumer
 * @author: houwei
 * @date: 2019/1/31 16:46
 */
@Component
public class Consumer1 {
    private static final Log logger = LogFactory.getLog(Consumer1.class);

    @JmsListener(destination = "Tom")
    public void receiveQueue(String text) {
        logger.info("Tom receive message:" + text);
    }
}

对于订阅模式的消息接收,需要指定 containerFactory

package com.example.activemq;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @className: Consumer2
 * @description: Consumer2
 * @author: houwei
 * @date: 2019/1/31 17:28
 */
@Component
public class Consumer2 {
    private static final Log logger = LogFactory.getLog(Consumer2.class);

    @JmsListener(destination = "news", containerFactory = "myJmsContainerFactory")
    public void subscribeTopic(String text) {
        logger.info("Jacky receive subscribe news Topic"+text);
    }
}

我们可以定义一个全局Bean

package com.example.activemq;

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

import javax.jms.ConnectionFactory;

/**
 * @author houwei
 */
@SpringBootApplication
public class ActivemqApplication {

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

    @Bean
    JmsListenerContainerFactory<?> myJmsContainerFactory(ConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }

}

订阅模式,可以运行多人订阅相关

package com.example.activemq;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @className: Consumer
 * @description: Consumer
 * @author: houwei
 * @date: 2019/1/31 16:46
 */
@Component
public class Consumer1 {
    private static final Log logger = LogFactory.getLog(Consumer1.class);

    @JmsListener(destination = "Tom")
    public void receiveQueue(String text) {
        logger.info("Tom receive message:" + text);
    }

    @JmsListener(destination = "news", containerFactory = "myJmsContainerFactory")
    public void subscribeTopic(String text) {
        logger.info("Tom receive subscribe news Topic" + text);
    }
}

测试:http://localhost:9090/api/producer/publish?topic=news&msg=hello world2

Consumer1与 Consumer2 都可以收到话题为news的消息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值