Activemq Jms 简单示例
发布于: 2021 年 01 月 09 日
简介
简单的 Activemp JMS 示例代码
activemq 运行
简单使用 docker 启动一个:
docker run -dit --name mq -p 11616:61616 -p 8161:8161 rmohr/activemq
复制代码
maven 依赖配置
依赖大致如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.jms.activemq</groupId>
<artifactId>jms-activemp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>jms-activemp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
复制代码
配置类编写
配置 activemq 相关连接,大致如下:
@Configuration
public class JmsConfig {
private final String BROKER_URL = "tcp://localhost:11616";
private final String broker_username = "admin";
private final String broker_password = "admin";
@Bean
public ActiveMQConnectionFactory connectionFactory() {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(BROKER_URL);
connectionFactory.setUserName(broker_username);
connectionFactory.setPassword(broker_password);
return connectionFactory;
}
@Bean
public JmsTemplate jmsTemplate() {
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(connectionFactory());
return template;
}
@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrency("1-1");
return factory;
}
}
复制代码
消费监听
配置 Activemq 的监听消费,监听函数有返回值的请参考后面的链接
@Component
public class JmsConsumer {
@JmsListener(destination = "activeTest")
public void receiveMessage(final Map message) {
System.out.println(message.toString());
}
}
复制代码
生产者
发送消息到 activemq
@Component
public class JmsProducer {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(final String topic, final String message) {
Map map = new Gson().fromJson(message, Map.class);
jmsTemplate.convertAndSend(topic, map);
}
}
复制代码
测试运行
在主函数发送
@SpringBootApplication
@EnableJms
@Slf4j
public class JmsActivempApplication implements ApplicationRunner {
@Autowired
private JmsProducer producer;
public static void main(String[] args) {
SpringApplication.run(JmsActivempApplication.class, args);
}
@Override
public void run(ApplicationArguments args) {
String topic = "activeTest";
Map<String, String> message = new HashMap<>(1);
message.put("test", "test");
log.info("send message to topic " + topic + " :: " + message);
producer.sendMessage(topic, message);
}
}
复制代码
参考链接
划线
评论
复制
发布于: 2021 年 01 月 09 日阅读数: 17
版权声明: 本文为 InfoQ 作者【萧】的原创文章。
原文链接:【http://xie.infoq.cn/article/0aff204ce0ad7c5c0b62253d3】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
萧
关注
还未添加个人签名 2018.09.09 加入
代码是门手艺活,也是门艺术活
评论