使用Spring JMS简化异步消息处理
来源:互联网 发布:java webservice pdf 编辑:程序博客网 时间:2024/04/30 04:16
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
org.jnp.interfaces.NamingContextFactory localhost org.jnp.interfaces:org.jboss.naming UIL2ConnectionFactory queue/CreditRequestSendQueue queue/CreditReqeustReceiveQueue 30000 我们开始认清Spring的真面目把。无厘头的配置,XML的梦魇。从一种混乱到另一种还乱。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=607922
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 使用Spring JMS简化异步消息处理
- 写在Blog访问量超过15万次
- 脚本的威力:新一期的IBM周刊有四篇PHP的教程
- 感谢CSDN提名我候选2006最有价值博客,感谢朋友们的投票
- ESB的几种模式
- 业务人员用的软件=BPEL+SOA
- 使用Spring JMS简化异步消息处理
- 2007世界各国GDP排名
- Symbian OS上的文件操作和UNICODE转换
- 从一个工作流流转中人员设定的例子看WBISF流程引擎所定义的用户授权安全模型
- VS 2005 & SQL Server 2005 on Windows Vista
- 世界上最简单的模板,比Smarty,Xtemplate还要强大
- 用Mind Map 激发你的记忆潜能
- 脚本的威力:新一期的IBM周刊有四篇PHP的教程
- 2006 Java技术的赢家和输家