您的当前位置:首页正文

Spring Boot 整合 ActiveMQ

来源:花图问答

准备工作

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 
import org.springframework.jms.core.JmsTemplate;
import 

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 

/**
 * 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 
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的参数,具体参考源码配置