Enterprise JavaBeans 入门
将 EJB 部署到 WebSphere 应用服务器
本章主要讲述IBM WebSphere 应用服务器 (WAS) 基本架构和将一个 bean 部署到 WAS 的基本步骤。
本章讲述内容
- IBM WebSphere 应用服务器 (WAS) 基本架构
- 将一个 bean 部署到 WAS 的步骤
节点、服务器和容器
- 节点:在您机器上运行的一个 WebSphere 应用服务器实例
- 服务器
- 在一个节点上运行的多个服务器
- 对 Enterprise JavaBeans 和 servlet 引擎有不同服务器
- 在一个服务器中的多个容器:每个容器可包含不定数目的 Bean
在多个服务器中的多个容器
- 支持多个、独立、虚拟服务器
- 每个服务器运行在一个单独的 Java VM 中
- 有各自的工作目录、日志 ...
- 每个服务器独立于其它的服务器:可以独立地启动/停止,而不会影响其它服务器中的容器运行
- 每个容器有各自的设置
- 数据库设置 (url, driver, user, password, ...)
- 在一个服务器中的容器可以独立地启动/停止
EJB 部署的基本知识
- 当您部属一个 EJB 时,WebSphere 将需要:
- EJB 基本文件 (Remote, Home 接口, Bean 类)
- EJB 部属文件 (stubs, ties 和其它生成的代码)
- Dependent 类 (通常为常规 Java 类)
- 部属代码也可以由 WebSphere 应用服务器来产生 (如果有需要,使用 Jetace 工具):如果从 VisualAge 部属,就不再需要了
从 VisualAge for Java 中导出
- VisualAge 生成两个 JAR 文件
- "deployed JAR" 文件是您要来部属到 WebSphere 的
- "client JAR" 包含客户端应用所需所有代码
- 缺省情况下,VisualAge 将导出您的 bean 的类:您将需要自己选择来导出 helper 类
- 选中您的 EJB 组,然后选择 "EJB > Export > Deployed JAR"
导出到 WebSphere
- 两个 EJB JAR 目录
- DeployableEJBs:只包含三个基本 EJB 类的 JAR 文件
- DeployedEJBs:包含三个基本 EJB 类和生成代码的 JAR 文件
| | |
Deployed JAR 与 Client Jar 的区别
- Deployed JAR
- 三个基本 EJB 类
- 所有生成的类
- Stubs 和 ties
- 部属描述符(Deployment descriptor)
- manifest.mf 文件
|
- Client JAR
- Home 接口
- Remote 接口
- Home stub (2 个类)
- Bean stub (2 个类)
- manifest.mf
| |
打开 WebSphere 管理控制台
- Start > Programs > IBM WebSphere > Application Server v3.5 > 管理控制台
部属您的 Bean
- 运行 "Deploy Enterprise Bean"
- 启动一个向导来一步步完成部属工作
- 部属一个 Bean 最简单的方法
- 遵循提示的步骤
启动您的服务器
| →→→→→→→→ | |
在 VisualAge 中测试
- 您可以使用生成的测试客户机来调试您的应用
- 将 Provider URL 改成端口 900
- 如果 EJB 服务器运行在其它机器上时要修改主机地址
| |
|
public static void main(String[] args) throws Exception { Client client = new Client("iiop://rosebud:900/"); Calculator calculator = client.getCalculatorHome().create(); System.out.println(calculator.computeProduct(6.0,7.0)); } |
|
配置一个客户端应用
- 客户端 classpath 要求包括:
- Java runtime 类库
- [WAS_Install]libejs.jar
- [WAS_Install]libujc.jar
- 您的 EJB client JAR 文件
|
> java -classpath c:WebSphereAppServerlibejs.jar; c:WebSphereAppServerlibujc.jar; c:clientCalculatorClient.jar; c:client experiments.client.Client
|
|
从一个 servlet 调 Bean...
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println(""); try { double operand1 = new Double(request.getParameter("operand1")).doubleValue(); double operand2 = new Double(request.getParameter("operand2")).doubleValue(); Client client = new Client("iiop://rosebud:900"); Calculator calculator = client.getCalculatorHome().create(); out.println(calculator.computeProduct(operand1, operand2)); } catch (NumberFormatException e) { out.print("The parameters are not correctly formatted."); } catch (Exception e) { out.println("Your request cannot be processed at this time."); e.printStackTrace(out); } out.println(""); } |
|
- 最好 cache bean 的 home 接口:创建 context 可能开销很大
public synchronized MyBeanHome getMyBeanHome() throws NamingException { if (myBeanHome == null) myBeanHome = findMyBeanHome(); return myBeanHome; } |
|
private MyBeanHome findMyBeanHome() throws NamingException { Context context = getInitialContext(); Object object = context.lookup("MyBean"); context.close(); return (MyBeanHome)PortableRemoteObject .narrow(object, MyBeanHome.class); } |
|
重新部署 Bean
- 重新导出您修改后的 Bean:Deployed EJB JAR 文件
- 停止容器 (或服务器,或节点)
- 删除 Bean
- 重新安装 Bean:鼠标右键单击容器,选择 "Create > Enterprise Bean"
- 重新启动容器 (或服务器,或节点)
注意!
- 有很多种方法来部属 EJB
- 您看到的是最简单明了的
- 还可以通过 XMLConfig 工具来批处理部属 EJB
- 或者使用 WSCP 编写脚本
本章讲述内容
- 要部属一个 Bean,您必需:
- 导出导一个 Deployed EJB JAR 文件中
- 使用 "Deploy Enterprise Bean" 任务来部属 EJB 到 WebSphere 中的 服务器/容器中
- 启动服务器
- 在应用服务器中运行多个服务器
- 多个容器运行在一个服务器中:每个有单独设置
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-130485/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/374079/viewspace-130485/