准备工作
下载:http://activemq.apache.org/activemq-5152-release.html
解压 运行bin目录下的activemq.bat(根据Windows 64bit还是32bit 选择启动)
启动成功后 访问 http://127.0.0.1:8161/ 进入ActiveMQ的管理页面, 默认用户名/密码: admin/admin
1. 创建一个spring boot工程,添加activeMQ starter,
compile group: 'org.springframework.boot', name: 'spring-boot-starter-activemq', version: '1.5.9.RELEASE'
注意: 添加starter以后,就可以删除spring-boot-starter
build.gradle
buildscript {
ext {
springBootVersion = '2.0.0.M7'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.zheting.it.activemq'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
//compile('org.springframework.boot:spring-boot-starter') 注意添加其他starter,就不需要spring-boot-starter
compile group: 'org.springframework.boot', name: 'spring-boot-starter-activemq', version: '1.5.9.RELEASE'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
2.0 创建生产者
package com.zheting.it.activemq.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;
import javax.jms.Queue;
/**
* created by zheTing on 2018-01-24 10:51
*/
@Component
public class MessageCreator implements CommandLineRunner {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private Queue queue;
@Override
public void run(String... arg0) throws Exception {
// This will put text message to queue
this.jmsTemplate.convertAndSend(this.queue, "Hello Spring Boot ActiveMQ");
System.out.println("Message has been put to queue by sender");
}
}
3.0 创建消费者
package com.zheting.it.activemq.demo;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
/**
* created by zheTing on 2018-01-24 10:53
*/
@Component
public class MessageReceiver {
@JmsListener(destination = "first.queue")
public void receiveQueue(String text) {
System.out.println("Message Received: " + text);
}
}
@JMSListner:This Annotation is actually used to mark a method to be the target of a JMS message listener on the specified destination().
4.0 Spring Boot 程序入口
package com.zheting.it.activemq.demo;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import javax.jms.Queue;
/**
* @EnableJMS annotation is used to trigger search for method annotated with @JMSListener, hence to create JMS listener in the background.
*/
@SpringBootApplication
@EnableJms
public class SpringbootActivemqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootActivemqApplication.class, args);
}
/**
* 初始化一个队列, 队列名称为 first.queue
*/
@Bean
public Queue queue() {
return new ActiveMQQueue("first.queue");
}
}
@EnableJMS annotation is used to trigger search for method annotated with @JMSListener, hence to create JMS listener in the background.
PS: application.properties中配置ActiveMQ的参数,具体参考源码配置
参考文章:
https://spring.io/guides/gs/messaging-jms/
https://springframework.guru/spring-boot-example-of-spring-integration-and-activemq/
https://java2blog.com/spring-boot-activemq-example/#external_activemq
https://memorynotfound.com/spring-boot-embedded-activemq-configuration-example/