– Start
点击此处观看本系列配套视频。
和点对点一样,Spring 使用 JmsTemplate 来发送和接收消息,使我们不用再关心那些样板代码,下面是一个简单的例子。
发送消息
package shangbo.activeMQ.example4;
import javax.jms.JMSException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jms.core.JmsTemplate;
class Publisher {
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 topic world");
System.exit(0);
}
}
接收消息
package shangbo.activeMQ.example4;
import javax.jms.JMSException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jms.core.JmsTemplate;
class Subscriber {
public static void main(String[] args) throws JMSException {
// 实例化 Spring IoC 容器
@SuppressWarnings("resource")
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
// 接收消息
String msg = (String) jmsTemplate.receiveAndConvert();
System.out.println(msg);
System.exit(0);
}
}
Spring 配置
package shangbo.activeMQ.example4;
import javax.jms.ConnectionFactory;
import javax.jms.Topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
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_TOPIC = "systemA.systemB.Price.Topic";
@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 Topic priceTopic() {
return new ActiveMQTopic(PRICE_TOPIC);
}
@Bean
public JmsTemplate priceJmsTemplate(ConnectionFactory factory, Topic priceTopic) {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(factory);
jmsTemplate.setDefaultDestination(priceTopic);
jmsTemplate.setReceiveTimeout(5 * 1000);
return jmsTemplate;
}
}
– 更多参见:JMS + ActiveMQ 精萃
– 声 明:转载请注明出处
– Last Updated on 2018-05-31
– Written by ShangBo on 2018-05-31
– End