📄 lockertestclient1.java
字号:
package cmpsample;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;public class LockerTestClient1 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 LockerRemoteHome lockerRemoteHome = null; private LockerRemote lockerRemote = null; //Construct the EJB test client public LockerTestClient1() { 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("LockerRemote"); //look up jndi name and cast to Home interface lockerRemoteHome = (LockerRemoteHome) PortableRemoteObject.narrow(ref, LockerRemoteHome.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 LockerRemote create(Integer id) { long startTime = 0; if (logging) { log("Calling create(" + id + ")"); startTime = System.currentTimeMillis(); } try { lockerRemote = lockerRemoteHome.create(id); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: create(" + id + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: create(" + id + ")"); } e.printStackTrace(); } if (logging) { log("Return value from create(" + id + "): " + lockerRemote + "."); } return lockerRemote; } public LockerRemote findByPrimaryKey(Integer id) { long startTime = 0; if (logging) { log("Calling findByPrimaryKey(" + id + ")"); startTime = System.currentTimeMillis(); } try { lockerRemote = lockerRemoteHome.findByPrimaryKey(id); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: findByPrimaryKey(" + id + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: findByPrimaryKey(" + id + ")"); } e.printStackTrace(); } if (logging) { log("Return value from findByPrimaryKey(" + id + "): " + lockerRemote + "."); } return lockerRemote; } //---------------------------------------------------------------------------- // Methods that use Remote interface methods to access data through the bean //---------------------------------------------------------------------------- public Integer getId() { Integer returnValue = null; if (lockerRemote == null) { System.out.println("Error in getId(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getId()"); startTime = System.currentTimeMillis(); } try { returnValue = lockerRemote.getId(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getId()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getId()"); } e.printStackTrace(); } if (logging) { log("Return value from getId(): " + returnValue + "."); } return returnValue; } public void setLockername(String lockername) { if (lockerRemote == null) { System.out.println("Error in setLockername(): " + ERROR_NULL_REMOTE); return ; } long startTime = 0; if (logging) { log("Calling setLockername(" + lockername + ")"); startTime = System.currentTimeMillis(); } try { lockerRemote.setLockername(lockername); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: setLockername(" + lockername + ")"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: setLockername(" + lockername + ")"); } e.printStackTrace(); } } public String getLockername() { String returnValue = ""; if (lockerRemote == null) { System.out.println("Error in getLockername(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getLockername()"); startTime = System.currentTimeMillis(); } try { returnValue = lockerRemote.getLockername(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getLockername()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getLockername()"); } e.printStackTrace(); } if (logging) { log("Return value from getLockername(): " + returnValue + "."); } return returnValue; } public String getStuInfo() { String returnValue = ""; if (lockerRemote == null) { System.out.println("Error in getStuInfo(): " + ERROR_NULL_REMOTE); return returnValue; } long startTime = 0; if (logging) { log("Calling getStuInfo()"); startTime = System.currentTimeMillis(); } try { returnValue = lockerRemote.getStuInfo(); if (logging) { long endTime = System.currentTimeMillis(); log("Succeeded: getStuInfo()"); log("Execution time: " + (endTime - startTime) + " ms."); } } catch(Exception e) { if (logging) { log("Failed: getStuInfo()"); } e.printStackTrace(); } if (logging) { log("Return value from getStuInfo(): " + returnValue + "."); } return returnValue; } public void executeRemoteCallsWithDefaultArguments() { if (lockerRemote == null) { System.out.println("Error in executeRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE); return ; } getId(); setLockername(""); getLockername(); getStuInfo(); } //---------------------------------------------------------------------------- // 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) { LockerTestClient1 client = new LockerTestClient1(); client.findByPrimaryKey(new Integer(2)); client.getStuInfo(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -