`

SpringBoot-第十三章 SpringBoot整合ActiveMQ

 
阅读更多

 

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

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

 

在调试过程中红色参数一定要设置为false。测试成true会报错。一直没有找到原因,TBD。

spring.activemq.broker-url=tcp://xxx.xxx.xxx.xxx:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=false
spring.activemq.pool.max-connections=100

 

启动类添加@EnableJms注解。JmsListenerContainerFactory用于支持既能订阅有能点对点。最后一段代码中用到

@EnableJms
……
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerFactoryTopic(ConnectionFactory activeMQConnectionFacotry)
{
   DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
   bean.setPubSubDomain(true);
   bean.setConnectionFactory(activeMQConnectionFacotry);
   return bean;
}

 

public interface ProducterService {
    public void sendMessage(Destination destination, final String message);
    public void sendMessage(final String message);
    public void publish(final String msg);
}
@Service
public class ProducterServiceImpl implements ProducterService {
    @Autowired
private Queue queue;
    @Autowired
private JmsMessagingTemplate jmsTemplate;
    @Override
public void sendMessage(final String message)
    {
        jmsTemplate.convertAndSend(this.queue,message);
    }
    @Override
public void sendMessage(Destination destination, final String message)
    {
        jmsTemplate.convertAndSend(destination,message);
    }
    @Autowired
private Topic topic;
    @Override
public void publish(String msg)
    {
        jmsTemplate.convertAndSend(this.topic,msg);
    }
}

 

@RestController
@RequestMapping("/order")
public class OrderMQController {

    @Autowired
private ProducterService producter;

    @RequestMapping("/done")
    public Object order (String msg)
    {
         producter.sendMessage(msg);
         return "success";
    }
    @RequestMapping("/done1")
    public Object order1 (String msg)
    {
        Destination destination = new ActiveMQQueue("order.queue");
        producter.sendMessage(destination,msg);
        return "success";
    }

    @RequestMapping("/publish")
    public Object publishOrder(String msg)
    {
        producter.publish(msg);
        return "success";
    }
}

 

@Component
public class OrderConsumer {
    @JmsListener(destination = "order.queue")
    public void receiveQueue(String text)
    {
        System.out.println("OrderConsumers收到:"+text);
    }
}

 

@Component
public class TopicConsumer {
// @JmsListener如果不指定独立的containerFactory的话只能支持一直模式:或者是点对点,或者是消息订阅
@JmsListener(destination = "kevin.topic", containerFactory="jmsListenerContainerFactoryTopic")
    public void receiveTopic1(String text)
    {
        System.out.println("TopicConsumers1收到:"+text);
    }
    @JmsListener(destination = "kevin.topic", containerFactory="jmsListenerContainerFactoryTopic")
    public void receiveTopic2(String text)
    {
        System.out.println("TopicConsumers2收到:"+text);
    }
    @JmsListener(destination = "kevin.topic")
    public void receiveTopic3(String text)
    {
        System.out.println("TopicConsumers3收到:"+text);
    }
}

 

@Component
public class TopicConsumer {
// @JmsListener如果不指定独立的containerFactory的话只能支持一直模式:或者是点对点,或者是消息订阅
@JmsListener(destination = "kevin.topic", containerFactory="jmsListenerContainerFactoryTopic")
    public void receiveTopic1(String text)
    {
        System.out.println("TopicConsumers1收到:"+text);
    }
    @JmsListener(destination = "kevin.topic", containerFactory="jmsListenerContainerFactoryTopic")
    public void receiveTopic2(String text)
    {
        System.out.println("TopicConsumers2收到:"+text);
    }
    @JmsListener(destination = "kevin.topic")
    public void receiveTopic3(String text)
    {
        System.out.println("TopicConsumers3收到:"+text);
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

    springboot-activemq.zip

    SpringBoot与ActiveMQ的整合是Java开发中常见的一项任务,特别是在构建分布式系统时,消息队列作为解耦组件起着至关重要的作用。本资源提供的"springboot-activemq.zip"是一个示例项目,用于演示如何在SpringBoot...

    springboot-nettysocketio +netty+activeMq在线客服系统

    springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot +netty+activeMq在线客服系统springboot...

    springboot集成activemq实现消息接收demo

    而ActiveMQ是Apache出品的一款开源消息中间件,它遵循JMS(Java Message Service)规范,用于处理应用程序之间的异步通信。本教程将详细介绍如何在Spring Boot项目中集成ActiveMQ,实现消息接收的Demo。 首先,我们...

    阿里云java短信验证码源码-springboot-learning-3:springboot框架相关技术的案例项目及使用springboot

    SpringBoot整合框架各种实用的组件技术点以及一些框架重要技术点的项目案例的实现,纯属个人技术积累和框架学习,有缺漏之处请指出。 主体版本号 java v1.8 springboot v2.0.5.RELEASE 各技术点预览目录 组件名称...

    apache-activemq-5.10到apache-activemq6.1大版本合集

    activemq-parent-5.10.0-source-release.zip activemq-parent-5.10.2-source-release.zip activemq-parent-5.12.3-source-release.zip apache-activemq-5.10.2-bin.tar.gz apache-activemq-5.11.4-bin.zip apache-...

    springboot + activemq 整合

    springboot + activemq 整合 ,其中springboot-activemq-consumer 是消费端,springboot-activemq-producer 生成者 ,启动生成者,会默认生成100个消息,启动消费端,会消费生产的100个消息

    springboot-learning-1:springboot框架相关技术的案例项目及使用springboot框架时的需注意点

    springboot-learning-1 springboot框架相关技术的案例项目及使用springboot框架时的需注意点 项目介绍 SpringBoot集成框架各种实用的组件技术点以及一些框架重要技术点的项目案例的实现,纯属个人... SpringBoot整合E

    apache-activemq-5.9.0-bin

    Apache ActiveMQ是世界上最流行的开源消息代理和队列服务器,它基于Java Message Service(JMS)规范,为分布式系统提供高效、可靠和可扩展的消息传递功能。这个“apache-activemq-5.9.0-bin”压缩包包含了Apache ...

    Springboot-activeMQ

    ActiveMQ是Apache下的开源项目,完全支持JMS1.1和J2EE1.4规范的JMS Provider实现。  由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行。 activemq的queue和topic  JMS中定义...

    springboot-dubbo.rar

    添加基于消息的最终一致性分布式事务解决方法+springboot整合rocketmq 四期: 缓存和mysql数据一致性问题的解决方案+redis分布式锁解决缓存雪崩和缓存击穿的问题 # 部署: 先启动提供者,再启动web消费者 java -...

    apache-activemq-5.8.0-bin.zip

    Apache ActiveMQ是Apache软件基金会开发的一个开源消息中间件,它基于Java Message Service(JMS)规范,用于在分布式系统中高效地传输数据。这个压缩包"apache-activemq-5.8.0-bin.zip"包含了ActiveMQ 5.8.0版本的...

    SpringBoot+ActiveMq+MQTT实现消息的发送和接收

    在本文中,我们将深入探讨如何使用SpringBoot、ActiveMQ和MQTT来实现消息的发送与接收。这是一个典型的分布式系统中的消息通信场景,其中SpringBoot作为应用程序框架,ActiveMQ作为消息中间件,而MQTT(Message ...

    springboot整合activemq 消费者 ACK手动确认 &消息重发

    springboot整合 activeMq 消费者 消费接收消息 包含队列模式点对点发 以及 主题模式一对多 这是消费者的demo consumer 。 里面有消息重发机制,手动确认ACK模式。 配合 producer 生产者demo使用。

    Springboot和ActiveMQ的整合实例

    在IT行业中,Spring Boot和ActiveMQ的整合是一个常见的任务,特别是在构建分布式系统和微服务架构时。Spring Boot以其简化配置和快速开发的特性受到广泛欢迎,而ActiveMQ作为一款开源的消息中间件,提供了消息队列...

    apache-activemq-5.5.1-bin.zip加上入门demo

    解压缩apache-activemq-5.5.1-bin.zip,然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。 包含了apache-activemq-5.5.1-bin.zip以及ActiveMQ一个helloworld的demo启动ActiveMQ以后,登陆:...

    activemq-cpp-library-3.9.5-src.zip

    《ActiveMQ-CPP库3.9.5源代码解析与应用》 ActiveMQ-CPP库是Apache ActiveMQ项目的一部分,它提供了一套C++接口,用于与ActiveMQ消息代理进行通信。这个库允许开发者在C++应用程序中实现高级消息队列协议(AMQP)和...

    java springboot整合activemq工程

    java springboot整合activemq工程 #activemq配置 #默认情况下activemq提供的是queue模式 true是可以使用topic,false是仅使用queue模式 spring.jms.pub-sub-domain: true # 设置连接的activemq服务器 spring....

    spring boot 实践学习案例,与其它组件整合

    spring boot 实践学习案例,与其它组件结合如 mybatis、jpa、dubbo、redis、mongodb、memcached、kafka、rabbitmq、activemq、elasticsearch、security、shiro等 #### Spring Boot 版本 - 2.0.3.RELEASE #### 模块...

Global site tag (gtag.js) - Google Analytics