mysql active推送消息_Spring Boot整合ActiveMQ实现消息收发和订阅

javax.jms.ConnectionFactory接口提供了一个标准的用于创建一个javax.jms.Connection的方法,javax.jms.Connection用于和JMS代理(broker)交互。尽管为了使用JMS,Spring需要一个ConnectionFactory,但通常不需要直接使用它,而是依赖于上层消息抽象,Spring Boot会自动配置发送和接收消息需要的设施(infrastructure)。

如果发现ActiveMQ在classpath下可用,Spring Boot会配置一个ConnectionFactory。如果需要代理,将会开启一个内嵌的,已经自动配置好的代理(只要配置中没有指定代理URL)。

引入spring-boot-starter-activemq,在pom.xml配置文件中增加配置如下(基于之前章节“Spring Boot 构建框架”中的pom.xml文件):

org.springframework.boot

spring-boot-starter-activemq

ActiveMQ配置是通过spring.activemq.*中的外部配置来控制的。在application.properties配置文件增加如下内容:spring.activemq.broker-url=tcp://192.168.1.100:9876

spring.activemq.user=admin

spring.activemq.password=secret

默认情况下如果目标不存在,ActiveMQ将创建一个,所以目标是通过它们提供的名称解析出来的。

应用整合AxtiveQ支持案例

消息生产者,具体代码如下:import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsMessagingTemplate;

import org.springframework.stereotype.Service;

@Service("producer")

public class Producer {

// 该方式可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装

@Autowired

private JmsMessagingTemplate jmsTemplate;

// 发送消息,destination是发送到的队列,message是待发送的消息

public void sendMessage(Destination destination, final String message){

jmsTemplate.convertAndSend(destination, message);

}

}

两个消息消费者,具体代码如下:/***************** Consumer1 ******************/

import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;

@Component

public class Consumer {

// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息

@JmsListener(destination = "mytest.queue")

public void receiveQueue(String text) {

System.out.println("Consumer收到的报文为:"+text);

}

}

/***************** Consumer2 ******************/

import org.springframework.jms.annotation.JmsListener;

import org.springframework.stereotype.Component;

@Component

public class Consumer2 {

// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息

@JmsListener(destination = "mytest.queue")

public void receiveQueue(String text) {

System.out.println("Consumer2收到的报文为:"+text);

}

}

注意的是消息消费者的类上必须加上@Component或@Service注解,这样的话,消息消费者类就会被委派给Listener类,原理类似于使用SessionAwareMessageListener以及MessageListenerAdapter来实现消息驱动POJO。

简单测试消息情况,具体代码如下:import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTest

public class SpringbootJmsApplicationTests {

@Autowired

private Producer producer;

@Test

public void contextLoads() throws InterruptedException {

Destination destination = new ActiveMQQueue("mytest.queue");

for(int i=0; i<100; i++){

producer.sendMessage(destination, "my site is blog.yoodb.com");

}

}

}

运行结果如下:Consumer2收到的报文为:my site is blog.yoodb.com

Consumer收到的报文为:my site is blog.yoodb.com

Consumer2收到的报文为:my site is blog.yoodb.com

Consumer收到的报文为:my site is blog.yoodb.com

Consumer2收到的报文为:my site is blog.yoodb.com

Consumer收到的报文为:my site is blog.yoodb.com

Consumer2收到的报文为:my site is blog.yoodb.com

Consumer收到的报文为:my site is blog.yoodb.com

Consumer2收到的报文为:my site is blog.yoodb.com

Consumer收到的报文为:my site is blog.yoodb.com

Consumer2收到的报文为:my site is blog.yoodb.com

Consumer收到的报文为:my site is blog.yoodb.com

Consumer2收到的报文为:my site is blog.yoodb.com

你可能感兴趣的:(mysql,active推送消息)