ActiveMQ结合Spring开发

ActiveMQ结合Spring开发

----------------------------------------------------------------------------

Spring结合ActiveMQ开发 : 发送和接收queue

1.首先在pom.xml文件中添加activemq的依赖包

      <!-- Spring对JMS的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<!-- 添加ActiveMQ的pool包 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.9.0</version>
</dependency>

2.在Spring的配置文件applicationContext.xml中配置jmsTemplate

 <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.81:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="destination"></property>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="spring-queue"/>
</bean>

3.创建一个queue的消息发送者程序

 import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service; @Service
public class SpringQueueSender {
@Autowired
private JmsTemplate jt = null; public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringQueueSender sqs = (SpringQueueSender)ctx.getBean("springQueueSender");
sqs.jt.send(new MessageCreator(){
public Message createMessage(Session s) throws JMSException {
TextMessage msg = s.createTextMessage("Spring send msg ----> Hello activeMQ");
return msg;
}
});
}
}

4.创建一个queue的消息接收者程序

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service; @Service
public class SpringQueueReceiver {
@Autowired
private JmsTemplate jt = null; public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringQueueReceiver sqr = (SpringQueueReceiver)ctx.getBean("springQueueReceiver");
String msg = (String)sqr.jt.receiveAndConvert();
System.out.println("receive msg = " + msg);
}
}

运行结果:

ActiveMQ结合Spring开发

Spring结合ActiveMQ开发 : 发送和接收topic

1.在Spring的配置文件applicationContext.xml中配置topic

 <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://120.76.123.81:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="destinationTopic"></property>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean> <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-topic"/>
</bean>

topic消息发送者和接收者 程序和上面一样。

Spring结合ActiveMQ开发 : 在Spring中配置消费者

Spring的applicationContext.xml配置

     <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.81:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="destination"></property>
<property name="messageConverter">
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
</property>
</bean> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="spring-queue"/>
</bean> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"/>
<property name="destination" ref="destination"/>
<property name="messageListener" ref="messageListener"/>
</bean> <bean id="messageListener" class="com.test.spring.MyMessageListener">
</bean>

消费发送者程序

 import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service; @Service
public class SpringQueueSender {
@Autowired
private JmsTemplate jt = null; public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringQueueSender sqs = (SpringQueueSender)ctx.getBean("springQueueSender");
sqs.jt.send(new MessageCreator(){
public Message createMessage(Session s) throws JMSException {
TextMessage msg = s.createTextMessage("在Spring中配置消息接收者 ----> Hello activeMQ");
return msg;
}
});
}
}

消息接受者程序

 import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage; public class MyMessageListener implements MessageListener{ public void onMessage(Message msg) {
TextMessage txtMsg = (TextMessage)msg;
try{
System.out.println("reveive txt msg = " + txtMsg);
}catch(Exception e){
e.printStackTrace();
}
}
}
上一篇:Python学习笔记之条件、循环和其他语句


下一篇:第29章 项目10:DIY街机游戏