【Java】SpringBoot下使用ActiveMQ

JMS



1 什么是JMS

JMS全称Java Message Service,即Java消息服务应用程序接口,是Java平台中一种面向消息中间件(MOM)的API。用在两个应用程序之间,或分布式系统中发送消息,进行异步通信。JMS是一种与具体语言无关的API,绝大多数MOM提供商都对JMS提供支持。

2 SpringBoot中使用ActiveMQ中间件

消息中间件的优点:

  • 应用解耦。
  • 异步处理。
  • 流量消峰。

2.1 基础配置

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>jms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jms</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.messaginghub</groupId>
            <artifactId>pooled-jms</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
application.yml
server:
  port: 8080


spring:
  activemq:
    user: admin
    password: admin
    broker-url: tcp://localhost:61616
    pool:
      enabled: true
      max-connections: 5

  jms:
    cache:
      session-cache-size: 5
    # pub-sub-domain: true

2.2 PTP(Point-To-Point 点对点)模式

注意:如果在ActiveMQ中使用PTP,必须保证配置文件中的spring.jms.pub-sub-domain设为false(即默认值),因为如果为true时只能使用发布/订阅模式

创建消费者(Consumer)
package com.demo.jms.consumer;

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

@Component
public class MessageConsumer {
    
    @JmsListener(destination = "queue")
    public void receiveMessage(String message) {
        System.out.println(this.getClass().getName() + ": " + message);
    }   
}
创建生产者(producer)
package com.demo.jms.producer;

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

@Service
public class ProducerService {
    
    private JmsMessagingTemplate jmsMessagingTemplate;
    
    @Autowired
    public ProducerService (JmsMessagingTemplate template) {
        this.jmsMessagingTemplate = template;
    }
    
    public void sendMessage(String destinationName, String msg) {
        Destination destination = new ActiveMQQueue(destinationName);
        jmsMessagingTemplate.convertAndSend(destination, msg);
    }
}
生产者生产
package com.demo.jms.controller;

import com.demo.jms.producer.ProducerService;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.jms.Destination;

@Controller
public class ProducerController {
    
    private ProducerService service;

    public ProducerController(ProducerService service) {
        this.service = service;    
    }

    @GetMapping(value="/send/{destinationName}/{msg}")
    public void sendMsg( @PathVariable("destinationName") String destinationName, @PathVariable("msg") String msg) {
        service.sendMessage(destinationName, msg);
    }
}

2.3 发布订阅模式(Publish/Subscribe)

注意:发布订阅模式中必须将spring.jms.pub-sub-domain设置为true

创建消费者
package com.demo.jms.consumer;

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

@Component
public class TopicMessageConsumer {
    
    @JmsListener(destination = "topic")
    public void receiveTopic(String message) {
        System.out.println(this.getClass().getName() + ": " + message);
    }   

    @JmsListener(destination = "topic")
    public void receiveTopic2(String message) {
        System.out.println("发布订阅模式: " + message);
    }  
}
创建生产者
package com.demo.jms.producer;

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

@Service
public class TopicProducerService {
    
    private JmsMessagingTemplate jmsMessagingTemplate;
    
    @Autowired
    public TopicProducerService(JmsMessagingTemplate template) {
        this.jmsMessagingTemplate = template;
    }
    
    public void sendMessageTopic(String topicName, String msg) {
        // 此处与点对点模式不同
        ActiveMQTopic topic = new ActiveMQTopic(topicName);
        jmsMessagingTemplate.convertAndSend(topic, msg);
    }
}
生产者生产
package com.demo.jms.controller;

import com.demo.jms.producer.TopicProducerService;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.jms.Destination;

@Controller
public class TopicProducerController {
    
    private TopicProducerService service;

    public TopicProducerController(TopicProducerService service) {
        this.service = service;    
    }

    @GetMapping(value = "/topic/{topicName}/{msg}")
    public void sendTopic(@PathVariable("topicName") String topicName, @PathVariable("msg") String msg) {
        service.sendMessageTopic(topicName, msg);
    }
}

2.4 发布订阅模式与点对点模式共存

由于spring.jms.pub-sub-domain设置为true时仅支持发布/订阅,设置为false时又不会触发发布/订阅。所以需要在代码中主动添加发布/订阅的监听bean,且spring.jms.pub-sub-domain必须设置为***false***

配置configuration
package com.demo.jms.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import javax.jms.ConnectionFactory;

