我使用jboss-7.1.1 Final
在netbean 7.3中创建了一个Ejb项目在Ejb模块中,我有以下内容:
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
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;
}
}
我用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());
}
}
}
}
但我有这个例外
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$1.handleServerMessage(Protocol.java:127)
at org.jboss.naming.remote.protocol.v1.RemoteNamingServerV1$MessageReciever$1.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)
答案 0 :(得分:4)
JNDI地址格式在JBoss 7上发生了重大变化。可以找到相关文档here。
尝试将LibrarySessionBean/remote
替换为:
app-name/module-name/LibrarySessionBean!com.tutorialspoint.stateless.LibrarySessionBeanRemote`
其中:
app-name = .ear的名称(不带.ear后缀)或 应用程序名称通过application.xml部署描述符配置。 如果应用程序没有打包在.ear中,那么就没有了 app-name是JNDI字符串的一部分。
module-name = .jar的名称 或.war(没有.jar / .war后缀),其中部署了bean 或者在web.xml / ejb-jar.xml中配置的模块名称 部署。模块名称是JNDI字符串中的必需部分。
答案 1 :(得分:4)
当您调用EJB时,不应使用远程命名项目,而应使用https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
中所述的远程EJB调用您的JNDI名称将如下所示:
context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName)
在您的情况下,appName和distinctName为空(无EAR)。 请参阅提供的链接中的示例。
答案 2 :(得分:0)
以下代码是一个适合您示例的标准。总是工作。只需记住在“ appName ”和“ moduleName ”中更改de值
就是这样
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);