我创建了一个Web项目并创建了一个EJB(接口:LibrarySessionBeanRemote.java和class:LibrarySessionBean)。我已经在JBoss 7.1服务器上部署了war文件。以下是两个文件的代码:
LibrarySessionBeanRemote.java
package com.practice.stateless;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibrarySessionBeanRemote {
void addBook(String bookName);
List<String> getBooks();
}
LibrarySessionBean.java
package com.practice.stateless;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {
List<String> bookShelf;
public LibrarySessionBean() {
bookShelf = new ArrayList<String>();
}
@Override
public void addBook(final String bookName) {
bookShelf.add(bookName);
}
@Override
public List<String> getBooks() {
return bookShelf;
}
}
我试图在main方法中本地调用EJB,下面是代码:
LibrarySessionBeanTest2
package com.practice.stateless.test;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.practice.stateless.LibrarySessionBean;
import com.practice.stateless.LibrarySessionBeanRemote;
public class LibrarySessionBeanTest2 {
public static LibrarySessionBeanRemote doLookup() throws NamingException {
final Properties jndiProp = new Properties();
jndiProp.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.naming.remote.client.InitialContextFactory");
jndiProp.put(Context.PROVIDER_URL, "remote://localhost:4447");
jndiProp.put(Context.SECURITY_PRINCIPAL, "username");
jndiProp.put(Context.SECURITY_CREDENTIALS, "password");
final String appName = "";
final String moduleName = "StatelessExample";
final String distinctName = "";
final String beanName = LibrarySessionBean.class.getSimpleName();
final String viewClassName = LibrarySessionBeanRemote.class.getName();
final Context ctx = new InitialContext(jndiProp);
final String jndiName = "ejb:" + appName + "/" + moduleName + "/"
+ distinctName + "/" + beanName + "!" + viewClassName;
// jndiName = "java:global/StatelessExample/LibrarySessionBean";
return (LibrarySessionBeanRemote) ctx.lookup(jndiName);
}
public static void invokeStatelessBean() throws NamingException {
final LibrarySessionBeanRemote librarySessionBeanRemote = doLookup();
librarySessionBeanRemote.addBook("book 1");
for (final String book : librarySessionBeanRemote.getBooks()) {
System.out.println(book);
}
}
public static void main(final String[] args) {
try {
invokeStatelessBean();
} catch (final NamingException e) {
e.printStackTrace();
}
}
}
运行LibrarySessionBeanTest2类后,我收到以下错误:
javax.naming.NameNotFoundException: ejb:/StatelessExample//LibrarySessionBean!com.practice.stateless.LibrarySessionBeanRemote -- service jboss.naming.context.java.jboss.exported.ejb:.StatelessExample."LibrarySessionBean!com.practice.stateless.LibrarySessionBeanRemote"
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:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
我是EJB 3的新手,所以我不知道我是否正确使用JNDI名称。如果有人可以帮我解决这个问题,那将会很棒。
答案 0 :(得分:0)
现在通过添加另外两个属性修复了问题,请在下面找到它们:
jndiProp.put("jboss.naming.client.ejb.context", true);
jndiProp.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");