Java异步消息 JMS AMQP RabbitMQ和ActiveMQ(一)

1. 与其他系统进行集成,需要将消息发送给其他系统,Java引入了JMS(Java Message Server 即Java消息服务);

2. JMS分为点对点(Point to Point)和发布订阅(Publish/Subscribe)两种形式。发布订阅模式,可以进行更多的扩展,使得更多的系统能监控得到消息。

3. 实际工作中实现JMS服务规范的很多,有常用传统的ActiveMQ、分布式的Kafka、还有实现AMQP协议(Advanced Mesage Queuing Protocol)的RabbitMQ等。

4. ActiveMQ:去http://activemq.apache.org/components/classic/download/http://activemq.apache.org/download.html下载ActiveMQ压缩安装包。解压-->打开bin目录,根据windows系统位数,进入相应目录,找到activemq.bat,即可启动ActiveMQ。Linux系统启动activemq.sh。

5. 访问http://127.0.0.1:8161/,输入admin,admin,即可登录。

6. 项目配置:

    6.1 为了支持activemq,需要引入依赖:

  // https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-activemq
  compile group: 'org.springframework.boot', name: 'spring-boot-starter-activemq', version: '2.3.2.RELEASE'
  // https://mvnrepository.com/artifact/org.apache.activemq/activemq-pool
  compile group: 'org.apache.activemq', name: 'activemq-pool', version: '5.16.0'

    引入activemq的starter依赖,这样便可以通过application.properties文件配置关于activemq的内容;引入了activemq-pool的依            赖,便可以使用连接池机制。

     6.2 配置application.properties: 

spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
    pool:
      enabled: true
      max-connections: 50
  jms:
    pub-sub-domain: true
    template:
      default-destination: activemq.default.destination

     6.3 通过JmsTemplate这个模板对象来处理消息的发送或接收,spring 4.1 之后提供注解@JmsListener简化开发工作。因此需要定义一个接口,来处理JMS的消息接收和发送。

     6.4 添加了服务接口后,配置activemq信任User类:

     6.5 如果遇到:错误: 程序包com.jianxin.learnbase.mq.service不存在错误,可以尝试在 不存在的模块的build.gradle中加上jar.enabled=true和bootJar.enabled=false,尝试解决。

     6.6 如果报错:No qualifying bean of type 'org.springframework.jms.core.JmsTemplate' available。

<dependency>
    <groupId>org.messaginghub</groupId>
    <artifactId>pooled-jms</artifactId>
</dependency>
<!-- 去掉原来的连接池依赖
 <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
</dependency> 

可能是springboot版本变更,需要变更引入jar包:https://www.jianshu.com/p/bb396bf573bd 

6.7 项目详见:https://gitee.com/manongfaner/gradle-mulit-learn2

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值