`
dingqijie
  • 浏览: 101414 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

was ejb学习

阅读更多
最近因为项目需要,对ilog进行了学习,包括ilog在was的安装以及客户端连接ilog等内容。
个人感觉很多是通过ejb来进行查找及调用的,所以,特意自己做了一个ejb在was上部署以及客户端连接的例子:
   1、ejb方面
   ejb采用ejb 2,分三个类:Hello、HelloHome、HelloWord;
   Hello:
   public class Hello implements SessionBean {
    /** The session context */
    private SessionContext context;
    public Hello() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * Set the associated session context. The container calls this method
     * after the instance creation.
     *
     * The enterprise bean instance should store the reference to the context
     * object in an instance variable.
     *
     * This method is called with no transaction context.
     *
     * @throws EJBException Thrown if method fails due to system-level error.
     */
    public void setSessionContext(SessionContext newContext)
        throws EJBException {
        context = newContext;
    }
    public void ejbCreate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
       System.out.println("ejb create");
    }
    public void ejbRemove() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    public void ejbActivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    public void ejbPassivate() throws EJBException, RemoteException {
        // TODO Auto-generated method stub
    }
    /**
     * An example business method
     *
     * @ejb.interface-method view-type = "remote"
     *
     * @throws EJBException Thrown if method fails due to system-level error.
     */
    public String sayIt() throws EJBException {
        System.out.println("hello didi");
        return "hello didi.";
    }
}
    HelloHome
    public interface HelloHome
    extends javax.ejb.EJBHome
    {
    public com.rox.HelloWorld create()
      throws javax.ejb.CreateException,java.rmi.RemoteException;
    }
    HelloWorld
    public interface HelloWorld extends EJBObject{
    public String sayIt() throws RemoteException;
    }
   2、发布ejb到was中
   注意:在ejb的jndi项,填写ejb/hello,以便以后在client端进行引用。
   3、将导入的ejb导出
   注意:was比较恶心的地方在这里,因为我们在创建ejb的时候,没有生成stub等类,而was在发布我们的ejb时,自动给我们补齐,所以,我们从发布的ejb导出客户端需要的jar包。
   4、客户端编写
   注意事项:
   a、jre问题
   必须采用ibm的jre,否则无法通过;
   b、依赖jar包问题
   依赖的jar包包括三部分:
   部分一:was中的lib里面的包;
   部分二:was中的runtime中的包;
   部分三:从was导出的ejb的jar包;
   以上包中,第一部分可能不全部需要,但本人没有研究确切的需要那几个包,不过,添加了全部包后,可以确保测试通过;
   c、客户端的代码实现
javax.naming.InitialContext initialContext = null;
   Properties environment = new Properties();
//environment.put(Context.PROVIDER_URL,   "iiop://10.66.49.249:2809"); 因为本人在本机测试,所以不用设置这条。
   environment.put(Context.INITIAL_CONTEXT_FACTORY,   "com.ibm.websphere.naming.WsnInitialContextFactory");
environment.put(Context.URL_PKG_PREFIXES,   "com.ibm.ws.naming");
initialContext = new javax.naming.InitialContext(environment);
HelloHome helloHome = null;
Object obj = initialContext.lookup("ejb/hello");
helloHome = (HelloHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class);
HelloWorld hello = helloHome.create();
System.out.println(hello.sayIt());


//thank you ,that is all!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics