继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

spring boot activityMq配置使用demo

捡肥皂的男孩
关注TA
已关注
手记 3
粉丝 5
获赞 161

简单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);
             }
         });
    }
}
打开App,阅读手记
4人推荐
发表评论
随时随地看视频慕课网APP

热门评论

这个案例整合进去能运行?

查看全部评论