ActiveMQ
Spring整合ActiveMQ
pom.xml依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.3.23.RELEASE</version> </dependency>
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.15.9</version> </dependency>
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.9</version> </dependency> <dependency> <groupId>org.apache.xbean</groupId> <artifactId>xbean-spring</artifactId> <version>3.16</version> </dependency>
|
applicationContext.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
<context:component-scan bean-package="*" />
<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://192.168.1.132: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" /> </bean>
<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>
|
生产者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Service public class SpringMQ_Produce { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args){ ApplicationContext ctx = new ClassPathXmlAppalicationContext("applicationContext.xml"); SpringMQ_Produce produce = (SpringMQ_Produce)ctx.getBean("springMQ_Produce"); produce.jmsTemplate.send(new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage message = session.createTextMessage("Spring和ActiveMQ的整合"); return message; } }); } }
|
消费者
1 2 3 4 5 6 7 8 9 10 11
| @Service public class SpringMQ_Consumer { @Autowired private JmsTemplate jmsTemplate; public static void main(String[] args){ ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); SpringMQ_Consumer consumer = (SpringMQ_Consumer)ctx.getBean("springMQ_Consumer"); String value = (String)consumer.jmsTemplate.receiveAndConvert(); System.out.println("消费者收到消息:" + value); } }
|
监听器配置
1 2 3 4 5 6 7 8
|
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsFactory" /> <property name="destination" ref="destinationQueue" /> <property name="messageListener" ref="myMessageListener"/> </bean>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @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(); } } } }
|
配置监听器后,只需要启动生产者,消费者不用启动,自动会监听记录。