使您的应用程序调用我的应用程序,第 1 部分: Apache Geronimo 通信基础 —— 开发、部署和测试(下)
2010-03-19 00:00:00 来源:WEB开发网成功部署应用程序后,您将接收到如 图 14 所示的消息。
图 14. 部署成功
测试
最后,您可创建一个客户机或测试程序来测试您的应用程序。
创建客户机
您的客户机程序(参见 清单 14)将向 POTopic 发布一条带有采购请求信息的 JMS 消息。由于 PurchaseOrderMDB 侦听 POTopic,服务器将调用 onMessage() 方法,此方法又会调用 PurchaseOrderEJB 在数据库中创建采购订单。
客户机 PurchaseOrderPublisher.java 位于 $part1.home/src/examples/po/test 目录中。
清单 14. 发布 JMS 消息的客户机程序
public class PurchaseOrderPublisher {
public static void main(String a[]) throws Exception {
Logger log = Logger.getLogger("PurchaseOrderPublisher");
javax.jms.TopicConnection conn = null;
javax.jms.TopicPublisher publisher = null;
javax.jms.TopicSession session = null;
try {
// JMS JNDI Properties for Apache Geronimo Server
java.util.Hashtable env = new java.util.Hashtable();
env.put("java.naming.factory.initial",
"org.activemq.jndi.ActiveMQInitialContextFactory");
env.put("java.naming.provider.url",
"tcp://localhost:61616");
env.put("topic.POTopic", "POTopic");
env.put("connectionFactoryNames", "POConnectionFactory");
// create a new initial context with jndi properties
javax.naming.Context ctx =
new javax.naming.InitialContext(env);
// lookup the connection factory
javax.jms.TopicConnectionFactory factory =
(javax.jms.TopicConnectionFactory) ctx
.lookup("POConnectionFactory");
// create a new TopicConnection
conn = factory.createTopicConnection();
javax.jms.Topic mytopic =
(javax.jms.Topic) ctx.lookup("POTopic");
// create a new TopicSession
session = conn.createTopicSession(false,
javax.jms.Session.AUTO_ACKNOWLEDGE);
//create a Topic Publisher to publish messages to 'POTopic'
publisher = session.createPublisher(mytopic);
// start the topic connection
conn.start();
// create a new purchase order with test data
PurchaseOrderBean poBean = new PurchaseOrderBean();
Random random = new Random();
String poNum = "PO" + random.nextInt();
poBean.setPurchaseOrderNum(poNum);
poBean.setItem("Pens");
poBean.setDescription("Need pens for Marketing dept.");
poBean.setUnitPrice(new Integer(2));
poBean.setQuantity(new Integer(100));
poBean.setRequestorEmail("user1@localhost.com");
// create a jms message
javax.jms.ObjectMessage poMsg =
session.createObjectMessage(poBean);
// publish the jms message to 'POTopic'
publisher.publish(poMsg);
} catch (Exception e) {
log.severe("Error Occurred: " + e);
} finally {
// release all jms resources
if (publisher != null)
publisher.close();
if (session != null)
session.close();
if (conn != null)
conn.close();
}
}// end main
}// end class
更多精彩
赞助商链接