springboot 使用 ActiveMQ

22 篇文章 0 订阅

pom文件添加依赖

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

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
    <version>5.17.1</version>
</dependency>

配置 application.yml

server:
  port: 8080 #springBoot项目访问端口
 
spring:
  activemq:
    broker-url: tcp://192.168.140.xx:30005 #你activeMQ的ip和端口号
    user: admin   #activeMq账号
    password: admin #activeMq密码
    pool:
      enabled: true       #连接池启动
      max-connections: 10 #最大连接数  

SpringBoot 的启动类添加解 @EnableJms

@SpringBootApplication
@EnableJms
public class MqconverterApplication {

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

}

创建 activeMQ 配置类

mport org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;


/**
 * 配置类
 */
@Configuration
public class ActiveMQConfig {

    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;

    @Value("${spring.activemq.user}")
    private String userName;

    @Value("${spring.activemq.password}")
    private String password;

    @Bean
    public ConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(userName, password, brokerUrl);
    }

    /**
     * 在 Queue 模式中,对消息的监听需要对containerFactory进行配置
     */
    @Bean("queueListener")
    public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(false);
        return factory;
    }

    /**
     * 在 topic 模式中,对消息的监听需要对containerFactory进行配置
     */
    @Bean("topicListener")
    public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }

}

pubSubDomain:为true 代表 topic 模式,false 代表队列模式,默认是false

在这里插入图片描述

生产者

@RestController
public class ActiveMQProducerController {

    /**
     * 队列模式
     * @param msg 消息
     * @return
     */
    @PostMapping("/queue/{msg}")
    public String sendQueue(@PathVariable("msg") String msg) {
        ActiveMQQueue queue = new ActiveMQQueue("testq");
        jmsMessagingTemplate.convertAndSend(queue, msg);
        return "success";
    }

    /**
     * topic 模式
     * @param msg 消息
     * @return
     */
    @PostMapping("/topic/{msg}")
    public String sendTopic(@PathVariable("msg") String msg) {
        ActiveMQTopic topic = new ActiveMQTopic("test");
        jmsMessagingTemplate.convertAndSend(topic, msg);
        return "success";
    }
}

消费者

import com.njc.mqconverter.property.KafkaTopicProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ActiveMQConsumer {

    @Autowired
    KafkaTemplate kafkaTemplate;

    /**
     * queue模式的消费者
     */
    @JmsListener(destination = "testq", containerFactory = "queueListener")
    public void readActiveQueue(String message) {
        String topic = KafkaTopicProperty.getSap();
        kafkaTemplate.send(topic, message);
        log.info("activeMQ 消费者接收:{}", message);
    }

    /**
     * topic 模式的消费者
     */
    @JmsListener(destination = "test", containerFactory = "topicListener")
    public void readActivTopic1(String message) {
        log.info("activeMQ topic 消费者1接收:{}", message);
    }

    /**
     * topic 模式的消费者
     */
    @JmsListener(destination = "test", containerFactory = "topicListener")
    public void readActivTopic2(String message) {
        log.info("activeMQ topic 消费者2接收:{}", message);
    }
}

监听者的队列模式可以不配 containerFactory ,但topic的需要配置

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值