@Configuration
public class ActiveMQConfig {

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory pooledJmsConnectionFactory) {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(pooledJmsConnectionFactory);
        return bean;
    }

}
消费者
package com.demo.jms.consumer;

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

@Component
public class MessageConsumer {
    
    @JmsListener(destination = "queue")
    public void receiveMessage(String msg) {
        System.out.println(this.getClass().getName() + ": " + msg);
    }
    
    /**
     * 注意:发布/订阅中的<code>containerFactory</code>需要设置成配置类<code>ActiveMQConfig</code>中
     * 的<code>jmsListenerContainerTopic</code>
     */
    @JmsListener(destination = "topic", containerFactory = "jmsListenerContainerTopic")
    public void receiveTopic(String msg) {
        System.out.println("receiveTopic: " + msg);
    }
    
    @JmsListener(destination = "topic", containerFactory = "jmsListenerContainerTopic")
    public void receiveTopic2(String msg) {
        System.out.println("receiveTopic2: " + msg);
    }
}
生产者
package com.demo.jms.producer;

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;

@Service
public class MessageProducer {

    private JmsMessagingTemplate jmsMessagingTemplate;

    public MessageProducer(){}

    @Autowired
    public MessageProducer(JmsMessagingTemplate template) {
        this.jmsMessagingTemplate = template;
    }

    /**
     * 发送消息:点对点模式。
     * @param destinationName 接收者
     * @param message 消息内容
     */
    public void sendMessage(String destinationName, String message) {
        Destination destination = new ActiveMQQueue(destinationName);
        jmsMessagingTemplate.convertAndSend(destination, message);
    }

    /**
     * 发送消息:发布/订阅模式
     */
    public void sendTopic(String topicName, String message) {
        ActiveMQTopic topic = new ActiveMQTopic(topicName);
        jmsMessagingTemplate.convertAndSend(topic, message);
    }
}

生产者生产
package com.demo.jms.controller;

import com.demo.jms.producer.MessageProducer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProducerController {

    private MessageProducer producer;

    public ProducerController(MessageProducer producer) {
        this.producer = producer;
    }

    @GetMapping(value = "/send/{destination}/{msg}")
    public void sendMessage(@PathVariable("msg") String msg, @PathVariable("destination") String destinationName) {
        producer.sendMessage(destinationName, msg);
    }

    @GetMapping(value = "/topic/{topicName}/{msg}")
    public void sendTopic(@PathVariable("msg") String msg, @PathVariable("topicName") String topicName) {
        producer.sendTopic(topicName, msg);
    }
}

项目启动后再浏览器中输入:

http://localhost:8080/send/queue/这是点对点模式

http://localhost:8080/topic/topic/这是发布订阅模式

则在后台分别输出

com.demo.jms.consumer.MessageConsumer: 这是点对点模式

receiveTopic2: 这是发布订阅模式
receiveTopic: 这是发布订阅模式

3. 注意事项

3.1 连接池的应用

  1. 如果在配置文件application.properties中使用了spring.activemq.pool.enadble = true后,需要在pom.xml中引入连接池相关依赖:
<!-- 在旧版本spring boot中需要引入activemq-pool依赖 -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
    <version>5.9.1</version>
</dependency>

<!-- 在spring boot 2.x中需要引入pooled-jms依赖 -->
<dependency>
    <groupId>org.messaginghub</groupId>
    <artifactId>pooled-jms</artifactId>
</dependency>

如果不引入,则会引发JmsTemplate注入失败的错误。具体引用哪一个依赖,则需要根据使用的SpringBoot版本来确定,或者查看类ActiveMQConnectionFactoryConfiguration引用的是哪一个包下的JmsPoolConnectionFactory。如SpringBoot 2.2.4.RELEASE中引用的是:

package org.springframework.boot.autoconfigure.jms.activemq;

import java.util.List;
import java.util.stream.Collectors;
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.commons.pool2.PooledObject;
import org.messaginghub.pooled.jms.JmsPoolConnectionFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jms.JmsPoolConnectionFactoryFactory;
import org.springframework.boot.autoconfigure.jms.JmsProperties;
import org.springframework.boot.autoconfigure.jms.JmsProperties.Cache;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;

@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnMissingBean({ConnectionFactory.class})
class ActiveMQConnectionFactoryConfiguration {
    // ...
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值