Spring-boot项目整合Activemq实现订阅发布模式

1.在项目POM中配置

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

2.在application.properties中配置

spring.jms.pub-sub-domain = true
spring.activemq.user = admin
spring.activemq.password = admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
spring.activemq.pool.maxConnections = 50
spring.activemq.pool.idleTimeout = 30000
spring.activemq.packages.trust-all = true
spring.activemq.broker-url = tcp://127.0.0.1:61616

3.配置Activemq发送消息类


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

import javax.jms.Queue;

@Component
public class JMSProducer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Queue queue;

    public void sendMessage( String message) {
        this.jmsMessagingTemplate.convertAndSend(queue, message);
    }
}

4.在配置发送消息的业务对象中添加jmsProducer消息发送对象

    @Autowired
    private JMSProducer jmsProducer;

5.使用jmsProducer消息发送对象

                        jmsProducer.sendMessage("hello word");

6.在另一个服务中实现消息消费


@Component
public class Consumer {
 
    @JmsListener(destination = "demo.test")
    public void receiveQueue(String text) {
        System.out.println(text);
    }
}

7.在SpringBootApplication对象中添加

@EnableJms //配置在对象上面
//配置在对象内进行注入
@Bean 
    public Queue queue() {
        return new ActiveMQQueue("huobi.kline.k");
    }

8.以上配置都属于初步在项目中整合Activemq,后期会提供更加深入都优化解析配置。

上一篇:SAP发布SAP HANA精简版和Web IDE 只为推广HANA?


下一篇:机房收费系统之思想性总结