ActiveMQ

安装ActiveMQ到linux

在ActiveMQ官网下载http://activemq.apache.org/

在Linux系统解压。

使用bin目录下的activemq命令启动:

[root@localhost bin]# ./activemq start

关闭:

[root@localhost bin]# ./activemq stop

查看状态:

[root@localhost bin]# ./activemq status

进入管理后台:
http://192.168.85.128:8161/admin
用户名:admin
密码:admin

如果登录不了,看看是不是防火墙拦截了

在这里插入图片描述
开启mq延迟功能

在 安装目录中的conf目录中
修改配置文件activemq.xml
添加属性 schedulerSupport=“true”
在这里插入图片描述

在springboot上的简单的例子

网上找了段代码
添加依赖

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

application.properties

spring.activemq.broker-url=tcp://192.168.126.132:61616
spring.activemq.in-memory=false
#true表示使用连接池,需要加activemq-pool的依赖包
spring.activemq.pool.enabled=false
#连接池最大连接数
spring.activemq.pool.max-connections=5
#空闲的连接过期时间,默认为30秒
spring.activemq.pool.idle-timeout=30000

创建Producer发送消息
ActiveManager.java

import org.apache.activemq.ScheduledMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.*;

@Component
public class ActiveManager {

    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;


    /**
     * @param data
     * @desc 即时发送
     */
    public void send(Destination destination, String data) {
        this.jmsMessagingTemplate.convertAndSend(destination, data);
    }
    /**
     * @desc 延时发送
     */
    public void delaySend(String text, String queueName, Long time) {
        //获取连接工厂
        ConnectionFactory connectionFactory = this.jmsMessagingTemplate.getConnectionFactory();
        try {
            //获取连接
            Connection connection = connectionFactory.createConnection();
            connection.start();
            //获取session
            Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 创建一个消息队列
            Destination destination = session.createQueue(queueName);
            MessageProducer producer = session.createProducer(destination);
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
            TextMessage message = session.createTextMessage(text);
            //设置延迟时间
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
            //发送
            producer.send(message);
            session.commit();
            producer.close();
            session.close();
            connection.close();
        } catch (Exception e) {
            e.getMessage();
        }
    }
}

ProducerRest.java

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;
import java.time.LocalDateTime;

@RestController
@RequestMapping(value = "/test/producer", produces = "application/json")
public class ProducerRest {
    /**
     * 注入ActiveManager
     */
    @Autowired
    ActiveManager activeManager;

    /**
     * 新增消息队列
     */
    @RequestMapping(value = "/add/queue", method = RequestMethod.GET)
    public void addQueue() {
        Destination destination = new ActiveMQQueue("beyondLiQueueTest");
        //传入队列以及值
        activeManager.send(destination, "success");
    }
    /**
     * 新增消息队列(延迟)
     */
    @RequestMapping(value = "/add/delay/queue", method = RequestMethod.GET)
    public void addDelayQueue() {
        System.out.println(LocalDateTime.now());
        activeManager.delaySend("success", "beyondLiDelayQueueTest", 5000L);
    }

}

创建Customer接收消息
CustomerRest.java

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

import java.time.LocalDateTime;

@Service
public class CustomerRest {

    /**
     * 监听方
     */
    //监听注解
    @JmsListener(destination = "beyondLiQueueTest")
    public void getQueue(String info) {
        System.out.println("成功监听beyondLiQueueTest消息队列,传来的值为:" + info);
    }
    //
    @JmsListener(destination = "beyondLiDelayQueueTest")
    public void getDelayQueue(String info) {
        System.out.println("成功监听beyondLiQueueTest消息队列,传来的值为:" + info + "当前时间为" + LocalDateTime.now());
    }

}

调用这两个接口可以看到效果

http://localhost:8080/test/producer/add/delay/queue
http://localhost:8080/test/producer/add/queue

这个例子是点对点消息模式。

消息模式

ActiveMQ传递消息有两种方式

  • Point-to-Point (点对点)消息模式:一个消费者对应一个生产者
  • Publisher/Subscriber(发布/订阅者)消息模式:一个生产者产生消息发送后,可以被多个消费者进行接收

spring boot 默认是点对点模式(Queue),如果要开启发布订阅(Topic)要实用配置

spring.jms.pub-sub-domain=true

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值