[转载]Enterprise JavaBeans 入门:将 EJB 部署到 WebSphere 应用服务器

Enterprise JavaBeans 入门

将 EJB 部署到 WebSphere 应用服务器


本章主要讲述IBM WebSphere 应用服务器 (WAS) 基本架构和将一个 bean 部署到 WAS 的基本步骤。

本章讲述内容

  • IBM WebSphere 应用服务器 (WAS) 基本架构
  • 将一个 bean 部署到 WAS 的步骤

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


节点、服务器和容器

  • 节点:在您机器上运行的一个 WebSphere 应用服务器实例
  • 服务器
    • 在一个节点上运行的多个服务器
    • 对 Enterprise JavaBeans 和 servlet 引擎有不同服务器
  • 在一个服务器中的多个容器:每个容器可包含不定数目的 Bean

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


在多个服务器中的多个容器

  • 支持多个、独立、虚拟服务器
    • 每个服务器运行在一个单独的 Java VM 中
    • 有各自的工作目录、日志 ...
  • 每个服务器独立于其它的服务器:可以独立地启动/停止,而不会影响其它服务器中的容器运行
  • 每个容器有各自的设置
    • 数据库设置 (url, driver, user, password, ...)
    • 在一个服务器中的容器可以独立地启动/停止

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


EJB 部署的基本知识

  • 当您部属一个 EJB 时,WebSphere 将需要:
    • EJB 基本文件 (Remote, Home 接口, Bean 类)
    • EJB 部属文件 (stubs, ties 和其它生成的代码)
    • Dependent 类 (通常为常规 Java 类)
  • 部属代码也可以由 WebSphere 应用服务器来产生 (如果有需要,使用 Jetace 工具):如果从 VisualAge 部属,就不再需要了

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


从 VisualAge for Java 中导出

  • VisualAge 生成两个 JAR 文件
    • "deployed JAR" 文件是您要来部属到 WebSphere 的
    • "client JAR" 包含客户端应用所需所有代码
  • 缺省情况下,VisualAge 将导出您的 bean 的类:您将需要自己选择来导出 helper 类
  • 选中您的 EJB 组,然后选择 "EJB > Export > Deployed JAR"

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


导出到 WebSphere

  • 两个 EJB JAR 目录
  • DeployableEJBs:只包含三个基本 EJB 类的 JAR 文件
  • DeployedEJBs:包含三个基本 EJB 类和生成代码的 JAR 文件
ejb-5-1.jpg

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


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
ejb-5-2.jpg

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


打开 WebSphere 管理控制台

  • Start > Programs > IBM WebSphere > Application Server v3.5 > 管理控制台
    ejb-5-3.gif

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


部属您的 Bean

  • 运行 "Deploy Enterprise Bean"
    • 启动一个向导来一步步完成部属工作
    • 部属一个 Bean 最简单的方法
  • 遵循提示的步骤
    ejb-5-4.gif ejb-5-5.gif

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


启动您的服务器

ejb-5-6.gif →→→→→→→→ ejb-5-7.gif

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


在 VisualAge 中测试

  • 您可以使用生成的测试客户机来调试您的应用
  • 将 Provider URL 改成端口 900
  • 如果 EJB 服务器运行在其它机器上时要修改主机地址
ejb-5-8.gif
  • 您将需要一个有 main() 方法的客户端程序
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));
}


blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


配置一个客户端应用

  • 客户端 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




blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


从一个 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);
}


blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


重新部署 Bean

  • 重新导出您修改后的 Bean:Deployed EJB JAR 文件
  • 停止容器 (或服务器,或节点)
  • 删除 Bean
  • 重新安装 Bean:鼠标右键单击容器,选择 "Create > Enterprise Bean"
  • 重新启动容器 (或服务器,或节点)

blue_rule.gif
c.gif
c.gif
u_bold.gif回页首


注意!

  • 有很多种方法来部属 EJB
  • 您看到的是最简单明了的
  • 还可以通过 XMLConfig 工具来批处理部属 EJB
  • 或者使用 WSCP 编写脚本

本章讲述内容

  • 要部属一个 Bean,您必需:
    • 导出导一个 Deployed EJB JAR 文件中
    • 使用 "Deploy Enterprise Bean" 任务来部属 EJB 到 WebSphere 中的 服务器/容器中
    • 启动服务器
  • 在应用服务器中运行多个服务器
    • 每个有自己的 VM
    • 有自己的定制设置
  • 多个容器运行在一个服务器中:每个有单独设置

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-130485/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/374079/viewspace-130485/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值