本文是关于spring和activeMq一种简单的整合方式,只做参考学习只用,侧重于对概念的理解。
1:JMS是Sun公司开发的一套访问面向消息的中间件(MOM)的标准的API,本文采用的MOM组件是 activeMq.大家可以到 [url]http://activemq.apache.org/download.html[/url]网站下载activemq的程序包,
它使用非常简单,解压缩之后直接运行D:\activemq-4.1.1\bin目录下的activemq.bat文件,启动服务就可以了。 而且我们只是简单的测试,所以不需要我们配置jndi的相关内容。服务启动
之后我们看到了相应的端口被启动了,这样我么的MOM组件准备就绪...
 
2:接下来,我们新建一个WEB的项目(我用的是MyEclipse),导入相关的包,建议大家不要使用MyEclipse中自带的那个spring2.0的包,因为好几个项目都是因为这个调试了很久,就是因为那个包有问题。呵呵。
导入spring2.0.jar、apache-activemq-4.1.1.jar、commons-pool-1.2.jar、long4j.jar、commons-logging-1.1.jar文件到lib目录下。接下来在WEB-INF下新建两个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="
        [url]http://www.springframework.org/schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
        [url]http://www.springframework.org/schema/tx[/url] [url]http://www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]" >

   < bean id ="connectionFactory" class ="org.apache.activemq.pool.PooledConnectionFactory" >
     < property name ="connectionFactory" >
       < bean class ="org.apache.activemq.ActiveMQConnectionFactory" >
         < property name ="brokerURL" >
           < value >tcp://localhost:61616 </ value >
         </ property >
       </ bean >
     </ property >
   </ bean >
    
   < bean id ="dest" class ="org.apache.activemq.command.ActiveMQQueue" >
     < constructor-arg value ="myDest" />
   </ bean >
    
   < bean id ="jmsTemplate" class ="org.springframework.jms.core.JmsTemplate" >
     < property name ="connectionFactory" ref ="connectionFactory" > </ property >
     < property name ="defaultDestination" ref ="dest" />
   </ bean >
    
   < bean id ="messageSender" class ="com.bo.impl.MessageSender" >
     < property name ="jmsTemplate" ref ="jmsTemplate" > </ property >
   </ bean >
</ beans >
 
 


<? xml version ="1.0" encoding ="UTF-8" ?>
< beans xmlns ="[url]http://www.springframework.org/schema/beans[/url]"
xmlns:xsi ="[url]http://www.w3.org/2001/XMLSchema-instance[/url]"
xmlns:tx ="[url]http://www.springframework.org/schema/tx[/url]"
xsi:schemaLocation="
        [url]http://www.springframework.org/schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-2.0.xsd[/url]
        [url]http://www.springframework.org/schema/tx[/url] [url]http://www.springframework.org/schema/tx/spring-tx-2.0.xsd[/url]" >
        
         < bean id ="dest" class ="org.apache.activemq.command.ActiveMQQueue" >
     < constructor-arg value ="myDest" > </ constructor-arg >
</ bean >
    
< bean id ="connectionFactory" class ="org.apache.activemq.pool.PooledConnectionFactory" >
     < property name ="connectionFactory" >
     < bean class ="org.apache.activemq.ActiveMQConnectionFactory" >
         < property name ="brokerURL" value ="tcp://localhost:61616" />
     </ bean >
     </ property >
</ bean >
    
< bean id ="jmsTemplate" class ="org.springframework.jms.core.JmsTemplate" >
     < property name ="connectionFactory" ref ="connectionFactory" > </ property >
     < property name ="defaultDestination" ref ="dest" > </ property >
</ bean >
    
< bean id ="messageReceiver" class ="com.bo.impl.MessageReceiver" >
     < property name ="jmsTemplate" 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( "这个世界真的很无奈!");
}
    
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();
}
}
7: 测试结果:
消息编码:ID:hc-369a3f54b2b0-1440-1224731999968-1:0:1:1:1
目标对象:queue://myDest
消息模式:2
消息发送时间:2008-10-23 11:20:00
消息失效时间:0
消息优先级:4
关联编码:null
回复消息的地址:null
消息类型:null
是否签收过:false
用户编码:123456789
应用程序编码:001002
已经尝试发送消息的次数:1
消息内容:这个世界真的很无奈!