SpringBoot JMS(ActiveMQ) 使用实践

ActiveMQ

1. 下载windows办的activeMQ后,在如下目录能够启动:java

    

2. 启动后会有如下提示spring

    

3. 因此咱们能够经过http://localhost:8161访问管理页面,经过tcp://localhost:61616来链接消息服务器,用到的用户名和密码都在如下文件中(默认为admin=admin)apache

    

springboot链接ActiveMQ

1. 加入依赖:windows

            spring-boot-starter-activemqspringboot

2. 配置链接属性:bash

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=false

消息的发送和接收

生产者/消费者模式

    1. 建立生产者服务器

package com.example.demo8activemq.jms;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;

/**
 * @author Created by yawn on 2017-10-26 16:15
 */
@Service
public class Producer {

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    public void sendMsg(String destinationName, String message) {
        System.out.println("============>>>>> 发送queue消息 " + message);
        Destination destination = new ActiveMQQueue(destinationName);
        jmsMessagingTemplate.convertAndSend(destination, message);
    }
}

    2. 建立消费者tcp

package com.example.demo8activemq.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

/**
 * @author Created by yawn on 2017-10-26 16:15
 */
@Service
public class Consumer {

    @JmsListener(destination = "test.queue")
    public void receiveMsg(String text) {
        System.out.println("<<<<<<============ 收到消息: " + text);
    }
}

    注意: @JmsListener是一个可重复的注解,在java7及如下版本jdk中,可使用@JmsListeners代替它。spring-boot

    3. 测试类测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo8ActivemqApplicationTests {

	@Resource
	private Producer producer;

	@Test
	public void contextLoads() {
		for (int i = 0; i < 10; i++) {
			producer.sendMsg("test.queue", "Queue Message " + i);
		}
	}
}

    4. 运行测试

发布/订阅模式

    1. 发布话题

package com.example.demo8activemq.jms;

import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.Destination;

/**
 * @author Created by yawn on 2017-10-28 17:09
 */
@Service
public class Publisher {

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    public void publish(String destinationName, String message) {
        Destination destination = new ActiveMQTopic(destinationName);
        System.out.println("============>>>>> 发布topic消息 " + message);
        jmsMessagingTemplate.convertAndSend(destination, message);
    }
}

    2. 订阅话题

package com.example.demo8activemq.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

/**
 * @author Created by yawn on 2017-10-28 17:15
 */
@Service
public class Subscriber {

    @JmsListener(destination = "test.topic", containerFactory = "myJmsContainerFactory")
    public void subscribe(String text) {
        System.out.println("===========<<<<<<<<收到订阅的消息" + text);
    }
}

    注意: 在pub/sub模式中,对消息的监听须要对containerFactory进行如下配置

@Bean
    JmsListenerContainerFactory<?> myJmsContainerFactory(ConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }

    3. 测试

@Test
	public void test() {
		for (int i = 0; i < 10; i++) {
			publisher.publish("test.topic", "Topic Message " + i);
		}
	}

应用

    按照以上步骤,在springboot中很容易就实现类两种模式的消息发送和接收。可是jms具体的应用场景是在不一样的应用程序之间,生产者和消费者每每是在不一样的应用中的。此外,以上例子中的消息咱们只发送字符串,其实还能够发送Object类型的消息,甚至可使用messageCreator自定义消息的转换,而不使用convertAndSend方法默认转换。

多个应用程序之间发送消息

1. 先使用一个只有发送者,没有消费者或订阅者的应用发送两类消息各十条

    

    

2. 咱们打开localhost:8161,能够看到

    

    

    两类都曾有十条消息入队,但只有queues中还存留10条消息。

3. 如今咱们启动包含消费者和订阅者的应用程序

    

    果真,只有消费者收到了queues中的消息。

    这说明订阅者接收topic是须要在topic发布以前订阅;而生产/消费模式下,消息发出后会存放在队列中,等待消费者消费。

    4. 咱们先启动两个包含订阅者和消费者的程序,再发布消息

    

    

    

    两个订阅者都收到 topic message 1~9, 而消费者中,一个收到消息 一、三、五、七、9,另外一个收到0、二、四、六、8。

    这说明有多个消息接收者时,生产/消费模式下多个消费者会轮流消费队列中的消息,而pub/sub模式下全部订阅者都会获得全部的消息。

    以上就是在多个应用程序之间验证了发布/订阅模式和生产/消费模式的不一样特色。

相关文章
相关标签/搜索