级别: 初级 Colin Yu (coliny@ca.ibm.com), WebSphere 业务场景技术设计师, IBM 多伦多实验室
2004 年 9 月 01 日 独立的 JMS Application(胖客户机)使您从为每个客户机上安装 WebSphere Application Client 的需求中解脱出来,但是它需要为客户机配置运行时环境,包括命名查找和类路径。本文详细介绍了 WebSphere Studio V5.1。
引言
文章
运用WebSphere Studio V5开发JMS Application 阐述了使用 WebSphere Studio 在 J2EE 应用程序客户端模块中开发点到点 JMS 应用程序的方法。
通过 IBM® WebSphere® Studio 自带的 Application Client Launcher 处理所有的环境设置。
在把 J2EE 应用程序模块部署到生产环境中之前,您必须在每个客户机上安装 WebSphere Application Client CD 里的 Application Client,
它是 J2EE 应用程序客户机的运行时环境。
运行在 WebSphere Application Client 上的应用程序客户机类似于运行在浏览器的瘦客户机。
另一方面,独立的 JMS Application(胖客户机),使您从为每个客户机安装 WebSphere Application Client 的需求中解脱出来。
然而,您需要配置运行时环境,包括客户机的命名查找和类路径。并且,在独立的 JMS 应用程序的单元测试中,不能使用 WebSphere Studio 的 MQ Simulator for Java™ Developers。与在以前的文章中创建的一样,本文将使用类似的编程方法,并且研究在 WebSphere Studio V5.1 里有关独立的 JMS 应用程序编码以及环境设置步骤方面的差异。
使用合适的配置创建 Java 项目
首先,为 JMS 应用程序创建 Java 项目并且配置它的运行时环境。
创建新的 Java 项目
- 打开 WebSphere Studio 的
Java透视图。
- 右键单击 Package explorer 视图,选择
New =>Project。
- 在新项目向导中,选择
Java =>Java Project。
- 输入项目名称
Point2Point ,然后单击
Finish。
图 1. 创建新 Java 项目
编辑项目的 Java 创建路径。
- 右键单击新创建的
Point2Point 项目,在上下文菜单中选择
Properties。
- 在左边的导航栏中,选择
Java Build Path。
- 在 Java Build Path 栏中,选择
Libraries:
图 2. 编辑 Java 创建路径
- 单击
Add Variable。
- 在 New variable classpath entry 对话框,选择
WAS_50_PLUGINDIR,然后单击
Extend:
图 3. 定位变量
- 从
mqjms\java\lib 文件夹中选择下列 JAR 文件(可以使用 Ctrl 键选择多个文件):
-
com.ibm.mq.jar
-
com.ibm.mqbind.jar
-
com.ibm.mqjms.jar
图 4. 添加 JAR 文件
- 重复步骤 5,在
lib 文件夹中选取下列 JAR 文件:
-
ecutils.jar
-
j2ee.jar
-
messaging.jar
-
messagingClient.jar
-
messagingImpl.jar
-
namingclient.jar
- 选择
OK来关闭 Properties 对话框。项目的 resulting .classpath 应该如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/mqjms/Java/lib/com.ibm.mq.jar"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/mqjms/Java/lib/com.ibm.mqbind.jar"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/mqjms/Java/lib/com.ibm.mqjms.jar"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/lib/ecutils.jar"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/lib/messaging.jar"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/lib/messagingClient.jar"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/lib/messagingImpl.jar"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/lib/namingclient.jar"/>
<classpathentry kind="var" path="WAS_50_PLUGINDIR/lib/j2ee.jar"/>
<classpathentry kind="output" path=""/>
</classpath>
|
上面的 JAR 文件要比编译代码时需要的多。它们中的一些是关于运行时的。如果遗漏它们中的一些,那么当启动应用程序时,可能会得到
java.lang.ClassNotFoundException或
java.lang.ClassCastException。
将属性文件导入到项目中
- 右键单击
Point2Point 项目,在上下文菜单中选择
Import...。
- 选择
File system作为输入源,然后单击
Next。
图 5. 从文件系统导入属性文件
- 单击
Browse,并找到
WsInstallDir\runtimes\base_v5\properties 文件夹,其中
WsInstallDir
是安装 WebSphere Studio 的文件夹。
- 选择
implfactory.properties并且单击
Finish。仔细检查确保文件已经导入到
Point2Point
项目的根目录。
图 6. 定位文件
创建点到点的独立 JMS 应用程序
该部分创建的应用程序基于
运用WebSphere Studio V5开发JMS Application 文章中的代码。
我们仅仅突出了有关独立 JMS 应用程序命名查询的不同。要获取全面详细的逐行解释,请参阅以前的文章。
导入代码
现在需要为点到点应用程序导入代码。
- 首先,下载并且解压缩本文带有的源代码。
- 右键单击
Point2Point项目,在上下文菜单中,选择
Import导入代码。
- 在接下来的
File Import向导中,选择
File system并且单击
Next。
- 在
File System Import向导中,如下面的图 3 所示,选择
Browse并且找到您把源代码解压缩后的目录。
- 检查
PointToPoint.java,然后单击
Finish。
检查代码
在独立的应用程序中,您需要指定 JNDI 属性来获得 InitialContext 对象。这是独立的应用程序和应用程序客户机之间的主要差别,应用程序客户机不需要指定 JNDI 属性。
String icf = "com.ibm.websphere.naming.WsnInitialContextFactory";
String url="iiop://localhost:2809";
// Initialise JNDI properties
Hashtable env =
new Hashtable();
env.put( Context.INITIAL_CONTEXT_FACTORY, icf );
env.put( Context.PROVIDER_URL, url );
final InitialContext context =
new InitialContext(env);
|
下面的 JMS 代码在独立的应用程序和应用程序客户端中是相同的。
// Lookup the queue connection factory from the initial context
final QueueConnectionFactory qcf = (QueueConnectionFactory) context.lookup("QCF");
// Create a new queue connection from the queue connection factory.
final QueueConnection conn = qcf.createQueueConnection();
// Start the connection
conn.start();
// Create a new queue session from the queue connection. The session should not be transacted
// and should use automatic message acknowledgement.
final QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
// Lookup the queue to be used to send and receive
// messages from the initial context.
final Queue q = (Queue) context.lookup("Q");
// Create a new queue sender using the queue session.
// The sender should be created to send messages to
// the queue q.
final QueueSender sender = session.createSender(q);
// Create a text message using the queue session.
// Initialize the message"s data to a String of your choice.
final TextMessage sentMessage = session.createTextMessage("My first JMS message.");
// Send the sentMessage object using the queue sender.
sender.send(sentMessage);
// Create a new queue receiver using the queue session.
// The queue receiver should be created to receive
// messages from the queue q.
final QueueReceiver receiver = session.createReceiver(q);
// Use the queue receiver to receive the message that
// was sent previously.
final TextMessage receivedMessage = (TextMessage)receiver.receiveNoWait();
// Output the received message to System.out
System.out.println("Message Received: " + receivedMessage.getText());
|

 |

