SpringBoot实战(十一)之与JMS简单通信
什么是JMS?
引用百度百科上的说明:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.springframework</groupId>
<artifactId>gs-messaging-jms</artifactId>
<version>0.1.0</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
二、编写消息接收器
package hello; 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());
} }
三、定义消息接收者
package hello; import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; @Component
public class Receiver { @JmsListener(destination = "mailbox", containerFactory = "myFactory")
public void receiveMessage(Email email) {
System.out.println("Received <" + email + ">");
} }
Receiver
也被称为消息驱动的POJO。正如您在上面的代码中所看到的,不需要实现任何特定的接口或方法具有任何特定的名称。此外,该方法可以具有非常灵活的签名。请特别注意,此类在JMS API上没有导入。
四、使用Spring发送和接收JMS消息
package hello; import javax.jms.ConnectionFactory; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType; @SpringBootApplication
@EnableJms
public class Application { @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;
} public static void main(String[] args) {
// Launch the application
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); // Send a message with a POJO - the template reuse the message converter
System.out.println("Sending an email message.");
jmsTemplate.convertAndSend("mailbox", new Email("info@example.com", "Hello"));
} }
@Bean注解,主要作用是控制反转(IOC),同
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
</bean>
举例说明:
例如以Message为例:
@Bean
public String message() {
return new String("hello");
}
它等价于
<bean id="message" class="java.lang.String">
<constructor-arg index="0" value="hello"/>
</bean>
通常情况下,有三种配置Bean的方式:
如图所示
关于Bean注解三种配置法,深入介绍可以参考该博文:https://blog.csdn.net/icarus_wang/article/details/51649635
@EnableJms
触发发现带注释的方法@JmsListener
,在封面下创建消息监听器容器。
为清楚起见,我们还定义了一个在接收器注释中myFactory
引用的bean JmsListener
。因为我们使用DefaultJmsListenerContainerFactoryConfigurer
Spring Boot提供的基础结构,所以JmsMessageListenerContainer
它与默认情况下引导创建的基础结构相同。
默认MessageConverter
是能够转换只有基本类型(例如String
,Map
,Serializable
)我们Email
是不是Serializable
故意的。我们想要使用Jackson并以文本格式将内容序列化为json(即作为a TextMessage
)。Spring Boot将检测a的存在,MessageConverter
并将其与默认值JmsTemplate
和任何JmsListenerContainerFactory
创建者相关联DefaultJmsListenerContainerFactoryConfigurer
。
JmsTemplate
使消息发送到JMS目的地变得非常简单。在main
runner方法中,启动后,您可以使用jmsTemplate
发送Email
POJO。因为我们的自定义MessageConverter
已自动关联到它,所以只会生成一个json文档TextMessage
。
你没看到的两个bean是JmsTemplate
和ConnectionFactory
。这些是由Spring Boot自动创建的。在这种情况下,ActiveMQ代理运行嵌入式。
注意:
Spring JmsTemplate
可以通过它的receive
方法直接接收消息,但这只能同步工作,这意味着它会阻塞。这就是为什么我们建议您使用侦听器容器,例如DefaultMessageListenerContainer
使用基于缓存的连接工厂,这样您就可以异步使用消息并以最大的连接效率。
最后运行结果如下图所示:
SpringBoot实战(十一)之与JMS简单通信的更多相关文章
- springboot实战开发全套教程,让开发像搭积木一样简单!Github星标已上10W+!
前言 先说一下,这份教程在github上面星标已上10W,下面我会一一给大家举例出来全部内容,原链接后面我会发出来!首先我讲一下接下来我们会讲到的知识和技术,对比讲解了多种同类技术的使用手日区别,大家 ...
- SpringBoot实战 之 异常处理篇
在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...
- apollo客户端springboot实战(四)
1. apollo客户端springboot实战(四) 1.1. 前言 经过前几张入门学习,基本已经完成了apollo环境的搭建和简单客户端例子,但我们现在流行的通常是springboot的客户端 ...
- SpringBoot实战(四)获取接口请求中的参数(@PathVariable,@RequestParam,@RequestBody)
上一篇SpringBoot实战(二)Restful风格API接口中写了一个控制器,获取了前端请求的参数,现在我们就参数的获取与校验做一个介绍: 一:获取参数 SpringBoot提供的获取参数注解包括 ...
- SpringBoot实战(二)Restful风格API接口
在上一篇SpringBoot实战(一)HelloWorld的基础上,编写一个Restful风格的API接口: 1.根据MVC原则,创建一个简单的目录结构,包括controller和entity,分别创 ...
- JMS消息通信服务
什么是Java消息服务 Java消息服务指的是两个应用程序之间进行异步通信的API,它为标准消息协议和消息服务提供了一组通用接口,包括创建.发送.读取消息等,用于支持JAVA应用程序开发.在J2EE中 ...
- SpringBoot实战之异常处理篇
在互联网时代,我们所开发的应用大多是直面用户的,程序中的任何一点小疏忽都可能导致用户的流失,而程序出现异常往往又是不可避免的,那该如何减少程序异常对用户体验的影响呢?其实方法很简单,对异常进行捕获,然 ...
- Docker深入浅出系列 | 单机Nginx+Springboot实战
目录 Nginx+Springboot实战 前期准备 实战目标 实战步骤 创建Docker网络 搭建Mysql容器 搭建额度服务集群 搭建Nginx服务 验证额度服务 附录 Nginx+Springb ...
- Eclipse搭建服务器,实现与Android的简单通信
---恢复内容开始--- 目标:实现客户端(Android App)与服务器(PC)的简单通信 相关准备:eclipse_mars.tomcat8.Android Studio 实现: 1.java环 ...
随机推荐
- MySQL一查就会
Table1--mysql常用操作 主题 用例 说明 书写规范 数据库和表的名称不一定要大写. 输入文本类型的数据时都要加上单引号: NULL 表示未定义,它不会等于另一个NULL: 不要使用双引号. ...
- golang 的md5加密
先看实现代码: package main import ( "crypto/md5" "encoding/hex" "fmt& ...
- #if, #elif, #else, #endif 使用
程序想要通过简单地设置一些参数就生成一个不同的软件,在不同的情况下可能只用到一部分代码,就没必要把所有的代码都写进去,就可以用条件编译,通过预编译指令设置编译条件,在不同的需要时编译不同的代码. (一 ...
- Windows安装ActiveMQ记录
1.下载压缩包(activeMQ应用要基于jdk服务上,安装本软件时,最好已经安装了jdk并且配置好了环境变量) 下载5.12.2版本:http://activemq.apache.org/activ ...
- HTML表单相关
表单:<input type="text" name="" value="" size="显示字符数" maxle ...
- JS判断浏览器版本
CSS html,body{ height: 100%; } body{ margin: 0 } div{ padding-left: 50px; } .span{ padding: 5px 15px ...
- Spring Boot 开发入门
准备工作 我们将使用Java开发一个简单的"Hello World" web应用,项目采用Maven进行构建 在开始前,打开终端检查下安装的Java和Maven版本是否可用: C: ...
- 软工读书笔记 week4 ——《黑客与画家》下
因为时间有限,只对书中后半部分几个篇章进行了阅读. 一.另一条路 作者以他自己为例,在那个没人知道什么叫“软件运行在服务器时”的时代,他和朋友选择创业时,没有选择写传统的桌面 ...
- 在Windows下为PHP5.6安装redis扩展和memcached扩展
一.php安装redis扩展 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本 2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...
- 如何查看服务器CPU核心数和线程数
知道服务器CPU型号,那么我们如何在服务器里面查看服务器CPU核心数和线程数呢? 步骤: 先用鼠标右键点击屏幕最下方的任务栏空白处.会弹出一个菜单. 在菜单中用鼠标左键点选“启动任务管理器”. 点击任 ...