t;/>
</property>
</bean>
清单8. JMS Receiver配置
<bean id="jmsReceiver" class="springexample.client.JMSReceiver">
<property name="jmsTemplate">
<ref bean="jmsTemplate"/>
</property>
</bean>
测试及监视
我写了一个测试类,命名为LoanApplicationControllerTest,用来测试LoanProc程序。我们可以使用这个类来设定贷款参数以及调用信用请求服务类。
让我们看一下不使用Spring JMS API而使用传统JMS开发途径的消息发送者实例。清单9显示了MessageSenderJMS类里的sendMessage方法,其中包含了使用JMS API处理消息的所有必需步骤。
使用Spring JMS轻松实现异步消息传递(7)
时间:2011-01-15 Srini Penchikala
清单9. 传统JMS实例
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的消息发送者实例。清单10显示了MessageSenderSpringJMS类中send方法的代码。
清单10. 使用Spring API的JMS实例
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();
}
}
如您所见,通过使用配置文件,所有与管理JMS资源有关的步骤都将 |