high-concurrent-queue
  • Introduction
  • 消息中间件的基本概念
  • ActiveMQ
    • JMS规范
    • JAVA原生客户端
    • 与Spring集成
    • 持久化机制
    • ack机制
    • ActiveMQ修改连接的用户名密码
  • Rabbitmq
    • AMQP规范
    • 安装配置
    • 原生Java客户端使用
    • 与Spring集成
    • ack机制
    • 持久化机制
    • 消息确认进制
      • 消息确认机制(AMQP事务)
      • 消息确认机制(Confirm模式)
    • 延迟队列
      • 消费端限流
      • TTL
      • 死信队列
      • 延迟队列——消息延迟推送
      • 实现延迟任务
    • direct、topic、fanout的使用以及区别
  • 实战场景
    • 系统解耦
    • 定时消息
    • 消峰填谷
    • 分布式事务
    • mq-rpc
    • 消息分发
  • kafka
    • kafka实战
Powered by GitBook
On this page
  • 参考文档:
  • 添加必要依赖
  • application.properties配置
  • 配置生产者和消费者
  • 测试用例

Was this helpful?

  1. ActiveMQ

与Spring集成

PreviousJAVA原生客户端Next持久化机制

Last updated 5 years ago

Was this helpful?

参考文档:

添加必要依赖

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

提供连接或嵌入ActiveMQ实例的必要依赖项,以及与JMS集成的Spring基础结构

application.properties配置

ActiveMQ基础配置

spring.activemq.broker-url=tcp://localhost:8161
spring.activemq.user = admin
spring.activemq.password = admin
spring.activemq.in-memory=true

ActiveMQ配置JMS连接池

spring.activemq.pool.enabled = true #需要添加activemq-pool依赖,false则不需要
spring.activemq.pool.max-connections = 50

org.apache.activemq:activemq-pool并对其进行配置来池化JMS资源 PooledConnectionFactory

org.apache.activemqactivemq-pool5.15.4

配置生产者和消费者

生产者

package com.master.activemq;

import javax.jms.Destination;

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

 /**
 *  @Author tuonioooo
 *  @Date 2018-7-20 11:15
 *  @Info  
 *  @Blog https://blog.csdn.net/tuoni123
 */
@Service("producer")
public class Producer {
    @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
    private JmsMessagingTemplate jmsTemplate;

    @Autowired
    private JmsTemplate jmsTemplate2;
    // 发送消息,destination是发送到的队列,message是待发送的消息
    public void sendMessage(Destination destination, final String message){
        jmsTemplate.convertAndSend(destination, message);
    }
}

消费者

package com.master.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
*  @Author tuonioooo
*  @Date 2018-7-20 11:15
*  @Info
*  @Blog https://blog.csdn.net/tuoni123
*/
@Component
public class Consumer {

    // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
    @JmsListener(destination = "mytest.queue")
    public void receiveQueue(String text) {
        System.out.println("Consumer收到的报文为:" + text);
    }
}

测试用例

package com.master;

import com.master.activemq.Producer;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.jms.Destination;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootActivemqApplicationTests {

    @Autowired
    private Producer producer;

    @Test
    public void contextLoads() throws InterruptedException {
        Destination destination = new ActiveMQQueue("mytest.queue");

        for(int i=0; i<100; i++){
            producer.sendMessage(destination, "myname is chhliu!!!");
        }
    }

}

代码示例:

SpringBoot ActiveMQ Support 官网
Activemq 官网
https://github.com/tuonioooo?tab=repositories