EJB客户端错误
我创建了一个 EJB 2。我将其部署在 JBoss-IDE 1.6 上,然后创建了一个 JUnit 测试来访问它。这是我用于客户端的代码:
public class DossierBeanTest extends TestCase {
protected DossierHome dossierHome;
protected Dossier dossier;
public DossierBeanTest(String argo){
super(arg0);
}
protected void setUp() throws Exception {
super.setUp();
Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL,"jnp://localhost:1199");
Context context=new InitialContext(env);
Object finderRef=context.lookup(dossierHome.JNDI_NAME);
dossierHome=(DossierHome)PortableRemoteObject.narrow(finderRef,DossierHome.class);
dossier=dossierHome.create();
}
public void testGetString() throws RemoteException {
assertEquals("Test",dossier.getTest());
}
但问题是该客户端失败并出现错误:
javax.naming.CommunicationException: Could not obtain connection to any of these
urls: localhost:1199 and discovery failed with error :
javax.naming.CommunicationException: Receive timed out [Root Exception is
java.net.SocketTimeoutException: Receive timed out][Root exception is
javax.naming.CommunicationException: Failed to connect to server localhost:1199
....
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1416)
...
I created an EJB 2. I deployed it on JBoss-IDE 1.6 and then, I created a JUnit test to access it. here's the code I used for the client :
public class DossierBeanTest extends TestCase {
protected DossierHome dossierHome;
protected Dossier dossier;
public DossierBeanTest(String argo){
super(arg0);
}
protected void setUp() throws Exception {
super.setUp();
Hashtable env=new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL,"jnp://localhost:1199");
Context context=new InitialContext(env);
Object finderRef=context.lookup(dossierHome.JNDI_NAME);
dossierHome=(DossierHome)PortableRemoteObject.narrow(finderRef,DossierHome.class);
dossier=dossierHome.create();
}
public void testGetString() throws RemoteException {
assertEquals("Test",dossier.getTest());
}
but the problem is that this client fails with the error :
javax.naming.CommunicationException: Could not obtain connection to any of these
urls: localhost:1199 and discovery failed with error :
javax.naming.CommunicationException: Receive timed out [Root Exception is
java.net.SocketTimeoutException: Receive timed out][Root exception is
javax.naming.CommunicationException: Failed to connect to server localhost:1199
....
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1416)
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人已经指出的那样,JBoss 命名服务器 默认侦听端口 1099。所以,除非您在
conf/jboss-service.xml
中更改此值(服务名称jboss:service=Naming
),您应该这样写:话虽如此,我通常更喜欢提供 JNDI
jndi.properties
文件(放置在类路径中)中的环境设置。在您的情况下,具有以下内容:并使用空参数构造函数
InitiatContext()
:这使得代码可移植(在测试环境中可能不是问题,好吧,我更喜欢不硬编码这样的东西)。
As other already pointed out, JBoss naming server listens by default on port 1099. So, unless you changed this value in
conf/jboss-service.xml
(service namejboss:service=Naming
), you should write:That being said, I usually prefer to provide the JNDI environment settings in a
jndi.properties
file (that you put on the classpath). In your case, with the following content:And use the empty parameter constructor
InitiatContext()
:This makes the code portable (might not be an issue in a testing context, well, I prefer to not hard code such things).
对于玻璃鱼,请参阅以下链接。
http://allinoneissues.blogspot.jp/2012/05/orgjnpinterfacesnamingcontext-failed -to.html
For Glass Fish see blow link.
http://allinoneissues.blogspot.jp/2012/05/orgjnpinterfacesnamingcontext-failed-to.html
我猜您使用了错误的端口号。您可以首先通过查看命令
netstat -a
的输出来检查这一点。如果没有进程正在侦听端口 1199,您必须检查 JBoss 配置。I'd guess you are using the wrong port number. You can check this first by looking at the output of the command
netstat -a
. If no process is listening on port 1199 you have to inspect your JBoss configuration.默认端口是1099,你改了吗?如果没有尝试从 1199 切换端口。
此外,如果您使用的是 Linux,您可能想尝试 127.0.0.1 而不是 localhost。
The default port is 1099, did you change it? if not try switching the port from 1199.
Also if you are on Linux you may want to try 127.0.0.1 instead of localhost.