📄 facadetestclient2.java
字号:
package bookstoreejb;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;import java.util.Collection;public class facadeTestClient2 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 facadeHome facadeHomeObject = null; private facade facadeObject = null; //Construct the EJB test client public facadeTestClient2() { 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("facade"); //look up jndi name and cast to Home interface facadeHomeObject = (facadeHome) PortableRemoteObject.narrow(ref, facadeHome.class); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded initializing local bean access through Local 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://zhaoqiang: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 facade create() { long startTime = 0; if (logging) { log("Calling create()"); startTime = System.currentTimeMillis(); } try { facadeObject = facadeHomeObject.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(): " + facadeObject + "."); } return facadeObject; } //---------------------------------------------------------------------------- // Methods that use Remote interface methods to access data through the bean //---------------------------------------------------------------------------- public Collection getAllBook() { Collection returnValue = null; if (facadeObject == null) { System.out.println("Error in getAllBook(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getAllBook()"); startTime = System.currentTimeMillis(); } try { returnValue = facadeObject.getAllBook(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getAllBook()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getAllBook()"); } e.printStackTrace(); } if (logging) { log("Return value from getAllBook(): " + returnValue + "."); } return returnValue; } public Object getBookByName(String bookName) { Object returnValue = null; if (facadeObject == null) { System.out.println("Error in getBookByName(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getBookByName(" + bookName + ")"); startTime = System.currentTimeMillis(); } try { returnValue = facadeObject.getBookByName(bookName); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getBookByName(" + bookName + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getBookByName(" + bookName + ")"); } e.printStackTrace(); } if (logging) { log("Return value from getBookByName(" + bookName + "): " + returnValue + "."); } return returnValue; } public void sendMessage(Order order) { if (facadeObject == null) { System.out.println("Error in sendMessage(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling sendMessage(" + order + ")"); startTime = System.currentTimeMillis(); } try { facadeObject.sendMessage(order); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: sendMessage(" + order + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: sendMessage(" + order + ")"); } e.printStackTrace(); } } public Collection getAllOrder() { Collection returnValue = null; if (facadeObject == null) { System.out.println("Error in getAllOrder(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getAllOrder()"); startTime = System.currentTimeMillis(); } try { returnValue = facadeObject.getAllOrder(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getAllOrder()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getAllOrder()"); } e.printStackTrace(); } if (logging) { log("Return value from getAllOrder(): " + returnValue + "."); } return returnValue; } public void deleteOrder(String orderid) { if (facadeObject == null) { System.out.println("Error in deleteOrder(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling deleteOrder(" + orderid + ")"); startTime = System.currentTimeMillis(); } try { facadeObject.deleteOrder(orderid); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: deleteOrder(" + orderid + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: deleteOrder(" + orderid + ")"); } e.printStackTrace(); } } public void executeRemoteCallsWithDefaultArguments() { if (facadeObject == null) { System.out.println("Error in executeRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE); return ; } getAllBook(); getBookByName(""); sendMessage(null); getAllOrder(); deleteOrder(""); } //---------------------------------------------------------------------------- // 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) { facadeTestClient2 client = new facadeTestClient2(); client.create(); client.deleteOrder("orderid1");// Use the client object to call one of the Home interface wrappers // above, to create a Remote interface reference to the bean. // If the return value is of the Remote interface type, you can use it // to access the remote interface methods. You can also just use the // client object to call the Remote interface wrappers. }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -