学习activemq(2)Spring boot整合activemq jms
来源:互联网 发布:linux 线程等待 编辑:程序博客网 时间:2024/06/14 16:12
JMS(JAVA Message Service,java消息服务)API是一个消息服务的标准或者说是规范,允许应用程序组件基于JavaEE平台创建、发送、接收和读取消息。它使分布式通信耦合度更低,消息服务更加可靠以及异步性。
消息模型
即“点对点”和“发布订阅”
Point-to-Point(P2P)Publish/Subscribe(Pub/Sub)
Point-to-Point(P2P)
Publish/Subscribe(Pub/Sub)
1. quickstart
例子地址
pom.xml依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
application.properties
spring.activemq.broker-url=tcp://123.56.78.70:61616spring.activemq.user=admin spring.activemq.password=admin spring.activemq.in-memory=true spring.activemq.pool.enabled=false
producer
/** * 源代码地址 https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-activemq * @date 2017年10月19日 下午5:19:24 */@Componentpublic class Producer implements CommandLineRunner { @Bean public Queue queue() { return new ActiveMQQueue("sample.queue"); } @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; @Override public void run(String... args) throws Exception { for (int i = 0; i < 10; i++) { send("hullo boot activemq" + i); } System.out.println("Message was sent to the Queue"); } public void send(String msg) { this.jmsMessagingTemplate.convertAndSend(this.queue, msg); }}
@Componentpublic class Consumer { @JmsListener(destination = "sample.queue") public void receiveQueue(String text) { LogCore.BASE.info("收到的信息为{}", text); }}
2. Spring官方例子
地址http://spring.io/guides/gs/messaging-jms/
Mail.java
public class Email { private String to; private String body; public Email() { } public Email(String to, String body) { this.to = to; this.body = body; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } @Override public String toString() { return String.format("Email{to=%s, body=%s}", getTo(), getBody()); }}
Receiver.java
import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;import com.sonic.activemq.LogCore;@Componentpublic class Receiver { @JmsListener(destination = "mailbox", containerFactory = "myFactory") public void receiveMessage(Email email) { System.out.println("Received <" + email + ">"); LogCore.BASE.info("rsv msg:{}", email); }}
JMSTest.java
@Configurationpublic class JMSTest implements CommandLineRunner { @Bean public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) { DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory(); // This provides all boot's default to this factory, including the message converter configurer.configure(factory, connectionFactory); // You could still override some of Boot's default if necessary. return factory; } @Bean // Serialize message content to json using TextMessage public MessageConverter jacksonJmsMessageConverter() { MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); converter.setTargetType(MessageType.TEXT); converter.setTypeIdPropertyName("_type"); return converter; } @Autowired JmsTemplate jmsTemplate; @Override public void run(String... args) throws Exception { System.out.println("Sending an email message."); jmsTemplate.convertAndSend("mailbox", new Email("info@example.com", "Hello")); }}
上面两个例子pom.xml和application.properties一样
但是要分开调试
阅读全文
0 0
- 学习activemq(2)Spring boot整合activemq jms
- spring boot整合JMS(ActiveMQ实现)
- spring boot整合JMS(ActiveMQ实现)
- spring boot jms activemq
- SPRING JMS 整合ACTIVEMQ
- Spring学习笔记 - Spring 整合 JMS - ActiveMQ
- spring boot整合activemq
- spring整合activeMq 调试JMS
- spring整合activeMq 调试JMS
- JMS-ActiveMq与Spring整合
- Spring ActiveMQ 整合: JMS 事务管理
- spring boot ActiveMQ学习
- spring boot整合activemq rabbitmq
- (八)Spring Boot整合ActiveMQ
- Spring Boot与ActiveMQ整合
- Spring整合Jms学习(一)_基于ActiveMQ实现
- JMS学习(4):--Spring和ActiveMQ整合的完整实例
- ActiveMQ、Spring整合学习
- Kafka集群配置
- mysql 常用
- iOS 本地推送以及自定义推送声音
- office2o.com 表单设计与审批功能介绍
- 使用Systemd包装SpringBoot应用
- 学习activemq(2)Spring boot整合activemq jms
- loading-css
- CSDN邀请您来GitChat赚钱啦!
- python编程练习2-----条件判断学习
- Apache 修改并发请求数
- STM32平台下对串行通信梳理
- android中message、messageQueue、Handler、looper的关系
- 分类和回归(四)-线性回归
- 利用Support-Annotations提高代码质量