spring2.0+activemq5.1.0整合
1.新建一个WEB的项目(我用的是MyEclipse),导入相关的包,建议大家不要使用MyEclipse中自带的那个spring2.0的包。导入activemq相关jar包到lib目录下。
2.建两个个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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<bean id="connectionFactory" ref="connectionFactory"></property>
<property name="defaultDestination" ref="dest"/>
</bean>
<bean id="messageSender" ref="jmsTemplate"></property>
</bean>
</beans>
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<bean id="dest" value="tcp://localhost:61616"/>
</bean>
</property>
</bean>
<bean id="jmsTemplate" ref="connectionFactory"></property>
<property name="defaultDestination" ref="dest"></property>
</bean>
<bean id="messageReceiver" ref="jmsTemplate"></property>
</bean>
</beans>
3.发送消息类:
public class MessageSender extends JmsGatewaySupport{
public void sendTextMsg(final String msg) {
this.getJmsTemplate().send(new MessageCreator() {
// 这里创建了一个 message 对象,然后可以对该对象进行 各种属性的定义
private Message message;
public Message createMessage(Session session) throws JMSException {
message = session.createTextMessage(msg);
message.setStringProperty("JMSXUserID", "123456789"); // 消息所属的用户编码
message.setStringProperty("JMSXApp1ID", "001002"); // 消息所属的应用程序编码
return message;
}
});
}
}
4.接收消息类:
public class MessageReceiver extends JmsGatewaySupport{
public void receiverTextMsg(){
TextMessage textMsg = (TextMessage)this.getJmsTemplate().receive();
try{
// 消息 header 中常有的 属性定义
System.out.println("消息编码:" + textMsg.getJMSMessageID());
System.out.println("目标对象:" + textMsg.getJMSDestination());
System.out.println("消息模式:" + textMsg.getJMSDeliveryMode()); // 消息的模式 分为持久模式和非持久模式, 默认是 非持久的模式(2)
long sendTime = textMsg.getJMSTimestamp();
Date date = new Date(sendTime);
DateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String temp = f.format(date);
System.out.println("消息发送时间:" + temp);
System.out.println("消息失效时间:" + textMsg.getJMSExpiration()); // 这里是一个 整型值 0 表示 该消息永远不会过期
System.out.println("消息优先级:" + textMsg.getJMSPriority()); // 优先级 0~9, 0 表示最低
System.out.println("关联编码:" + textMsg.getJMSCorrelationID());
System.out.println("回复消息的地址:" + textMsg.getJMSReplyTo()); // 回复消息的地址(Destination类型),由发送者设定
System.out.println("消息类型:" + textMsg.getJMSType()); // jms 不使用该字段, 一般类型是由 用户自己定义
System.out.println("是否签收过:" + textMsg.getJMSRedelivered()); // 如果是 真 ,表示客户端收到过该消息,但是并没有签收
// 消息属性 (properties)
System.out.println("用户编码:" + textMsg.getStringProperty("JMSXUserID"));
System.out.println("应用程序编码:" + textMsg.getStringProperty("JMSXApp1ID"));
System.out.println("已经尝试发送消息的次数:" + textMsg.getStringProperty("JMSXDeliveryCount"));
// 消息体(body) 中传递的内容
System.out.println("消息内容:" + textMsg.getText());
}catch(JMSException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
5.测试发送消息:
public class TestMessageSender {
private static ApplicationContext ctx = null;
static{
ctx = new FileSystemXmlApplicationContext(new String[] { "WebRoot/jms_sender.xml" });
}
public static void sentTextMsg(){
MessageSender messageSender = (MessageSender)ctx.getBean("messageSender");
messageSender.sendTextMsg("I‘m activemq~~");
}
public static void main(String[] args){
sentTextMsg();
}
}
6.测试接收消息类:
public class TestMessageReceiver {
private static ApplicationContext ctx = null;
static {
ctx = new FileSystemXmlApplicationContext(new String[] { "WebRoot/jms_receiver.xml" });
}
public static void getTextMsg(){
MessageReceiver messageSender = (MessageReceiver) ctx.getBean("messageReceiver");
messageSender.receiverTextMsg();
}
public static void main(String[] args) {
getTextMsg();
}
}