你需要什么
- 大约 15 分钟
- IntelliJ IDEA或其他编辑器
- JDK 1.8或更高版本
- Maven 3.2+
你会建立什么
本指南将指导您完成使用 JMS
代理发布和订阅消息的过程。您将构建一个应用程序,该应用程序使用Spring的 JmsTemplate
发布单个消息并使用托管 bean
的 @JmsListener
注释方法订阅该消息。
构建步骤
1、添加maven
依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jms依赖 -->
<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>
2、创建一个建消息POJO
。
public class Email {
private String to;
private String body;
//...setter 、getter..
}
3、创建一个消息接收器(receiver)
import com.sqlb.guidejms.bean.Email;
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 + ">");
}
}
JmsListener
注解定义了此方法应侦听的 Destination
的名称以及用于创建基础消息侦听器容器的对JmsListenerContainerFactory
的引用。严格地说,最后一个属性是不必要的,除非你需要定制容器的构建方式,因为Spring Boot会在必要时注册一个默认工厂。
4、配置一把
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;
import javax.jms.ConnectionFactory;
@Configuration
public class JmsConfig {
@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;
}
}
4、使用Spring发送
import com.sqlb.guidejms.bean.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class JmsController {
@Autowired
JmsTemplate jmsTemplate;
@RequestMapping("/hello")
public String hello(){
System.out.println("Sending an email message.");
jmsTemplate.convertAndSend("mailbox", new Email("info@example.com", "Hello"));
return "ok";
}
}
5、启动程序
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jms.annotation.EnableJms;
@SpringBootApplication
@EnableJms
public class GuideJmsApplication {
public static void main(String[] args) {
SpringApplication.run(GuideJmsApplication.class, args);
}
}
@EnableJms
触发发现使用@JmsListener
注解的方法,创建消息监听器容器。
测试
浏览器端http://localhost:8080/hello
:
控制台输出可以看出,已经接收到消息了。