⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enterprise1testclient1.java

📁 自己学习EJB时
💻 JAVA
字号:
package statelesssessionbean;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;/* *SessionBean的客户端测试程序 */public class Enterprise1TestClient1 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 Enterprise1Home enterprise1Home = null;  private Enterprise1 enterprise1 = null;  //Construct the EJB test client  public Enterprise1TestClient1() {    initialize();  }  public void initialize() {    long startTime = 0;    if (logging) {      log("Initializing bean access.");      startTime = System.currentTimeMillis();    }    try {      //得到运行环境的context,通过getInitialContext()方法实现,不同的J2EE服务器在这里的运行代码不一样      Context context = getInitialContext();      //通过context查找服务器上的JNDI名字      Object ref = context.lookup("Enterprise1");      //得到EJB的Home Interface      enterprise1Home = (Enterprise1Home) PortableRemoteObject.narrow(ref, Enterprise1Home.class);      //从Home Interface得到Remote Interface      //  enterprise1=create();  //this creat() is in the client        enterprise1=enterprise1Home.create();      //从Remote Interface中调用其中的方法       double fist=3;       double second=5;       double result=enterprise1.addtion(fist,second);       System.out.println("fist:"+fist +"second"+second+"result"+result);      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://zxp:7001";//        t3://zxp:7001是服务器的运行地址,其中zxp是计算机名称,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 Enterprise1 create() {    long startTime = 0;    if (logging) {      log("Calling create()");      startTime = System.currentTimeMillis();    }    try {      enterprise1 = enterprise1Home.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(): " + enterprise1 + ".");    }    return enterprise1;  }  //----------------------------------------------------------------------------  // Methods that use Remote interface methods to access data through the bean  //----------------------------------------------------------------------------  public double addtion(double fist, double second) {    double returnValue = 0f;    if (enterprise1 == null) {      System.out.println("Error in addtion(): " + ERROR_NULL_REMOTE);      return returnValue;    }    long startTime = 0;    if (logging) {      log("Calling addtion(" + fist + ", " + second + ")");      startTime = System.currentTimeMillis();    }    try {      returnValue = enterprise1.addtion(fist, second);      if (logging) {        long endTime = System.currentTimeMillis();        log("Succeeded: addtion(" + fist + ", " + second + ")");        log("Execution time: " + (endTime - startTime) + " ms.");      }    }    catch(Exception e) {      if (logging) {        log("Failed: addtion(" + fist + ", " + second + ")");      }      e.printStackTrace();    }    if (logging) {      log("Return value from addtion(" + fist + ", " + second + "): " + 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) {    Enterprise1TestClient1 client = new Enterprise1TestClient1();    // 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 + -