栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot 使用 ActiveMQ

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot 使用 ActiveMQ

pom文件添加依赖


    org.springframework.boot
    spring-boot-starter-activemq



    org.apache.activemq
    activemq-pool
    5.17.1

配置 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);
    }

    
    @Bean("queueListener")
    public JmsListenerContainerFactory queueJmsListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(false);
        return factory;
    }

    
    @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 {

    
    @PostMapping("/queue/{msg}")
    public String sendQueue(@PathVariable("msg") String msg) {
        ActiveMQQueue queue = new ActiveMQQueue("testq");
        jmsMessagingTemplate.convertAndSend(queue, msg);
        return "success";
    }

    
    @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;

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

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

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/959165.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号