高级Meb技术 J阳a2 Enterprise Edtion
高级Web技术 Java 2 Enterprise Edtion
本次课程内容 资源管理与EJB环境
本次课程内容 ◼ 资源管理与EJB环境
例子背景 定义一个简单的 session bean,提供简单的币值 换算的功能。 币值换算要用到汇率,汇率是一个会经常变化的值,因 此将汇率声明为环境项目( Environment Entry),在 部署时可以修改汇率,bean提供服务时根据EJB环境中 的汇率值进行币值换算
例子背景 ◼ 定义一个简单的session bean,提供简单的币值 换算的功能。 ◼ 币值换算要用到汇率,汇率是一个会经常变化的值,因 此将汇率声明为环境项目(Environment Entry),在 部署时可以修改汇率,bean提供服务时根据EJB环境中 的汇率值进行币值换算
编写 Remote接口 package FlexConverter; import javax.ejb. i import java rmi k public interface Converter extends EJBObject { public double dollarToYen double dollars) throws RemoteException; public double yenToEuro(double yen throws Remote Exception; } ■定义了两个方法币值换算 dollarToYen:美元到日元换算 a yen ToEuro:日元到欧元换算
编写Remote接口 ◼ 定义了两个方法币值换算 ◼ dollarToYen:美元到日元换算 ◼ yenToEuro:日元到欧元换算 package FlexConverter; import javax.ejb.*; import java.rmi.*; public interface Converter extends EJBObject { public double dollarToYen(double dollars) throws RemoteException; public double yenToEuro(double yen) throws RemoteException; }
Home接口 package Flex Converter; import java. io Serializable import java rmi Remote Exception; import javax.ejb. import javax. naming. *i public interface ConverterHome extends EJBHome { public Converter created throws Remote Exception, CreateException; ■定义了一个 create方法 ■返回 Converter类型
Home接口 ◼ 定义了一个create方法 ◼ 返回Converter类型 package FlexConverter; import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.*; import javax.naming.*; public interface ConverterHome extends EJBHome { public Converter create() throws RemoteException, CreateException; }
编写 Session Bean类 public class Converter Bean implements Session Bean public double dollar ToYen(double dollars Context initCtx Double dollars ToYen null try initCtx new InitialContext(; myEnv=(Context)initCtx lookup(java: comp/env); dollarsToYen Double)my Env. lookup dollarsToYen ); catch(NamingException nek ne printstackTrace o; return dollarsToYen doubleValueo* dollars; public double yen ToEuro(double yen )类似 dollar ToYe的实现 } public Converter Beano合 商业方法实现 public void ejbcreate()仆 从EB环境中查找环境 public void ejbRemoveot 条目 dollarsToYen(美元对日元汇 public void ejbPassivateot 率),根据其值进行币值换算 public void ejbActivateot public void setSession Context(Session Context Cont }
编写Session Bean类 public class ConverterBean implements SessionBean{ public double dollarToYen(double dollars){ Context initCtx, myEnv; Double dollarsToYen = null; try{ initCtx = new InitialContext(); myEnv = (Context)initCtx.lookup("java:comp/env"); dollarsToYen = (Double)myEnv.lookup("dollarsToYen"); }catch(NamingException ne){ ne.printStackTrace(); } return dollarsToYen.doubleValue() * dollars; } public double yenToEuro(double yen){ //类似dollarToYen的实现 } public ConverterBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbPassivate() {} public void ejbActivate() {} public void setSessionContext(SessionContext Context) {} } 商业方法实现: 从EJB环境中查找环境 条目dollarsToYen(美元对日元汇 率),根据其值进行币值换算
组装部署EJB New Enterprise Bean Wizard-Environment Entries Please list any environment entries that are referenced in the code of the Enterprise Bean, as well as their Java type Optionally, you can provide descriptions and default values to be used for these entries Coded Entry T Value dollarsToYen Double Double 创建EB时增加环境项目: dollarsToYen en Toeuro Hel Cancel Back Next Finis
组装/部署EJB 创建EJB时增加环境项目: dollarsToYen yenToEuro
组装部署EJB Application Deployment Tool: ConverterApp le Edit Tools Help ♀口Fles iles. Applica tio ns. GonverterApp FlexGonvJAR GonverterBean ♀□ Applications Reso urce Env. Refs Resource Refs SecurityTransactions e◇ BankApp General Env Entries EJB Refs 9& FlexcowJAR Environment Entries Refe fenced in Code O Converter Coded Entry Typ value e◆ local objects do‖ arsToYen Double 1210 e◇ NewBankApp renToeuro Double 00077 e◆ TaxBeanApp ♀ Servers ♀ localhost 创建完EB也可以修改、添加或删 除环境项目
组装/部署EJB 创建完EJB也可以修改、添加或删 除环境项目
客户程序 public class ConverterClient public static void main( String[]args)t Xt Context initial new Initial ContextO; ConverterHome converterHome=(ConverterHome)PortableRemoteobject narrow( initial lookup( FlexibleConverterBean"), ConverterHome class; Converter converter converterHome create o; System. out println("create OK) double amount converter dollarToYen (100.00); System out printin ("$100 equals Yen"+ String valueof(amount)) amount converter yenToEuro(100.00); System. out printin ("Yen 100 equals Euro"+ String valueof(amount)); converter. remove O; catch(Exception et eprintstackTrace O;
客户程序 public class ConverterClient{ public static void main(String[] args){ try{ Context initial = new InitialContext(); ConverterHome converterHome = (ConverterHome)PortableRemoteObject.narrow( initial.lookup("FlexibleConverterBean"), ConverterHome.class); Converter converter = converterHome.create(); System.out.println("create OK"); double amount = converter.dollarToYen(100.00); System.out.println("$100 equals Yen " + String.valueOf(amount)); amount = converter.yenToEuro(100.00); System.out.println("Yen 100 equals Euro " + String.valueOf(amount)); converter.remove(); }catch(Exception e){ e.printStackTrace(); } } }
目标 当汇率发生变化时,仅需要修改环境项目的值,而 不需要修改Bean的源代码
目标 ◼ 当汇率发生变化时,仅需要修改环境项目的值,而 不需要修改Bean的源代码