spring boot整合activemq实现MQ的使用-java编程中

位置:首页>文章>详情   分类: 教程分享 > Java教程   阅读(5039)   2023-03-28 11:29:14

一.去activemq官网下载mq软件

去Apache官网下载activemq软件,并安装。

二.编写Java代码

java编程中spring boot整合activemq实现MQ的使用
1.在spring boot项目中添加activemq的依赖
<!-- activemq support -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
<!-- /activemq support -->
2.application.properties配置文件中添加mq的相关配置
#==================activemq Config Start==================
spring.activemq.broker-url=tcp://127.0.0.1:61616
spring.activemq.in-memory=true
spring.activemq.password=admin
spring.activemq.user=admin
spring.activemq.packages.trust-all=false
spring.activemq.packages.trusted=
spring.activemq.pool.configuration.*=
spring.activemq.pool.enabled=false
spring.activemq.pool.expiry-timeout=0
spring.activemq.pool.idle-timeout=30000
spring.activemq.pool.max-connections=1
#==================activemq Config End  ==================

3.消息提供者
package com.htwl.collection.cqyth.amq;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * MQ消息提供者
 * 
 * @author leftso
 *
 */
@Component
@EnableScheduling // 测试用启用任务调度
public class Provider {
	/** MQ jms实例 **/
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;

	private static int count = 0;

	@Scheduled(fixedDelay = 5000) // 每5s执行1次-测试使用
	public void send() {

		Destination destination = new ActiveMQQueue("TEST_QUEUE_LOG");// 这里定义了Queue的key
		String message = "Send AMQ Test ..." + count;
		System.out.println("[" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()) + "]" + message);
		count++;
		this.jmsMessagingTemplate.convertAndSend(destination, message);
	}
}
4.消费
package com.htwl.collection.cqyth.amq;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

	Logger LOGGER = LoggerFactory.getLogger(this.getClass());

	/**
	 * 使用@JmsListener注解来监听指定的某个队列内的消息
	 **/
	@JmsListener(destination = "TEST_QUEUE_LOG")
	public void removeMessage(String msg) {
		System.out.println("["+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date())+"]Receive:"+msg);
	}
}
 
地址:https://www.leftso.com/article/94.html

相关阅读

spring boot整合activemq。本博客将通过一个简单的例子讲解在spring boot中activemq如何作为消费者(Consumer )和如何在spring boot中消息提供者...
       习使用嵌入式ActiveMQ配置Spring Boot应用程序,以便在JMSTemplate 的帮助下发送和接收JMS消息
Java编程之Spring Boot通过JMSTemplate 整合ActiveMQ
java编程中spring框架5.0介绍说明/概述,spring5,spring框架,java编程
Java编程中发邮件也是常用的。但是原生的jdk自带的发送邮件用起来还是比较麻烦的。spring框架在Java语言中完全是神一样的存在,通过spring框架的邮件工具来发送邮件就非常方便了,本文...
java编程为啥会出现spring框架,为什么要有Spring?
Java编程中Spring Boot整合RabbitMQ实现消息中间件RabbitMQ的使用
Java编程中spring boot项目如何获取spring容器applicationContext
Spring框架中,可以在6个内置的Scope中创建bean,也可以定义自定义范围。 在这六个范围中,只有在使用Web感知的ApplicationContext时才有四个范围可用。singlet...
Java编程之Spring Cloud Hystrix Circuit熔断/断路