博客统计信息

51cto推荐博客
用户名:张健的博克
文章数:19
评论数:59
访问量:70353
无忧币:355
博客积分:841
博客等级:4
注册日期:2007-06-12

我的技术圈(2)

更多>>
spring整合activeMq 调试JMS<一>
2008-10-23 11:15:48
本文是关于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
消息内容:这个世界真的很无奈!
分享至
更多
一键收藏,随时查看,分享好友!
ivan18248
1人
了这篇文章
类别:Spring学习体验技术圈()┆阅读()┆评论() ┆ 推送到技术圈返回首页

文章评论

 <<   1   2   3   >>   页数 ( 1/3 )  
2008-10-23 15:48:04
能发送SOAP消息吗?
博主回复:
2008-10-23 18:03:56
当然可以!

2008-11-14 19:51:07
不错,好文章,支持下!

2008-11-24 10:09:52
游戏需要规则,但不是所有的游戏都有不变的规则。
为什么爱情消失后不能有友情?

2008-12-05 11:20:37
楼主我有个问题:我的客户他们自己系统对外公开SOCKET IP和地址,我想通过ACTIVEX 发送报文给他们,并接收返回报文,并且客户端系统不允许我们装任何软件,请问ACTIVEXmq能实现这个吗?
博主回复:
2008-12-05 14:16:05
不知道你对jms的概念理解多少,ActiveMq你可以把他看做是一个第三方的服务器,也就是一个面向消息的中间件。那么你可以通过标准的API发送任何的消息到服务器上,同时你也可以从上面取得你想要的消息,我觉得它能够解决你的问题啊!

2008-12-08 09:49:19
我对ActiveMq理解不多。但我的问题是:
1、客户系统只对外提供IP地址和端口
2、客户系统服务器不允许安装任何软件
3、我的服务器安装ActiveMq,实现发送报文给客户系统,并且接收返回报文。

不知道ActiveMq能否实现这个功能?
博主回复:
2008-12-09 09:51:19
1: 通过MOM你并不需要知道你客户服务器的IP地址和端口,但是需要你的客户系统服务器知道你的MOM服务器的位置,并且程序员需要通过标准的API将数据发送到MOM上,或者从MOM上接收数据下来。
2: 你的客户系统服务器上并不需要安装任何的软件,但是需要增加JMS的代码。
3:你同样可以从MOM上取回客户服务器发送给你的数据,也可以将数据通过MOM发送给你的客户系统服务器。

不知道这样解释可以吗? 呵呵......

 <<   1   2   3   >>   页数 ( 1/3 )  

发表评论            

【技术门诊】专家解析:软考重点难点及应试技巧
昵  称:
登录  快速注册
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容: