Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 使用Spring JMS简化异步消息处理
【标  题】:使用Spring JMS简化异步消息处理
【关键字】:Spring,JMS
【来  源】:http://blog.csdn.net/danny_xcz/archive/2006/02/24/607922.aspx

使用Spring JMS简化异步消息处理

Your Ad Here

http://www.onjava.com/lpt/a/6490

这片文章介绍了Spring是如何简化异步消息调用的,它通过一个贷款的例子来说Spring是如何减少

开发中的量的。

下面是传统开发需要的代码量

public void sendMessage() {

    queueName = "queue/CreditRequestSendQueue";
    System.out.println("Queue name is " + queueName);

    /*
     * Create JNDI Initial Context
     */
    try {
        Hashtable env = new Hashtable();
        env.put("java.naming.factory.initial",
            "org.jnp.interfaces.NamingContextFactory");
        env.put("java.naming.provider.url","localhost");
        env.put("java.naming.factory.url.pkgs",
            "org.jnp.interfaces:org.jboss.naming");

        jndiContext = new InitialContext(env);
    } catch (NamingException e) {
        System.out.println("Could not create JNDI API " +
            "context: " + e.toString());
    }

    /*
     * Get queue connection factory and queue objects from JNDI context.
     */
    try {
        queueConnectionFactory = (QueueConnectionFactory)
        jndiContext.lookup("UIL2ConnectionFactory");

        queue = (Queue) jndiContext.lookup(queueName);
    } catch (NamingException e) {
        System.out.println("JNDI API lookup failed: " +
            e.toString());
    }

    /*
     * Create connection, session, sender objects.
     * Send the message.
     * Cleanup JMS connection.
     */
    try {
        queueConnection =
            queueConnectionFactory.createQueueConnection();
        queueSession = queueConnection.createQueueSession(false,
                Session.AUTO_ACKNOWLEDGE);
        queueSender = queueSession.createSender(queue);
        message = queueSession.createTextMessage();
        message.setText("This is a sample JMS message.");
        System.out.println("Sending message: " + message.getText());
        queueSender.send(message);

    } catch (JMSException e) {
        System.out.println("Exception occurred: " + e.toString());
    } finally {
        if (queueConnection != null) {
            try {
                queueConnection.close();
            } catch (JMSException e) {}
        }
    }
}
 
然后是Spring的代码
public void send() {
    try {
        ClassPathXmlApplicationContext appContext = new 
ClassPathXmlApplicationContext(new String[] {
                "spring-jms.xml"});

        System.out.println("Classpath loaded");

        JMSSender jmsSender = (JMSSender)appContext.getBean("jmsSender");

        jmsSender.sendMesage();

        System.out.println("Message sent using Spring JMS.");
    } catch(Exception e) {
        e.printStackTrace();
    }
}
表面上看Spring获胜,代码少了很多,但是我们再来看看Spring配置的XML
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">
                org.jnp.interfaces.NamingContextFactory
            </prop>
            <prop key="java.naming.provider.url">
                localhost
            </prop>
            <prop key="java.naming.factory.url.pkgs">
                org.jnp.interfaces:org.jboss.naming
            </prop>
        </props>
    </property>
</bean>
<bean id="jmsQueueConnectionFactory"
      class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>UIL2ConnectionFactory</value>
    </property>
</bean>
<bean id="sendDestination"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>queue/CreditRequestSendQueue</value>
    </property>
</bean>
<bean id="receiveDestination"
    class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiTemplate">
        <ref bean="jndiTemplate"/>
    </property>
    <property name="jndiName">
        <value>queue/CreditReqeustReceiveQueue</value>
    </property>
</bean>
<bean id="jmsTemplate" 
      class="org.springframework.jms.core.JmsTemplate102">
    <property name="connectionFactory">
        <ref bean="jmsQueueConnectionFactory"/>
    </property>
    <property name="defaultDestination">
        <ref bean="destination"/>
    </property>
    <property name="receiveTimeout">
        <value>30000</value>
    </property>
</bean>
<bean id="jmsSender" class="springexample.client.JMSSender">
    <property name="jmsTemplate">
        <ref bean="jmsTemplate"/>
    </property>
</bean>
<bean id="jmsReceiver" class="springexample.client.JMSReceiver">
    <property name="jmsTemplate">
        <ref bean="jmsTemplate"/>
    </property>
</bean>
 
我们开始认清Spring的真面目把。无厘头的配置,XML的梦魇。从一种混乱到另一种还乱。
 
 
IBM DB2初体验:【上一篇】
Java代码编写的30条建议(转载):【下一篇】
【相关文章】
  • spring培训-笔记
  • 启动SpringSide--Promatic Enterprise Application KickStart项目
  • Spring AOP实际应用一例
  • 实例讲解-整合iBATIS和Spring在WEB开发中的应用
  • 搞定了Jms的ObjectMessage对象传递 ,这个项目可以告一段落了
  • 流行FrameWork整合之Spring??—— IoC反向控制篇
  • 流行FrameWork整合之Spring??—— IoC反向控制篇
  • Webwork 2.2的Action是否使用Spring的prototype?获取的性能对比
  • 流行FrameWork整合之Spring??—— IoC反向控制篇
  • 流行FrameWork整合之Spring??—— IoC反向控制篇
  • 【随机文章】
  • 枚 举(enum)
  • 将表数据生成SQL脚本的存储过程
  • 学习MySQL多表操作和备份处理
  • Maya 4.0 创建场景物体-使用摄像机(3)
  • 在工具栏中使用真彩色图标
  • 分析Windows NT/2000堆内存与虚拟内存组织
  • 新版CCNA考试内容说明
  • Windows XP显示技巧六则
  • 心灵的选择
  • 再提供两套清纯美女扑克
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.