SpringBoot 整合 Apache ActiveMQ

SpringBoot 整合 Apache ActiveMQ

1. pom.xml 添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2. 修改application.yml

在这个文件中加入ActiveMQ的相关配置

spring:
  activemq:
    broker-url: tcp://192.168.44.129:61616
    user: admin
    password: admin
3. 创建消息生产者Producer
package cn.tyrone.springboot.integrate.activemq;

import javax.jms.Queue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * 消息生产者 
 *
 */
@Component
public class Producer {

    /*
     * Jms消息模板
     */
    @Autowired private JmsMessagingTemplate jmsMessagingTemplate;

    /*
     * 队列
     */
    @Autowired private Queue queue;

    /*
     * 发送消息
     */
    public void send(String msg){
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }
}
4. 创建消息消费者Consumer
package cn.tyrone.springboot.integrate.activemq;

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

@Component
public class Consumer {

    @JmsListener(destination = "sample.queue")
    public void receiveMessge(String text){
        System.out.println("发送的消息:\t" + text);
    }
}
5. 创建SpringBoot启动类
package cn.tyrone.springboot.integrate.activemq;

import javax.jms.Queue;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;

@SpringBootApplication
@EnableJms
public class Application implements CommandLineRunner {

    /*
     * 声明一个消息队列
     */
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("sample.queue");
    }

    /*
     * 注入消息生产者
     */
    @Autowired private Producer Producer;


    @Override
    public void run(String... args) throws Exception {
        Producer.send("Hello! SpringBoot integrate Apache ActiveMQ!!!");
        System.out.println("发送消息结束!!!");

    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

运行启动类后,查看控制台日志:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)

2017-07-20 15:02:27.237  INFO 3796 --- [           main] c.t.s.integrate.activemq.Application     : Starting Application on EZ-20161228YDGX with PID 3796 (D:\tyrone-workspace\springboot-study\tyrone-springboot-integrate-activemq\target\classes started by Administrator in D:\tyrone-workspace\springboot-study\tyrone-springboot-integrate-activemq)
2017-07-20 15:02:27.242  INFO 3796 --- [           main] c.t.s.integrate.activemq.Application     : No active profile set, falling back to default profiles: default
2017-07-20 15:02:27.310  INFO 3796 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1f0f1111: startup date [Thu Jul 20 15:02:27 CST 2017]; root of context hierarchy
2017-07-20 15:02:28.801  INFO 3796 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-07-20 15:02:28.807  INFO 3796 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
发送消息开始!!!
----接收消息开始 ----
接收的消息内容:    Hello! SpringBoot integrate Apache ActiveMQ!!!
----接收消息结束 ----
发送消息结束!!!
2017-07-20 15:02:29.100  INFO 3796 --- [           main] c.t.s.integrate.activemq.Application     : Started Application in 2.316 seconds (JVM running for 3.106)

自此,SpringBoot整合Apache ActiveMQ结束!

参考链接:https://github.com/myNameIssls/spring-boot/tree/master/spring-boot-samples
源代码链接:https://github.com/myNameIssls/springboot-study/tree/master/tyrone-springboot-integrate-activemq

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值