|
使用 WebSphere Studio 测试 JMS 点到点应用程序
在该部分中,我们将创建一个 WebSphere Application Server 实例并且定义服务器配置时 JMS 应用程序所使用的相应的 JMS 管理对象,然后测试应用程序。
配置 WebSphere Studio 测试环境
在运行
PointToPoint 应用程序之前,需要创建和配置测试环境。对于独立的 Java 客户机和 J2EE 应用程序客户机,这一步是类似的。下列的步骤中部分是来自 Andy Wilkinson 的文章,在这些步骤中,我们将突出有关独立的 JMS 应用程序的不同并且在 WebSphere Studio V5.1 中更改:
- 在
J2EE 透视图中,切换到
J2EE Hierarchy视图。右键单击
Servers条目并且选择
New=>
Server and Server Configuration打开
Server Creation 向导,如下面的图 7 所示。指定服务器名为
server1 并选择
WebSphere version 5.0=>
Test Environment的服务器类型。单击
Finish完成测试服务器的创建。
图 7. 创建新的服务器实例
- 在创建了新的测试环境服务器后,必须更新它的配置以便包含
PointToPoint 应用程序代码从 JNDI 中查找到的 JMS 对象。在 J2EE Hierarchy 中双击
server1,打开它的编辑器,转换到
JMS选项卡,显示 WebSphere JMS Provider Options 面板。
- 在该面板中,首先需要检查的是正在被更改的
Server Settings。在面板的顶部,确保
Server Settings是展开的而
Cell Settings和
Node Settings没有展开。您必须为 JMS 服务器增加一个队列供
PointToPoint 应用程序发送和接受它的消息。在面板的 JMS Server Properties 部分,单击
Add,输入
PointToPointQ ,再单击
OK。
- MQ Simulator for Java Developers 不支持运行在应用程序服务器 JVM 进程外面的客户机。需要安装
WebSphere Embedded Messaging 或
WebSphere MQ作为 JMS 提供程序。这里我们假设已经安装了 Embedded JMS Messaging ,所以选择
Embedded Messaging 作为 JMS Provider。现在 JMS Server 的配置应该类似于图 8:
提示:如果选择 MQ Simulator for Java Developers 作为 JMS Provider,那么当启动应用程序时将会得到
com.ibm.mq.MQException:MQJE001:Completion Code 2, Reason 2012。
图 8. 配置 JMS 服务器属性
- 配置完嵌入的 JMS 服务器后,现在必须在测试环境服务器的 JNDI 名称空间中配置所需的 QueueConnectionFactory 和 Queue 对象。在 WebSphere JMS Provider Options 面板的 Server Settings 部分,向下移动到 JMS Connection Factories 部分,到 WASQueueConnectionFactories 条目表并且单击它右边的
Add按钮。在接下来的向导中,指定
QCF 的 Name 和 JNDI 名称,利用下拉式列表框,分别指定作为本地主机和 server1 的 Node 和 Server Name。根据图 9 所示检查配置的更改,单击
OK。
图 9. 配置 JMS QueueConnectionFactory 属性
- 配置的最后一步是为在上面的 JMS 服务器中配置的队列创建 JNDI 条目。向下移动到 JMS Destinations 部分的 WAS Queue 条目表,单击右边的
Add按钮。在接下来的向导中,指定
PointToPointQ 的 Name(该值必须和 JMS 服务器中创建的队列的名称相匹配)和
Q 的 JNDI Name(该值必须和用在应用程序代码中查找对象的名称相匹配)。使用下拉式列表框,指定本地主机的 Node。根据图 10 中所示检查配置的更改,然后单击
OK。
图 10. 配置 JMS Queue 属性
- 保存更新的服务器配置,通过右键单击
Servers视图的 Servers 选项卡中的条目启动服务器,选择
Start。现在准备运行
PointToPoint 应用程序。
提示:如果您使用 WebSphere MQ 作为 JMS 提供程序,那么需要通过 WebSphere Admin Console 而不是服务器配置编辑器来配置 JMS 管理对象。
使用 WebSphere Studio 启动 JMS 点到点应用程序
您可以从常见的 Java 启动器启动应用程序。
- 如果您不在 Java 透视图中,通过单击左边相应的图标切换到 Java 视图。
- 在 Package explorer 视图,选择
PointToPoint.java。
- 在菜单中,选择
Run=>
Run as =>Java application。
- 应用程序的输入如图 11:
图 11. 启动应用程序
结束语
本文突出了为独立的 JMS 应用程序配置运行时环境和修改代码的方法。您可以在任何 JMS 应用程序开发中应用本文学到的同样步骤。
参考资料
关于作者  | 
|  |
Colin Yu 是 IBM 多伦多实验室业务场景开发组的技术设计师。Colin 于 1995 年从安大略省的 University of Waterloo 获得了工程学士学位,并于 2000 年从该校获得了应用科学的硕士学位。他是 IBM 认证的 WebSphere Application Server 方面的企业开发人员和系统专家,还是 IBM 认证的 WebSphere Studio Application Developer 和 VisualAge for Java 方面的解决方案开发人员。
您可以通过
coliny@ca.ibm.com与 Colin 联系。
|
对本文的评价
|