springboot集成activemq的实例代码

2025-05-29 0 57

ActiveMQ

ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位。

特性

  1. 多种语言和协议编写客户端。语言: Java,C,C++,C#,Ruby,Perl,Python,PHP。应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP
  2. 完全支持JMS1.1和J2EE 1.4规范 (持久化,XA消息,事务)
  3. 对Spring的支持,ActiveMQ可以很容易内嵌到使用Spring的系统里面去,而且也支持Spring2.0的特性
  4. 通过了常见J2EE服务器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的测试,其中通过JCA 1.5 resource adaptors的配置,可以让ActiveMQ可以自动的部署到任何兼容J2EE 1.4 商业服务器上
  5. 支持多种传送协议:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
  6. 支持通过JDBC和journal提供高速的消息持久化
  7. 从设计上保证了高性能的集群,客户端-服务器,点对点
  8. 支持Ajax
  9. 支持与Axis的整合
  10. 可以很容易的调用内嵌JMS provider,进行测试

更多关于 ActiveMQ 的内容可以点击这里。

Spring-Boot 集成 ActiveMQ

添加maven依赖

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
<!--

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-activemq</artifactId>

</dependency>

-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

</dependency>

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-client</artifactId>

</dependency>

没有直接使用注释的依赖,是因为其含有如下依赖

?

1

2

3

4
<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-broker</artifactId>

</dependency>

而它的作用是什么呢,会在程序中直接内嵌 ActivityMQ,也就是说不需要安装 ActiveMQ,但是这个如果服务宕机了,内嵌的 ActiveMQ 也就没了。关键,这个内嵌的 ActiveMQ 而无法看到图形化界面,所以这里没有直接使用注释里的依赖。

在application.properties中增加如下配置

?

1

2

3

4

5

6
# activemq

spring.activemq.broker-url=tcp://localhost:61616

spring.activemq.user=admin

spring.activemq.password=admin

spring.activemq.in-memory=true

spring.activemq.pool.enabled=false

这里对 ActiveMQ 的端口做一个简短说明,61616为消息代理接口 ,8161 为管理界面

JAVA代码实现

定义QUEUE

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
package com.activemq.queue;

import org.apache.activemq.command.ActiveMQQueue;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import javax.jms.Queue;

@Configuration

public class QueueConfig {

@Bean

public Queue logQueue() {

return new ActiveMQQueue(QueueName.LOG_QUEUE);

}

}

消息生产者

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32
package com.activemq.producer;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.CommandLineRunner;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.stereotype.Component;

import javax.jms.Queue;

@Component

public class LogProducer implements CommandLineRunner {

private static final Logger LOGGER = LoggerFactory.getLogger(LogProducer.class);

@Autowired

private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired

private Queue logQueue;

@Override

public void run(String... strings) throws Exception {

send("This is a log message.");

LOGGER.info("Log Message was sent to the Queue named sample.log");

}

public void send(String msg) {

this.jmsMessagingTemplate.convertAndSend(this.logQueue, msg);

}

}

消息消费者

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
package com.activemq.consumer;

import com.activemq.queue.QueueName;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;

@Component

public class LogConsumer {

private static final Logger LOGGER = LoggerFactory.getLogger(LogConsumer.class);

@JmsListener(destination = QueueName.LOG_QUEUE)

public void receivedQueue(String msg) {

LOGGER.info("Has received from " + QueueName.LOG_QUEUE + ", msg: " + msg);

}

}

测试接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
@Autowired

private LogProducer logProducer;

@GetMapping("/activemq/send")

public String activemq(HttpServletRequest request, String msg) {

msg = StringUtils.isEmpty(msg) ? "This is Empty Msg." : msg;

try {

logProducer.send(msg);

} catch (Exception e) {

e.printStackTrace();

}

return "Activemq has sent OK.";

}

启动类

增加如下注解@EnableJms

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
@Configuration//配置控制

@EnableAutoConfiguration//启用自动配置

@ComponentScan//组件扫描

@EnableConfigurationProperties({EmailProp.class})

@EnableJms

public class Bootstrap {

private static final Logger LOGGER = LoggerFactory

.getLogger(Bootstrap.class);

public static void main(String[] args) throws Exception {

SpringApplication.run(Bootstrap.class, args);

LOGGER.info("Server running...");

}

}

测试

运行服务,在浏览器输入 http://127.0.0.1:8080/activemq/send?msg=test%20log,会在控制台看到如下输出

?

1

2
INFO 1498 --- [enerContainer-1] c.j.a.activemq.consumer.LogConsumer : Has received from sample.log, msg: test log

[DefaultMessageListenerContainer-1] INFO c.j.a.activemq.consumer.LogConsumer - Has received from sample.log, msg: test log

打开 ActiveMQ 的管理页面,用户名密码都是admin,可以看到如下信息

springboot集成activemq的实例代码

官方示例:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-activemq

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://blog.csdn.net/zl18310999566/article/details/54313464

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 springboot集成activemq的实例代码 https://www.kuaiidc.com/117163.html

相关文章

发表评论
暂无评论