本章将通过sping注入的方式实现点到点的activitemq发送与接收
实例1:消息发送
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
* 点到点消息测试
* @Description:
* @author wuyan
* @date 2015年12月23日 上午10:53:54
*/
public class AMQSender {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:/spring/application-context.xml" });
JmsTemplate template = (JmsTemplate) applicationContext
.getBean("jmsTemplate");
//通过<span style="font-family: Arial, Helvetica, sans-serif;">org.springframework.jms.core.MessageCreator 接口创建消息体</span>
template.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session
.createTextMessage("Hello Text Message!");
}
});
System.out.println("<span style="font-family: Arial, Helvetica, sans-serif;">消息</span><span style="font-family: Arial, Helvetica, sans-serif;">发送成功!");</span>
}
}
实例2:消息接收者
/**
*
*/
package wy.amq;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
/**
* 消息接收器
* @Description:
* @author wuyan
* @date 2015年12月23日 上午10:53:54
*/
public class AMQReceiver{
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "classpath:/spring/application-context.xml" });
JmsTemplate template = (JmsTemplate) applicationContext
.getBean("jmsTemplate");
while(true){
try {
Message message = template.receive();
System.out.println("成功接收消息:" + ((ActiveMQTextMessage)message).getText());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
实例3:application-context.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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<!-- 配置connectionFactory -->
<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://localhost:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>
<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
<property name="defaultDestinationName" value="subject" />
<!--false为点到点模式 true为订阅发布模式-->
<property name="pubSubDomain" value="false" />
</bean>
</beans>