📄 helloejbtestclient1.java~1~
字号:
package hello;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;import javax.ejb.Handle;import javax.ejb.EJBMetaData;import javax.ejb.HomeHandle;public class HelloEJBTestClient1 extends Object { private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first."; private static final int MAX_OUTPUT_LINE_LENGTH = 100; private boolean logging = true; private HelloEJBHome helloEJBHome = null; private HelloEJB helloEJB = null; //Construct the EJB test client public HelloEJBTestClient1() { initialize(); } public void initialize() { long startTime = 0; if (logging) { log("Initializing bean access."); startTime = System.currentTimeMillis(); } try { //get naming context Context context = getInitialContext(); //look up jndi name Object ref = context.lookup("HelloEJB"); //look up jndi name and cast to Home interface helloEJBHome = (HelloEJBHome) PortableRemoteObject.narrow(ref, HelloEJBHome.class); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded initializing bean access through Home interface."); 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://localhost: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; } } //---------------------------------------------------------------------------- // Methods that use Home interface methods to generate a Remote interface reference //---------------------------------------------------------------------------- public HelloEJB create() { long startTime = 0; if (logging) { log("Calling create()"); startTime = System.currentTimeMillis(); } try { helloEJB = helloEJBHome.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(): " + helloEJB + "."); } return helloEJB; } public void remove(Handle p0) { long startTime = 0; if (logging) { log("Calling remove(" + p0 + ")"); startTime = System.currentTimeMillis(); } try { helloEJBHome.remove(p0); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: remove(" + p0 + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: remove(" + p0 + ")"); } e.printStackTrace(); } } public void remove(Object p0) { long startTime = 0; if (logging) { log("Calling remove(" + p0 + ")"); startTime = System.currentTimeMillis(); } try { helloEJBHome.remove(p0); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: remove(" + p0 + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: remove(" + p0 + ")"); } e.printStackTrace(); } } public EJBMetaData getEJBMetaData() { EJBMetaData returnValue = null; long startTime = 0; if (logging) { log("Calling getEJBMetaData()"); startTime = System.currentTimeMillis(); } try { returnValue = helloEJBHome.getEJBMetaData(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getEJBMetaData()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getEJBMetaData()"); } e.printStackTrace(); } if (logging) { log("Return value from getEJBMetaData(): " + returnValue + "."); } return returnValue; } public HomeHandle getHomeHandle() { HomeHandle returnValue = null; long startTime = 0; if (logging) { log("Calling getHomeHandle()"); startTime = System.currentTimeMillis(); } try { returnValue = helloEJBHome.getHomeHandle(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getHomeHandle()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getHomeHandle()"); } e.printStackTrace(); } if (logging) { log("Return value from getHomeHandle(): " + returnValue + "."); } return returnValue; } //---------------------------------------------------------------------------- // Methods that use Remote interface methods to access data through the bean //---------------------------------------------------------------------------- public String hello() { String returnValue = ""; if (helloEJB == null) { System.out.println("Error in hello(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling hello()"); startTime = System.currentTimeMillis(); } try { returnValue = helloEJB.hello(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: hello()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: hello()"); } e.printStackTrace(); } if (logging) { log("Return value from hello(): " + returnValue + "."); } return returnValue; } //---------------------------------------------------------------------------- // Utility Methods //---------------------------------------------------------------------------- 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 method public static void main(String[] args) { HelloBeanTestClient1 client = new HelloBeanTestClient1(); Hello hello = client.create(); try { System.out.println(hello.hello()); } catch (Exception ex) { // } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -