代码编织梦想

ActiveMQ整合SpringBoot

pom.xml

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-activemq</artifactId>
		<version>1.5.0.RELEASE</version>
	</dependency>
	<!--消息队列连接池-->
	<dependency>
		<groupId>org.apache.activemq</groupId>
		<artifactId>activemq-pool</artifactId>
		<version>5.15.0</version>
	</dependency>

application.yml配置文件

spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin
close-timeout: 15s # 在考虑结束之前等待的时间
in-memory: true # 默认代理URL是否应该在内存中。如果指定了显式代理,则忽略此值。
non-blocking-redelivery: false # 是否在回滚回滚消息之前停止消息传递。这意味着当启用此命令时,消息顺序不会被保留。
send-timeout: 0 # 等待消息发送响应的时间。设置为0等待永远。
queue-name: active.queue
topic-name: active.topic.name.model
pool:
enabled: true
max-connections: 10 #连接池最大连接数
idle-timeout: 30000 #空闲的连接过期时间,默认为30秒
在这里插入图片描述

ActiveMQ结构

在这里插入图片描述

ActiveConfig

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
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 org.springframework.jms.core.JmsMessagingTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
public class ActiveConfig {

@Value("${spring.activemq.broker-url}")
private String brokerUrl;

@Value("${spring.activemq.user}")
private String username;

@Value("${spring.activemq.topic-name}")
private String password;

@Value("${spring.activemq.queue-name}")
private String queueName;

@Value("${spring.activemq.topic-name}")
private String topicName;

@Bean(name = "queue")
public Queue queue() {
    return new ActiveMQQueue(queueName);
}

@Bean(name = "topic")
public Topic topic() {
    return new ActiveMQTopic(topicName);
}

@Bean
public ConnectionFactory connectionFactory(){
    return new ActiveMQConnectionFactory(username, password, brokerUrl);
}

@Bean
public JmsMessagingTemplate jmsMessageTemplate(){
    return new JmsMessagingTemplate(connectionFactory());
}

// 在Queue模式中,对消息的监听需要对containerFactory进行配置
@Bean("queueListener")
public JmsListenerContainerFactory<?> queueJmsListenerContainerFactory(ConnectionFactory connectionFactory){
    SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setPubSubDomain(false);
    return factory;
}

//在Topic模式中,对消息的监听需要对containerFactory进行配置
@Bean("topicListener")
public JmsListenerContainerFactory<?> topicJmsListenerContainerFactory(ConnectionFactory connectionFactory){
    SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setPubSubDomain(true);
    return factory;
}

}

ProducerController

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;

/**

  • 生产者
    */
    @RestController
    public class ProducerController
    {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    @PostMapping("/queue/test")
    public String sendQueue(@RequestBody String str) {
    this.sendMessage(this.queue, str);
    return “success”;
    }

    @PostMapping("/topic/test")
    public String sendTopic(@RequestBody String str) {
    this.sendMessage(this.topic, str);
    return “success”;
    }

    // 发送消息,destination是发送到的队列,message是待发送的消息
    private void sendMessage(Destination destination, final String message){
    jmsMessagingTemplate.convertAndSend(destination, message);
    }
    }

queue模式的消费者 QueueConsumerListener

import com.alibaba.fastjson.JSONObject;
import com.jeesite.modules.alarm.service.BizAlarmService;
import com.jeesite.modules.influx.InfluxDBService;
import com.jeesite.modules.mqtt.config.MqttData;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class QueueConsumerListener {

@Resource
private InfluxDBService influxDBService;

@Resource
private BizAlarmService bizAlarmService;

//queue模式的消费者
@JmsListener(destination="${spring.activemq.queue-name}", containerFactory="queueListener", concurrency = "4-8")
public void readActiveQueue(String message) {
    MqttData mqttData= JSONObject.parseObject(message, MqttData.class);
    influxDBService.save(mqttData);
}

}

topic模式的消费者 TopicConsumerListener

import com.alibaba.fastjson.JSONObject;
import com.jeesite.modules.alarm.service.BizAlarmService;
import com.jeesite.modules.mqtt.config.MqttData;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class TopicConsumerListener {

@Resource
private BizAlarmService bizAlarmService;

//topic模式的消费者
@JmsListener(destination="${spring.activemq.topic-name}", containerFactory="topicListener", concurrency = "4-8")
public void readActiveQueue(String message) {
    MqttData mqttData= JSONObject.parseObject(message, MqttData.class);
    String choFaultCode = mqttData.getChoFaultCode();//故障码
    if (!"0".equals(choFaultCode)){
        bizAlarmService.addAlarm(choFaultCode,mqttData.getSn());
    }
}

}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/weixin_47638656/article/details/110859578

Connection request timed out与Adding the specified count to the semaphore would cause it to excee问题排除-爱代码爱编程

一,问题描述 C#开发的控制系统、Oracle数据库,在00:13用户反馈系统宕机,连上服务器查看,发现日志不再打印,准备重启服务,但重启失败,服务一直在Stopping中无法中止,无奈之下重启服务器后正常。 二,问题排除: 查看控制系统日志,发现从前一天的22:15:10,开始断断续续出现数据库连接失败的错误。到00:04时,彻底无法连接,服务假死。

ActiveMQ 代码案例-爱代码爱编程

一、原生API pom依赖 生产者 package cn.enjoyedu.usemq; import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /** *类说明:

深度报告(69页附下载)| 蜂窝车联网(C-V2X)技术与产业发展态势前沿报告2020-中国通信学会-爱代码爱编程

建议PC端阅读,下载完整PDF全文, 请关注本公众号后台回复“ 201207 ” 下载

MQ 系列之 ActiveMQ 可靠性-爱代码爱编程

说到 ActiveMQ 可靠性不可不提持久性、事务以及签收,正是这三个保证了单机版 ActiveMQ 的可靠性 1.1 持久性 1.1.1 非持久 ☞ 概述 所谓非持久化就是在 ActiveMQ 凉凉之后,消息不会被保留下来。 ☞ 示例 /** * Created with IntelliJ IDEA. * * @author

ActiveMQ 安装-爱代码爱编程

下载ActiveMQ 进入http://activemq.apache.org/ 安装环境 1、需要jdk 2、安装Linux系统。生产环境都是Linux系统。 安装步骤 第一步: 把ActiveMQ 的压缩包上传到Linux系统。 第二步:解压缩。 第三步:启动。 使用bin目录下的activemq命令启动: [root@localhost b

ActiveMQ 的使用方法-爱代码爱编程

原理图 Queue Producer 生产者:生产消息,发送端。 把jar包添加到工程中。使用5.11.2版本的jar包。 第一步:创建ConnectionFactory对象,需要指定服务端ip及端口号。 第二步:使用ConnectionFactory对象创建一个Connection对象。 第三步:开启连接,调用Connection对象的star