Spring整合ActiveMQ
阿新 • • 發佈:2020-08-11
匯入依賴
<!-- activeMQ jms 的支援 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency><!-- pool 池化包相關的支援 --> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.16.0</version> </dependency> <!-- aop 相關的支援 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.7.RELEASE</version> </dependency>
寫xml檔案(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--開啟自動掃描--> <context:component-scan base-package="com.imooc.activemq"/> <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://112.124.20.231:61616"></property> </bean> </property> <property name="maxConnections" value="100"></property> </bean> <!-- 佇列目的地 --> <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="spring-active-queue"></constructor-arg> </bean> <!-- jms 的工具類 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsFactory"/> <property name="defaultDestination" ref="destinationQueue"/> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/> </property> </bean> </beans>
生產者
@Service public class SpringMQ_producer { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); SpringMQ_producer producer = (SpringMQ_producer)ctx.getBean("springMQ_producer"); producer.jmsTemplate.send((session)->{ TextMessage textMessage = session.createTextMessage("spring和activeMQ的整合"); return textMessage; }); } }
消費者
@Service
public class SpringMQ_Consumer {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");
String s = (String) consumer.jmsTemplate.receiveAndConvert();
System.out.println("***消費者訊息****"+s);
}
}
Topic主題生產消費
監聽器
@Component
public class myMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
if (message != null&&message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println(textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
在applicationContext.xml中加入