Spring JMS 点对点消息

– Start
点击此处观看本系列配套视频。


非常类似 JDBC,Spring 使用 JmsTemplate 来发送和接收消息,使我们不用再关心那些样板代码,下面是一个简单的例子。

发送消息

package shangbo.activeMQ.example2;

import javax.jms.JMSException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jms.core.JmsTemplate;

class Producer {

	public static void main(String[] args) throws JMSException {
		// 实例化 Spring IoC 容器
		@SuppressWarnings("resource")
		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

		// 发送消息
		jmsTemplate.convertAndSend("hello queue world");
		System.exit(0);
	}
}

接收消息

package shangbo.activeMQ.example2;

import javax.jms.JMSException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jms.core.JmsTemplate;

class Consumer {
	public static void main(String[] args) throws JMSException {
		// 实例化 Spring IoC 容器
		@SuppressWarnings("resource")
		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);

		// 接收消息
		while (true) {
			String msg = (String) jmsTemplate.receiveAndConvert();
			if (msg == null) {
				System.out.println("No message received after 5 seconds");
			} else {
				System.out.println(msg);
			}
		}
	}

}

Spring 配置

package shangbo.activeMQ.example2;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;

@Configuration
public class AppConfig {
	// ActiveMQ 支持不同的协议,你可以在它的配置文件中 conf/activemq.xml 找到不同协议的连接方式
	public static String BROKER_URL = "tcp://0.0.0.0:61616";
	public static String USER = "admin";
	public static String PASSWORD = "admin";
	public static String PRICE_QUEUE = "systemA.systemB.Price.Queue";

	@Bean
	public ConnectionFactory connectionFactory() {
		CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
		connectionFactory.setTargetConnectionFactory(activeMQConnectionFactory());
		connectionFactory.setSessionCacheSize(20);

		return connectionFactory;
	}

	private ActiveMQConnectionFactory activeMQConnectionFactory() {
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
		factory.setBrokerURL(BROKER_URL);
		factory.setUserName(USER);
		factory.setPassword(PASSWORD);

		return factory;
	}

	@Bean
	public Queue priceQueue() {
		return new ActiveMQQueue(PRICE_QUEUE);
	}

	@Bean
	public JmsTemplate priceJmsTemplate(ConnectionFactory factory, Queue priceQueue) {
		JmsTemplate jmsTemplate = new JmsTemplate();
		jmsTemplate.setConnectionFactory(factory);
		jmsTemplate.setDefaultDestination(priceQueue);
		jmsTemplate.setReceiveTimeout(5 * 1000);

		return jmsTemplate;
	}

}

– 更多参见:JMS + ActiveMQ 精萃
– 声 明:转载请注明出处
– Last Updated on 2018-05-31
– Written by ShangBo on 2018-05-31
– End

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值