在一个简单的Java类中加入远程企业bean [英] Accesing an remote enterprise bean within a simple 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();
}
}
}
当我运行这个类我得到一个例外。
javax.naming.NameNotFoundException:endpoint.NewSessionRemote not found
at com.sun.enterprise.naming.TransientContext.doLookup(TransientContext.java:216)
在com.sun.enterprise.naming.TransientContext.lookup(TransientContext.java:188)
在com.sun。 enterprise.naming.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:74)
在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)
在java.lang。 reflect.Method.invoke(Method.java:597)
at com.sun.rar )
在com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:227)
在com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl .java:1846)
在com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1706)
在com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl .handleInput(CorbaMessageMediatorImpl.java:1088)
在com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:223)
在com.sun.corba.ee .impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:806)
在com.sun.corba.e e.impl.protocol.CorbaMessageMediatorImpl.dispatch(CorbaMessageMediatorImpl.java:563)
在com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.doWork(CorbaMessageMediatorImpl.java:2567)
在com。 sun.rar() >
所有其他企业bean类都是根据EJB 3.0标准编写的。
您有宝贵的贡献。
解决方案
是
javax.naming.NameNotFoundException:endpoint.NewSessionRemote not found
这是因为应用程序提供的JNDI名称与Servser(Glassfish)实际的JNDI名称不匹配,所以我确实检查了JNDI树Glassish通过其管理控制台(供应商特定),我注意到, NewSessionRemote 接口(这是会话bean NewSessionBean 的业务接口)的JNDI与我在申请方面提供的名称。那么这是怎么发生的,那么突然之间我发现有一些东西就是 ejb-jar.xml ,另一个名字叫JNDI名称分配给相同的 NewSessionRemote 使用标签。所以我只是删除它并重新部署EJB模块。就是这样。
解决方案使用JNDI时,您正在使用一个需要基于它的特定配置的API,以便连接到服务器(有关详细信息,请参阅 Javadoc )关于什么配置)。例如, java.naming.factory.initial
是指示要使用JNDI的哪个实现的属性。
现在,当在JavaEE服务器中运行代码时,此配置可以隐式提供,您需要做的就是在代码中完成的操作 - 实例化 InitialContext
,以及执行查找。但是,当在服务器外部运行时,此隐式配置不存在,因此您需要明确配置 InitialContext
。
您的示例代码使用 main()
方法,这表示您正在容器外运行。您需要的配置将取决于具体的应用服务器,您需要查找该文档以查看要提供的配置。
Here's my Java class
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.
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. 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. So I simply remove it and redeploy EJB module. That's it.
解决方案 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). For example, java.naming.factory.initial
is the property which indicates which implementation of JNDI you want to use.
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. However, when running outside the server, this implicit configuration is not present, and so you need to configure your InitialContext
explicitly.
Your sample code uses a main()
method, which suggests that you're running outside the container. 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.
这篇关于在一个简单的Java类中加入远程企业bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!