如何在此代码处解决 javax.naming.NameNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21052027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve javax.naming.NameNotFoundException at this code
提问by Vahid
I make a Ejb project in netbean 7.3 with jboss-7.1.1 Final
我使用 jboss-7.1.1 Final 在 netbean 7.3 中创建了一个 Ejb 项目
In Ejb module i have these:
在 Ejb 模块中,我有这些:
LibrarySessionBeanRemote.java
LibrarySessionBeanRemote.java
package com.tutorialspoint.stateless;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibrarySessionBeanRemote {
void addBook(String bookName);
List getBooks();
}
LibrarySessionBean.java
库会话Bean.java
package com.tutorialspoint.stateless;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote(LibrarySessionBeanRemote.class)
public class LibrarySessionBean implements LibrarySessionBeanRemote {
List<String> bookSelf;
public LibrarySessionBean() {
this.bookSelf = new ArrayList<String>();
}
@Override
public void addBook(String bookName) {
bookSelf.add(bookName);
}
@Override
public List getBooks() {
return bookSelf;
}
}
and I make a client with java application project type
我用java应用程序项目类型制作了一个客户端
package client;
import com.tutorialspoint.stateless.LibrarySessionBeanRemote;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class EJBTester {
BufferedReader brConsoleReader = null;
Properties props;
InitialContext ctx;
{
props = new Properties();
props.put(Context.SECURITY_PRINCIPAL, "testuser");
props.put(Context.SECURITY_CREDENTIALS, "test");
props.put(Context.PROVIDER_URL, "remote://localhost:4447");
props.put("jboss.naming.client.ejb.context", true);
props.put(Context.INITIAL_CONTEXT_FACTORY, org.jboss.naming.remote.client.InitialContextFactory.class.getName());
try {
ctx = new InitialContext(props);
} catch (NamingException ex) {
ex.printStackTrace();
}
brConsoleReader =
new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
EJBTester ejbTester = new EJBTester();
ejbTester.testStatelessEjb();
}
private void showGUI() {
System.out.println("**********************");
System.out.println("Welcome to Book Store");
System.out.println("**********************");
System.out.print("Options \n1. Add Book\n2. Exit \nEnter Choice: ");
}
private void testStatelessEjb() {
try {
int choice = 1;
LibrarySessionBeanRemote libraryBean =
(LibrarySessionBeanRemote) ctx.lookup("LibrarySessionBean/remote");
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
libraryBean.addBook(bookName);
} else if (choice == 2) {
break;
}
}
List<String> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
for (int i = 0; i < booksList.size(); ++i) {
System.out.println((i + 1) + ". " + booksList.get(i));
}
LibrarySessionBeanRemote libraryBean1 =
(LibrarySessionBeanRemote) ctx.lookup("LibrarySessionBean/remote");
List<String> booksList1 = libraryBean1.getBooks();
System.out.println(
"***Using second lookup to get library stateless object***");
System.out.println(
"Book(s) entered so far: " + booksList1.size());
for (int i = 0; i < booksList1.size(); ++i) {
System.out.println((i + 1) + ". " + booksList1.get(i));
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
try {
if (brConsoleReader != null) {
brConsoleReader.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
but i have this exception
但我有这个例外
javax.naming.NameNotFoundException: LibrarySessionBean/remote -- service jboss.naming.context.java.jboss.exported.LibrarySessionBean.remote
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:97)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
at org.jboss.naming.remote.protocol.v1.Protocol.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever.run(RemoteNamingServerV1.java:73)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
采纳答案by Erhard Siegl
When you call an EJB you should not use the remote-naming project, but the remote EJB-invocation as described in https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
当您调用 EJB 时,您不应使用远程命名项目,而应使用远程 EJB 调用,如https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+ 中所述客户端+使用+JNDI
Your JNDI name will look like:
您的 JNDI 名称将如下所示:
context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName)
appName and distinctName are empty in your case (no EAR). See the example in the provided link.
appName 和 distinctName 在您的情况下为空(没有 EAR)。请参阅提供的链接中的示例。
回答by Szymon Jednac
JNDI address format has changed significantly on JBoss 7. Related documentation can be found here.
JBoss 7 上的 JNDI 地址格式发生了显着变化。相关文档可以在这里找到。
Try replacing LibrarySessionBean/remote
with:
尝试替换LibrarySessionBean/remote
为:
app-name/module-name/LibrarySessionBean!com.tutorialspoint.stateless.LibrarySessionBeanRemote`
where:
在哪里:
app-name= the name of the .ear (without the .ear suffix) or the application name configured via application.xml deployment descriptor. If the application isn't packaged in a .ear then there will be no app-name part to the JNDI string.
module-name= the name of the .jar or .war (without the .jar/.war suffix) in which the bean is deployed or the module-name configured in web.xml/ejb-jar.xml of the deployment. The module name is mandatory part in the JNDI string.
app-name= .ear 的名称(不带 .ear 后缀)或通过 application.xml 部署描述符配置的应用程序名称。如果应用程序未打包在 .ear 中,则 JNDI 字符串将没有 app-name 部分。
module-name= 部署 bean 的 .jar 或 .war(不带 .jar/.war 后缀)的名称或部署的 web.xml/ejb-jar.xml 中配置的模块名称。模块名称是 JNDI 字符串中的必填部分。
回答by StackJavo
The Following code is an Standard a bit adapted to your example. Always Worked. Just remember to change de values in "appName" and "moduleName"
以下代码是一个有点适合您的示例的标准。一直工作。请记住更改“ appName”和“ moduleName”中的de值
That's it
就是这样
props = new Properties();
final String appName = "PackageEARProjectName";
final String moduleName = "PackageProjectName";
final String sessionBeanName = "LibrarySessionBean";
final String viewClassName = LibrarySessionBeanRemote.class.getName();
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
// username
jndiProps.put(Context.SECURITY_PRINCIPAL, "testuser");
// password
jndiProps.put(Context.SECURITY_CREDENTIALS, "test");
// This is an important property to set if you want to do EJB invocations via the remote-naming project
jndiProps.put("jboss.naming.client.ejb.context", true);
// create a context passing these properties
Context context = new InitialContext(jndiProps);
// lookup the bean
LibrarySessionBeanRemote LibrarySessionBean =(LibrarySessionBeanRemote)context.lookup(appName+"/"+moduleName+"/"+sessionBeanName+"!"+viewClassName);