?? firstejbbean.java
字號:
package interejbs;import javax.ejb.*;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class firstEJBBean implements SessionBean { SessionContext sessionContext; private secondEJBHome secondEJBHomeObject = null; public void ejbCreate() throws CreateException { try { //得到名字上下文 Context ctx = getInitialContext(); //查詢jndi名 Object ref = ctx.lookup("secondEJB"); //通過強(qiáng)制轉(zhuǎn)型得到Home接口 secondEJBHomeObject = (secondEJBHome) PortableRemoteObject.narrow(ref, secondEJBHome.class); } catch(Exception e) { e.printStackTrace(); } } public void ejbRemove() { } public void ejbActivate() { } public void ejbPassivate() { } public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; } //這個(gè)函數(shù)返回一個(gè)經(jīng)過初始化的上下文 private Context getInitialContext() throws Exception { String url = "t3://cgb-4wn01xj69v6:7001"; String user = null; String password = null; Properties properties = null; try { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url); if (user != null) { properties.put(Context.SECURITY_PRINCIPAL, user); properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); } return new InitialContext(properties); } catch(Exception e) { throw e; } } //這個(gè)函數(shù)將調(diào)用secondEJB public String MyPrint() { String ret="This is a example for interEJBS:\n"; ret=ret+"This is firstEJB,now calling secondEJB\n"; try { secondEJB secondEJB=secondEJBHomeObject.create(); ret=secondEJB.forFirst(ret)+"example is over!\n"; } catch(Exception e) { e.printStackTrace(); } return ret; } //這個(gè)函數(shù)將被secondEJB調(diào)用 public String forSecond(String fromSecond) { fromSecond=fromSecond+"This is firstEJB called by secondEJB\n"; return fromSecond; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -