当前页面: 开发资料首页 → Eclipse 专题 → IBM WebSphere 开发者技术期刊 : 使用 Eclipse 富客户端平台和 Rational Application Developer V6 创建 EJB 客户端
摘要: This article shows how to build a sample EJB client using the Eclipse Rich Client Platform (RCP), which has become increasingly popularity due to its extensible nature. Besides focusing on how to use Rational Application Developer to build an RCP application, this article also details how to configure an Eclipse RCP application to act as a J2EE application client.
引言
最近几年来,作为编写交互式开发环境(IDE)工具的框架 —— Eclipse 平台的流行程度显著增长。 关于 Eclipse 3.0 版本,同样的技术已经扩展为支持富客户端的开发,给予 Java? 开发者使用有自身感观的可扩展平台编写富客户端代码的能力。
开发富客户端的平台通称 Eclipse Rich Client Platform (RCP)。为什么着重强调富客户端呢?在 n-tiered 体系结构的这几年来,很明显开发给予终端用户丰富的使用浏览器方面经验的应用程序比较困难。基于浏览器的应用程序常常设法通过将 JavaScript 与 HTML 结合起来模拟某种可用性。然而,很明显对于应用程序来说,寻求更丰富的经验比使用更丰富的客户端技术平台要好。同样,Eclipse RCP 给予了开发者创建可扩展客户端应用程序的能力,并且这些可扩展客户端应用程序仍然可以加入到 n-tiered 体系结构中。Eclipse RCP 启用了下一代富客户端,并且是富客户端平台(例如 IBM Workplace Client 技术)的基础。
IBM Rational Application Developer V6 (以前是 IBM WebSphere? Studio Application Developer)是基于 Eclipse 3.0 平台的。这个版本使 J2EE 开发者可以创建与 J2EE 应用程序进行交互的富客户端应用程序。在这篇文章中,我们将使用 Rational Application Developer(以下称之为 Application Developer)中的 Eclipse 3.0 功能构建 Eclipse RCP 客户端,它们调用了配置于 Application Developer 的 WebSphere Application Server 测试环境中的 EJB 组件。
为了构建 Eclipse RCP 应用程序,它调用了运行于 WebSphere Application Server V6 内部的企业 bean,我们将会遵循的一般步骤包括:
为了进行这个练习,您需要:
C:\
的根目录下。设置 J2EE 应用程序
首先我们需要做的是安装适合于 WebSphere Application Server 的 IBM Application Client,它已经包括在 WebSphere Application Server V6(如果您没有客户端,您可以将您的配置改变为针对 Application Developer 测试环境中的服务器 JDK)中,并且接下来将应用程序部署到正在运行的 WebSphere Application Server:
C:\WebSphere6ND\AppClient directory
上。下载 ZIP 文件中的 Windows? 批处理(BAT)文件使用了这个类路径;如果您选择了另一个目录,您就必须修改批处理文件转而引用新的目录。图 1 展示了向导的第三个界面您所选择的目录。
localhost
作为主机名,如图 2 所示。
C:\RCPEJBClient\workspace
的工作区,(图 3)。
/runtimes/profiles/default/bin
启动服务器以及执行 startserver server1
)。
http://localhost:9060/admin
)运行管理控制台,如图 7 中所示。
C:\RCPEJBClient
,选择 TemperatureEAR.ear 并且接下来选择 Next (图 10)。TemeratureEAR 应用程序是将温度由摄氏温度转换成华氏温度的简单方法(尽管可能不是很精确)。
创建 RCP 客户端
我们现在已经为创建 RCP 客户端应用程序做好了准备,我们将会通过使用 Plug-in 向导将其用作简单的插件。当创建 RCP 应用程序时,随着对复用性的关注您应该常常创建若干插件。例如,您可以重写该实例来创建关于 J2EE 相关性的单点插件,另一种视图组件,以及 RCP Shell 的一种。这样使得不同的插件可以重用,例如,J2EE 代码。然而,我们将只能拥有这一运用的唯一插件,从根本上限制了这篇文章的作用域。
创建插件项目
com.ibm.issw.temperature.form
。这是一种很普通的插件命名约定。确定您选择的选项如图 24 所示:导入 J2EE JAR 和 EJB client JAR
现在我们将会把两个 JAR 导入我们的新插件中:
j2ee.jar
—— 插件与 EJB 客户端 API 进行交互,并且因此在您的类路径中您需要 j2ee.jar
。
j2ee.jar
的地方。因为 RCP 将使用运行时的 IBM Application Client for WebSphere Application Server,所以最好使用 JAR,您可以在 /lib
中找到它,如图 32 所示。确定 j2ee.jar
是唯一的选择。(如果您的插件使用其它的 WebSphere API,例如 WebSphere 程序设计模型扩展,您便会同样需要将其导入。否则,您无需导入其它 WebSphere 运行时 JAR;在运行期间,已生成的 EJB 代理服务器代码以及客户端容器代码装载 WebSphere 瘦客户端类库加载器中的类。)如果您没有安装客户端软件,您就能够从 /runtimes/base_v6/lib or /lib
中获得 j2ee.jar
。
Create EJB Client Jar
生成客户端 JAR)对于这一运用,download file 中提供了 EJB 客户端 JAR。使用文件系统选项导入 JAR 文件,如前所述,转到 C:\RCPEJBClient
然后选择 TemperatureEJBClient.jar
。(图 33)
build.properties
文件。您可以通过选择 build.properties 选项卡浏览该文件。plug-in.xml
打开。)选择 Runtime 选项卡,并且在运行时程序库部分,选择 Add 按钮(图 37)然后添加您刚刚导入的 JAR。
运行时对话框更新了插件的 MANIFEST.MF
文件。与 J2EE 应用程序很相似,清单定义了运行时类路径。您可以通过定位到 MANIFEST.MF 选项卡查看清单。
使用 SWT 布局小窗口构建视图
现在我们将要构建视图。download file 中提供了 View 类。我们将会导入该类(装载我们通过视图模板生成的类)并且接下来检验代码的相关部分。启动之前,我们需要确定我们的插件所依赖的插件。在视图中我们将会使用标准窗口小部件工具箱(SWT)布局小窗口,因此我们需要设置包括布局小窗口的 Eclipse 插件的相关性。
清单 1
<table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
org.eclispe.ui.forms
插件。
src
文件夹下,右键单击com.ibm.issw.temperature.form.views 包,然后选择 Import (图 40)。
C:\RCPEJBClient\View
,并且将三个文件导入包中,如图 41 所示。
</td></tr></table>
public void createPartControl(Composite parent)
{
toolkit = new FormToolkit(parent.getDisplay());
form = toolkit.createScrolledForm(parent);
form.setText(RcpMessages.getString("TemperatureView.Title"));
GridLayout layout = new GridLayout(2,false);
form.getBody().setLayout(layout);
Label label = toolkit.createLabel(form.getBody(),
"Enter temperature in Celsius: ");
Text textBox = toolkit.createText(form.getBody(), "", SWT.BORDER);
GridData gdText = new GridData(8);
textBox.setLayoutData(gdText);
Button button = toolkit.createButton(form.getBody(),
RcpMessages.getString("TemperatureView.Button"),
SWT.PUSH);
Label result = toolkit.createLabel(form.getBody(), "");
result.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
MyListener listener = new MyListener();
listener.setInput(textBox);
listener.setResult(result);
listener.setParent(parent);
listener.setFormToolkit(toolkit);
button.addListener(SWT.Selection,listener);
}
清单 2. <table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
public void handleEvent(Event event)
{
try
{
InitialContext ctx = new InitialContext();
String jndi = RcpMessages.getString("TemperatureView.JNDIName");
Object obj = ctx.lookup(jndi);
TempConverterHome homeRef = (TempConverterHome) PortableRemoteObject
.narrow(obj, TempConverterHome.class);
TempConverter serviceRef = homeRef.create();
retVal = serviceRef.celciusToFarenheight(Integer.parseInt(textBox.getText()));
System.out.println("TemperatureAction.testEJB EJB Successful
Return=" ++ retVal);
result.setText(textBox.getText() + " degrees Celsius is
approximately " +
Integer.toString(retVal) + " degrees Fahrenheit!");
} catch (Exception th)
{
System.out.println("TemperatureAction.testEJB EJB call ex="
+ th);
th.printStackTrace();
result.setText(th.getClass().getName() + ": " +
th.getMessage());
}
}
</td></tr></table>清单 3 <table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
TemperatureView.JNDIName=ejb/TempConverter
TemperatureView.Title=Celsius to Fahrenheit Temperature Converter
TemperatureView.Button=Convert to Fahrenheit
</td></tr></table>plug-in.xml
文件并且选择扩展点(在该情况下,您扩展了 View 插件)。对于我们来说这是额外的,因为我们在向导中使用 View 模板。您可以在 Extensions 选项卡中查看或者您可以浏览 plug-in.xml
资源。扩展配置如清单 4 所示。清单 4 <table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
</td></tr></table>创建您自己的 RCP 工作台和透视图
因为我们正在开发 RCP 应用程序,使用缺省的 Eclipse 工作台,工作台是按照 IDE 定制的,不是很理想的。因此,我们将会构建我们自己的工作台以及透视图。我们将会导入类,下载文件提供了这些类,导入到我们的插件中,接下来向工作台以及透视图的
清单 5
<table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>plug-in.xml
中添加条目。
C:\RCPEJBClient\RCPCode\src
文件夹并且导入三个类,如图 46 所示。
(默认包)
,它是根(图 47)。
</td></tr></table>
public class RcpWorkbenchAdvisor extends WorkbenchAdvisor {
public String getInitialWindowPerspectiveId() {
return "MyRCPPerspective";
}
public void preWindowOpen(IWorkbenchWindowConfigurer configurer) {
super.preWindowOpen(configurer);
configurer.setInitialSize(new Point(750, 600));
configurer.setShowCoolBar(false);
configurer.setShowStatusLine(false);
configurer.setTitle("Temperature Converter");
}
}
清单 6 <table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
public class MyRCPPerspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
layout.addView("com.ibm.issw.temperature.form.views.TemperatureView",
IPageLayout.LEFT,0.66f,IPageLayout.ID_EDITOR_AREA);;
layout.setEditorAreaVisible(false);
}
}
</td></tr></table>清单 7 <table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
public class TempRCPApplication implements IPlatformRunnable {
public Object run(Object args) throws Exception {
WorkbenchAdvisor workbenchAdvisor = new RcpWorkbenchAdvisor();
Display display = PlatformUI.createDisplay();
try {
int returnCode = PlatformUI.createAndRunWorkbench(display,workbenchAdvisor);
if(returnCode == PlatformUI.RETURN_RESTART)
{
return IPlatformRunnable.EXIT_RESTART;
}
else
{
return IPlatformRunnable.EXIT_OK;
}
} finally {
display.dispose();
}
}
}
</td></tr></table>plug-in.xml
中去。选择 Extension 选项卡,然后接下来选择 Add (图 48)。
org.eclipse.core.runtime.applications
(图 49)。
RcpApplication
(图 50)。
换句话说,您可以通过手动直接将它添加到 plug-in.xml
文件中,如清单 8 所示。附加的代码如黑体字所示:
清单 8 <table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
</td></tr></table>测试您的 RCP EJB 客户端
现在我们已经为运行 RCP EJB 客户端做好了准备。我们首先将会从 Application Developer 内部进行 RCP 应用程序的单元测试。接下来我们将会导出插件并且为在开发环境外部运行对其进行设置。
Application Developer 内部测试
关于 Eclipse 3.0 版本,您可以为了运行插件创建 launch configurations。这些 launch configuration 为了使用 WebSphere V6 JRE 需要进行设置(包括了 IBM Application Client for WebSphere Application Server 以及 WebSphere Application Server),通过 Perspective Preferences 进行添加。然而,不是使用 Eclipse,而是我们将会使用Application Developer,其中已经安装了 JRE。
C:\RCPEJBClient\Snippet1.txt
所提供的代码(清单 9)输入如图 61 所示代码。还不能运行。VM arguments 是针对安装 IBM Application Client for WebSphere Application Server 目录。如果您将它安装在不同的目录下,是用那个目录。否则,使用 Application Developer 中定位于 /runtimes/base_v6
的 WebSphere 实例。清单 9 <table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
-Djava.naming.provider.url=corbaloc::localhost
-Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory
-Dserver.root=C:\WebSphere6ND\AppClient
-Dcom.ibm.CORBA.ConfigURL=file:C:\WebSphere6ND\AppClient/properties/sas.client.props
-Djava.ext.dirs="C:\WebSphere6ND\AppClient\java\jre\lib\ext;
C:\WebSphere6ND\AppClient\java\jre\lib;C:\WebSphere6ND\AppClient\classes;
C:\WebSphere6ND\AppClient\lib;C:\WebSphere6ND\AppClient\lib\ext;
C:\WebSphere6ND\AppClient\properties
</td></tr></table>构建并运行 Application Developer 外部的 RCP 应用程序
为了运行 Application Developer 外部的 RCP 应用程序,我们将会需要使用导出插件向导。在 Eclipse 3 版本中,不存在任何 RCP 应用程序的导出机制,但您可以使用二进制构建完成这一流程。出于教学的目的,我们将亲自进行部署。
清单 10
<table bgcolor="#eeeeee" width="100%" cellpadding="5" cellspacing="0" border="1"><tr><td>
C:\RCPEJBClient\MyRCPApp.bat
复制到您导出插件的目录。C:\WebSphere6ND\AppClient
;否则,为了替换目录通过针对应用程序客户端更新初始的变量而更新脚本,或者针对 WebSphere Application Server 如果您不使用客户端的话)。
(我们在测试关于 WebSphere Application Server Base 的 RCP 应用程序。如果,在生产中,您将会调用部署到集群的 EJB 应用程序,参数 Djava.naming.provider.url=corbaloc::localhost
应该拥有表示像这样一个集群:corbaloc::
的 URL。)
</td></tr></table>
set WAS_CLIENT_HOME=C:\WebSphere6ND\AppClient
start %WAS_CLIENT_HOME%\java\jre\bin\javaw
-Djava.naming.provider.url=corbaloc::localhost
-Djava.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory
-Dserver.root=%WAS_CLIENT_HOME%
-Dcom.ibm.CORBA.ConfigURL=file:%WAS_CLIENT_HOME%/properties/sas.client.props
-Djava.ext.dirs=%WAS_CLIENT_HOME%\java\jre\lib\ext;%WAS_CLIENT_HOME%\java\jre\lib;%WAS_CLIENT_HOME%\
classes;%WAS_CLIENT_HOME%\lib;%WAS_CLIENT_HOME%\lib\ext;%WAS_CLIENT_HOME%\properties
-cp startup.jar org.eclipse.core.launcher.Main
-application com.ibm.issw.temperature.form.RcpApplication
startup.jar
以及基础插件。在 Application Developer 之中,基础 Eclipse 定位到 Application Developer 安装目录下的 eclipse
目录(图 69)。
startup.jar
到配置插件的目录。
org.eclipse.core.expressions_3.0.0
org.eclipse.core.runtime_3.0.1
org.eclipse.help_3.0.0
org.eclipse.jface_3.0.0
org.eclipse.osgi_3.0.1
org.eclipse.platform_3.0.1.1
org.eclipse.swt.win32_3.0.1
org.eclipse.swt_3.0.1
org.eclipse.ui.forms_3.0.0
org.eclipse.ui.workbench_3.0.1.1
org.eclipse.ui_3.0.1
org.eclipse.update.configurator_3.0.0
MyRCPApp.bat
运行应用程序。(再一次,初始请求会比后来的请求花费更长的时间。)
结束语
这篇文章描述了如何使用 Eclipse Rich Client Platform (RCP)构建 EJB 客户端应用程序。Eclipse 平台包括了大量的窗口、视图、框架(例如 Eclipse Modeling Framework),以及其它能够用于构建强大客户端应用程序的的扩展。了解如何使 Eclipse RCP 应用程序与 WebSphere Application Server 一体化对于构建一体化客户端应用程序是必不可少的。
致谢
作者由衷地感谢 Wayne Beaton 对本篇文章的审阅与改进。
下载
<table width="100%" class="data-table-1" cellspacing="0" cellpadding="0" border="0"><tr><th>描述</th><th>名字</th><th style="text-align:right;">大小</th><th>下载方法</th></tr><tr><td class="tb-row">Sample Rich Client Platform application</td><td nowrap="nowrap">RCPEJBClient.zip</td><td style="text-align:right;" nowrap="nowrap">1.6 MB</td><td nowrap="nowrap"> ?FTP|HTTP</td></tr></table><table cellspacing="0" cellpadding="0" border="0"><tr valign="top"><td colspan="5"></td></tr><tr><td></td><td>关于下载方法的信息</td><td></td><td></td><td>Get Adobe® Reader®</td></tr></table>
<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="middle">
</td><td valign="top" align="right"></td></tr></table></td></tr></table>
作者简介<table border="0" cellspacing="0" cellpadding="0" width="100%"><tr><td colspan="3"></td></tr><tr align="left" valign="top"><td>
</td><td></td><td width="100%">Roland Barcia 是位于纽约/新泽西州 Metro 区的 IBM Software Services for WebSphere 的 IT 顾问专家。他是书籍 IBM WebSphere: 部署和高级配置 的作者之一。
</td></tr></table>Patrick Nogay 是 IBM Software Services for WebSphere 的 IT 顾问专家。
</td></tr></table>
↑返回目录
前一篇: 集成 DB2 与 Apache Geronimo
后一篇: Apache Beehive 相关介绍