精华区 [关闭][返回]

当前位置:网易精华区>>讨论区精华>>编程开发>>● Java>>J2EE>>给入门者的资料(jb6+weblogic6.1)

主题:给入门者的资料(jb6+weblogic6.1)
发信人: vikern()
整理人: zjxyz(2002-09-08 22:55:14), 站内信件
摸索了好久才稍微有点明白了,想到自己摸索时到处找例子的痛苦,于是自己做了一个例子,并把过程写了一下希望对大家有所把帮助. 


一、Weblogic6.1和Jb6的集成实践见如下URL
http://www.linuxaid.com.cn/developer/showdev.jsp?&i=508

二、在发布EJB前,先Verify一次,没有Error和Warning才发布,因为如果eb-jar.xml有报错也一样可以发布到Weblogic6.1中,同时自动产生的*.xml有时会有错误的,别太过迷信它,本人就在这里受了不少苦

三、例程
------------------Hello.java------------------------------
package myhello;

import java.rmi.*;
import javax.ejb.*;

public interface Hello extends EJBObject {
public String hello() throws java.rmi.RemoteException;
}

-------------------HelloBean.java ----------------------
package myhello;

import java.rmi.*;
import javax.ejb.*;

public class HelloBean implements SessionBean {
  private SessionContext sessionContext;
  public void ejbCreate() {
    }
  public void ejbRemove() {
  }
  public void ejbActivate() {
  }
  public void ejbPassivate() {
  }
  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }
  public String hello(){
    System.out.print("Hello()");
    return "Hello,World!";
  }
}

-----------------HelloHome.java--------------------------
package myhello;

import java.rmi.*;
import javax.ejb.*;

public interface HelloHome extends EJBHome {
  public Hello create() throws RemoteException, CreateException;
}



-----------------------HelloTestClient1 ---------------
package myhello;

import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;

public class HelloTestClient1 {
  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 HelloHome helloHome = null;
  private Hello hello = null;

  //Construct the EJB test client
  public HelloTestClient1() {
    long startTime = 0;
    if (logging) {
      log("Initializing bean access.");
      startTime = System.currentTimeMillis();
    }

    try {
      //get naming context
      Context ctx = getInitialContext();

      //look up jndi name
      Object ref = ctx.lookup("Hello");

      //cast to Home interface
      helloHome = (HelloHome) PortableRemoteObject.narrow(ref, HelloHome.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://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 Hello create() {
    long startTime = 0;
    if (logging) {
      log("Calling create()");
      startTime = System.currentTimeMillis();
    }
    try {
      hello = helloHome.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(): " + hello + ".");
    }
    return hello;
  }

  //----------------------------------------------------------------------------
  // Methods that use Remote interface methods to access data through the bean
  //----------------------------------------------------------------------------

  public String hello() {
    String returnValue = "";
    if (hello == 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 = hello.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;
  }

  public void testRemoteCallsWithDefaultArguments() {
    if (hello == null) {
      System.out.println("Error in testRemoteCallsWithDefaultArguments(): " + ERROR_NULL_REMOTE);
      return ;
    }
    hello();
  }

  //----------------------------------------------------------------------------
  // 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) {
    HelloTestClient1 client = new HelloTestClient1();

    Hello ff=client.create();
    ff.hello();
    try {
    System.out.println("===============");
    System.out.println(ff.hello());
    System.out.println("===============");
    }
  catch (java.rmi.RemoteException ex) {
    ex.printStackTrace();
  }



    // 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.
  }
}



[关闭][返回]