简单springBoot ActivityMq 配置和使用
1.activeMQ准备
既然是使用的apache的activeMQ作为JMS的实现,那么首先我们应该到apache官网上下载activeMQ(http://activemq.apache.org/download.html),进行解压后运行其bin目录下面的activemq.bat文件启动activeMQ。
2.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>activityDemo</groupId>
<artifactId>activity</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent>
<dependencies>
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- activity 配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
</dependencies>
</project>
3.application.yml 配置
spring:
activemq:
in-memory: false
broker-url: tcp://127.0.0.1:61616
password: admin
user: admin
pooled: false
#activemq cosumer线程池
consumer:
corePoolSize: 1
maxPoolSize: 1
4.Application类
@SpringBootApplication
public class Application {
/**
* Queue
*
* @return
*/
@Bean(name = "testQueue")
public javax.jms.Queue testQueue() {
return new ActiveMQQueue("test.queue");
}
/**
* 异步线程池 初始化
*
* @return
*/
@Bean
public ThreadPoolTaskExecutor cosumerTaskExecutor(
@Value("${consumer.corePoolSize}") int corePoolSize,
@Value("${consumer.maxPoolSize}") int maxPoolSize) {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
return executor;
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.run(args);
}
}
5.Producer类(生产者)
@Component
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue testQueue;
public void sendText(String msg){
this.jmsMessagingTemplate.convertAndSend(this.testQueue,msg);
}
}
6.Consumer(消费者)
@Component
public class Consumer {
@Resource
private ThreadPoolTaskExecutor threadPoolTaskExecutor;
@JmsListener(destination = "test.queue")
private void getMessageText(final String text) {
threadPoolTaskExecutor.submit(new Runnable() {
@Override
public void run() {
try {
// 业务处理
Thread.sleep(2000);
System.out.println("##################################################################");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程ID = " + Thread.currentThread().getId());
System.out.println("消费者接受信息:" + text);
}
});
}
}
点击查看更多内容
4人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