Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > EJB(Enterprise JavaBeans)入门(10)
【标  题】:EJB(Enterprise JavaBeans)入门(10)
【关键字】:Java,is,EJB,10,Bean,Enterprise,JavaBean,Beans,Be,EJB,Enterprise,JavaBeans,10
【来  源】:网络

EJB(Enterprise JavaBeans)入门(10)

Your Ad Here ...
Employee employee = employeeHome.findById(42);
out.println(employee.getName());
out.println(employee.getEmail());
out.println(employee.getPhone());
... 两个远程方法调用 ...
EmployeeProxy employee =
employeeHome.findById(42).getProxy();
out.println(employee.getName());
out.println(employee.getEmail());
out.println(employee.getPhone());
... EmployeeBean method
public EmployeeProxy getProxy() {
EmployeeProxy proxy = new EmployeeProxy();
proxy.setName(getName());
proxy.setEmail(getEmail());
proxy.setPhone(getPhone());
proxy.setBirthdate(getBirthdate());
proxy.setStreet(getAddress().getStreet());
proxy.setCity(getAddress().getCity());
proxy.setState(getAddress().getState());
proxy.setCode(getAddress().getCode());
...
return proxy;
}

避免循环引用

  • 建议使用 Bean 间单向的引用
  • 互相引用的 Bean 可能造成死锁
  • 在 Bean 间的递归引用不再存在了

隐藏这些实体 Bean

不要将您的实体 Bean 直接暴露给客户端--使用一个会话 Bean 作为前端

· 隐藏实体 Bean:自动事务处理

为每个 Bean 的方法产生一个事务处理
客户端方法
void transferFunds(Account to, Account from, double amount)
throws RemoteException {
account1.debit(from); // TX_REQUIRED Assumed
account2.credit(to); // TX_REQUIRED Assumed
}
为 transferFunds 方法创建一个事务处理
TellerBean(会话 Bean)方法
void transferFunds(Account to, Account from, double amount)
throws RemoteException {
account1.debit(from); // TX_REQUIRED Assumed
account2.credit(to); // TX_REQUIRED Assumed
}

隐藏实体 Bean: Loose Coupling

  • 层次化的架构
    • 客户端不知道您在使用实体 Bean
    • 以后可以更换为其它技术,对客户端影响最小
    • 层次化架构无论对有无分布式对象和EJB都是有效的
  • 减少网络流量--使用 Proxy 对象在本地代表服务器端对象

Facade 模式...

  • Facade 提供进入您的系统的一个入口点
    • 由实体 Bean 提供的功能被隐藏起来
    • 它将调用功能,而不是实现功能
  • Facade 是一个会话 Bean (有状态或无状态)

  • 幕后的 Bean 不会被暴露给客户端
...
Teller facade = tellerHome.create();
facade.transferFunds(1234, 564, 2000.0);
...
TellerBean method
void transferFunds(int toId, int fromId, double amount)
throws FinderException, RemoteException {
Account to = accountHome.findById(toId);
Account from = accountHome.findById(fromId);
account1.debit(from);
account2.credit(to);
}

· Command 模式...

  • Command 处理器的参数为 command 对象
  • 很容易扩展
    • 非常容易添加新的"command"类
    • processCommand() 方法将 command 从客户端传递到服务器
      ...
      TransferCommand command = new TransferCommand();
      command.setFromAccount(1234);
      command.setToAccount(543);
      command.setAmount(1000.0);
      Teller teller = tellerHome.create();
      teller.processCommand(command);
      ...
    • 使用多态来执行 command
      • 形成一个非常容易扩展的框架
      • command 运行在服务器上
        TellerBean method
        CommandResult processCommand(Command command) {
        try {
        return command.process(getCommandContext());
        } catch (Exception e) {
        return new ResultFailure(...);
        }
        }
    • 一个 command 上下文提供了执行上下文
      例如,对系统资源的存取
      TransferCommand method
      CommandResult process(CommandContext context) throws Exception {
      Account toAccount = context.getAccountHome().findById(getToAccount());
      Account fromAccount = context.getAccountHome().findById(getFromAccount());
      toAccount.credit(amount);
      fromAccount.debit(amount);
      return new ResultSuccess();
      }

    本章讲述内容

    • 模式描述了通用的可重复的解决方案
    • 会话 Bean 可以(可能就应该)被用来在客户端和实体 Bean 间提供隔离
    (全文完)
全面研读EJB 2.0(1):【上一篇】
EJB(Enterprise JavaBeans)入门(9):【下一篇】
【相关文章】
  • 全面研读EJB 2.0(1)
  • 全面研读EJB 2.0(2)
  • 全面研读EJB 2.0(3)
  • javabean与ejb的区别
  • J2EE 组件开发:会话EJB
  • EJB 异常处理的最佳做法(1)
  • EJB 异常处理的最佳做法(2)
  • 用JavaBean实现文件上载(1)
  • Java嵌入式开发之j2me(6)
  • J2ME入门-(6)使用Kjava GUI组件的开发
  • 【随机文章】
  • c语言函数库
  • 我的第一篇文章!!
  • ORACLE Nologging相关知识
  • 11.1. Introduction to Lisp
  • Solaris 8 定制OpenWindows工作区
  • CVS新手介绍
  • 精确定位打印程序 (含源代码)
  • coolfly教程-破解入门第八集
  • BASH 中的字符串处理
  • ASP ODBC错误
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.