activeMQ-springboot整合

爱海
2023-12-01

activeMQ和springboot整合

pom文件,双方的基本配置相同:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.anlysqx</groupId>
  <artifactId>activeMQ-producer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
  </parent>
  
  <properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
  
  
  <dependencies>
  
  	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!-- spring boot web支持:mvc,aop... -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-activemq</artifactId>
		</dependency>
  	
  </dependencies>
  <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
  
  
  
</project>

然后是application.yml

spring:
  activemq:
    broker-url: tcp://127.0.0.1:61616
    user: admin
    password: admin
queue: anlysqx
server:
 port: 8888

两个只要保证port不同即可

producer一方:

QueueConfig.java

package com.anlysqx.producer;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueueConfig {
	
	@Value("${queue}")
	private String queue;
	
	@Bean
	public Queue logQueue(){
		return new ActiveMQQueue(queue);
	}

}

MyProducer.java

package com.anlysqx.producer;

import java.util.Date;

import javax.jms.Queue;

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;

@Component
@EnableScheduling
public class MqProducer {
	
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;
	
	@Autowired
	private Queue queue;
	
	
	@Scheduled(fixedDelay=5000)
	public void send(){
		String mes = "生产者生产消息:"+new Date().toString();
		jmsMessagingTemplate.convertAndSend(queue,mes);
		System.out.println(mes);
	}
	
}

App.java两个相同;

package com.anlysqx.producer;

import java.util.Date;

import javax.jms.Queue;

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;

@Component
@EnableScheduling
public class MqProducer {
	
	@Autowired
	private JmsMessagingTemplate jmsMessagingTemplate;
	
	@Autowired
	private Queue queue;
	
	
	@Scheduled(fixedDelay=5000)
	public void send(){
		String mes = "生产者生产消息:"+new Date().toString();
		jmsMessagingTemplate.convertAndSend(queue,mes);
		System.out.println(mes);
	}
	
}

Consumer方面只需要提供一个consumer.java即可:

package com.anlysqx.consumer;

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

@Component
public class MqConsumer {
	
	@JmsListener(destination="${queue}")
	private void receive(String msg){
		System.out.println("监听器监听到: msg = "+msg);
	}
	
}

使用注解:@JmsListener(destination="${queue}") 来标识使用的queue名称。其他的根据配置自动装配。

 

 

 

 

 类似资料: