简体   繁体   English

在简单的Java类中访问远程企业bean

[英]Accesing an remote enterprise bean within a simple Java class

Here's my Java class 这是我的Java课

import endpoint.NewSessionRemote;
import javax.naming.Context;
import javax.naming.InitialContext;

public class HelloClient {

    public static void main(String[] args) {
        try {
            Context ctx = new InitialContext();
            NewSessionRemote hello = (NewSessionRemote) ctx.lookup("endpoint.NewSessionRemote");
            System.out.println(hello.stringChange(4));
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

When I run this class I'm getting an exception. 当我运行此类时,我遇到了异常。

    javax.naming.NameNotFoundException: endpoint.NewSessionRemote not found
        at com.sun.enterprise.naming.TransientContext.doLookup(TransientContext.java:216)
        at com.sun.enterprise.naming.TransientContext.lookup(TransientContext.java:188)
        at com.sun.enterprise.naming.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:74)
        at com.sun.enterprise.naming.RemoteSerialContextProviderImpl.lookup(RemoteSerialContextProviderImpl.java:129)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:154)
        at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:687)
        at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:227)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1846)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1706)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:1088)
        at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:223)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:806)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:563)
        at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2567)
        at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:555)
java.lang.NullPointerException

All the other enterprise bean classes are written according to the EJB 3.0 standard. 所有其他企业bean类都是根据EJB 3.0标准编写的。 Your valuable contribution is expected. 期待您的宝贵贡献。

Solution

The exception was 例外是

javax.naming.NameNotFoundException: endpoint.NewSessionRemote not found

It occurs because the JNDI name that was given by the application side didn't match the servser's (Glassfish) actual JNDI name, so I did was check the JNDI tree in Glassish through its admin console (vendor specific) and I did notice that the JNDI for the NewSessionRemote interface (which is the business interface of the session bean NewSessionBean ) is different from the name which I have given in the application side. 发生这种情况是因为应用程序端提供的JNDI名称与服务器(Glassfish)的实际JNDI名称不匹配,所以我这样做是通过其管理控制台(特定于供应商)在Glassish中检查了JNDI树,我确实注意到用于NewSessionRemote接口(会话Bean NewSessionBean的业务接口)的JNDI与我在应用程序端提供的名称不同。 So how did this happen then suddenly something came in to my mind that's the ejb-jar.xml there is another name JNDI name assigned to the same NewSessionRemote using tag. 因此,这是怎么发生的,然后突然想到,这就是ejb-jar.xml ,另外一个名称JNDI名称是使用标签分配给同一NewSessionRemote的 So I simply remove it and redeploy EJB module. 因此,我只需删除它并重新部署EJB模块。 That's it. 而已。

Looks like you have no RMI registry (ie active server) you are lookingUp() against. 看起来您没有正在lookingUp() RMI注册表(即活动服务器)。

You supplied no Context.INITIAL_CONTEXT_FACTORY variable, so the lookup should be a valid URL, which it is not. 您没有提供Context.INITIAL_CONTEXT_FACTORY变量,因此查找应该是有效的URL,不是。

Hence, you should put something like this on your env (on the iCtx ): 因此,您应该在env (在iCtx )上放置以下iCtx

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");

I suggest you read the the simple examples over at http://java.sun.com/j2se/1.5.0/docs/guide/jndi/jndi-rmi.html 我建议您在http://java.sun.com/j2se/1.5.0/docs/guide/jndi/jndi-rmi.html上阅读简单的示例。

When using JNDI, you're using an API that requires a specific configuration underlying it in order to connect to the server (see the Javadoc for details on what that configuration is). 使用JNDI时,您使用的API需要在其基础上进行特定配置才能连接到服务器(有关该配置的详细信息,请参见Javadoc )。 For example, java.naming.factory.initial is the property which indicates which implementation of JNDI you want to use. 例如, java.naming.factory.initial是指示您要使用哪个JNDI实现的属性。

Now, when running code inside a JavaEE server, this configuration is available implicitly, and all you need to do is what you have done in your code - instantiate InitialContext , and perform a lookup. 现在,当在JavaEE服务器中运行代码时,此配置是隐式可用的,您所需要做的就是在代码中完成的工作-实例化InitialContext并执行查找。 However, when running outside the server, this implicit configuration is not present, and so you need to configure your InitialContext explicitly. 但是,当在服务器外部运行时,此隐式配置不存在,因此您需要显式配置InitialContext

Your sample code uses a main() method, which suggests that you're running outside the container. 您的示例代码使用main()方法,这表明您正在容器外部运行。 The config you need will depend on your specific application server, you'll need to look up that documentation to see what config to supply. 您所需的配置将取决于您的特定应用程序服务器,您需要查看该文档以查看要提供的配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM