?? timefunctionstestclient1.java
字號:
//一個簡單例子的客戶端測試程序package simpleejb;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class TimeFunctionsTestClient1 { static final private String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first."; static final private int MAX_OUTPUT_LINE_LENGTH = 100; private boolean logging = true; private TimeFunctionsHome timeFunctionsHome = null; private TimeFunctions timeFunctions = null; //創(chuàng)建一個訪問EJB的客戶端應用程序 public TimeFunctionsTestClient1() { long startTime = 0; if (logging) { log("Initializing bean access."); startTime = System.currentTimeMillis(); } try { //得到名字上下文 Context ctx = getInitialContext(); //查詢jndi名 Object ref = ctx.lookup("TimeFunctions"); //通過強制轉(zhuǎn)型得到Home接口 timeFunctionsHome = (TimeFunctionsHome) PortableRemoteObject.narrow(ref, TimeFunctionsHome.class); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded initializing bean access."); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed initializing bean access."); } e.printStackTrace(); } } 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) { log("Unable to connect to WebLogic server at " + url); log("Please make sure that the server is running."); throw e; } } //---------------------------------------------------------------------------- // 這個函數(shù)用于使用本地接口產(chǎn)生一個遠程接口的應用 //---------------------------------------------------------------------------- public TimeFunctions create() { long startTime = 0; if (logging) { log("Calling create()"); startTime = System.currentTimeMillis(); } try { timeFunctions = timeFunctionsHome.create(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: create()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: create()"); } e.printStackTrace(); } if (logging) { log("Return value from create(): " + timeFunctions + "."); } return timeFunctions; } //通過Bean使用遠程接口方法訪問數(shù)據(jù) public long getTime() { long returnValue = 0; if (timeFunctions == null) { System.out.println("Error in getTime(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getTime()"); startTime = System.currentTimeMillis(); } try { returnValue = timeFunctions.getTime(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getTime()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getTime()"); } e.printStackTrace(); } if (logging) { log("Return value from getTime(): " + returnValue + "."); } return returnValue; } public void testRemoteCallsWithDefaultArguments() { if (timeFunctions == null) { System.out.println("Error in testRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE); return ; } getTime(); } // 工具函數(shù) private void log(String message) { if (message == null) { System.out.println("-- null"); return ; } if (message.length() > MAX_OUTPUT_LINE_LENGTH) { System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ..."); } else { System.out.println("-- " + message); } } //main()函數(shù) public static void main(String[] args) { TimeFunctionsTestClient1 client = new TimeFunctionsTestClient1(); client.create(); client.getTime(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -