– Start
点击此处观看本系列配套视频。
自定义 MessageListener
package shangbo.activeMQ.example6;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public class MyMessageListener implements MessageListener {
public void onMessage(Message msg) {
if (msg instanceof TextMessage) {
String body;
try {
body = ((TextMessage) msg).getText();
System.out.println(body);
} catch (JMSException e) {
e.printStackTrace();
}
} else {
System.out.println("Unexpected message type: " + msg.getClass());
}
}
}
发送消息
package shangbo.activeMQ.example6;
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);
}
}
Spring 配置
package shangbo.activeMQ.example6;
import javax.jms.ConnectionFactory;
import javax.jms.MessageListener;
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;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
@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);
return jmsTemplate;
}
@Bean
public MyMessageListener myMessageListener() {
return new MyMessageListener();
}
@Bean
public DefaultMessageListenerContainer messageListenerContainer(ConnectionFactory factory, Topic priceTopic, MessageListener messageListener) {
DefaultMessageListenerContainer mlc = new DefaultMessageListenerContainer();
mlc.setConnectionFactory(factory);
mlc.setDestination(priceTopic);
mlc.setMessageListener(messageListener);
return mlc;
}
}
– 更多参见:JMS + ActiveMQ 精萃
– 声 明:转载请注明出处
– Last Updated on 2018-05-31
– Written by ShangBo on 2018-05-31
– End