我使用wildfly 10.1在NetBeans 8.2中创建了EJB项目。
我为此创建了两个项目:EJBComponent(LibrarySessionBean.java,LibrarySessionBeanRemote.java)和EjbTester(EJBTester.java)
我试图运行EJBTester.java时出错:
run:
sty 18, 2017 3:13:55 PM org.xnio.Xnio <clinit>
INFO: XNIO version 3.4.0.Final
sty 18, 2017 3:13:55 PM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.4.0.Final
sty 18, 2017 3:13:55 PM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 4.0.21.Final
sty 18, 2017 3:13:56 PM org.jboss.ejb.client.remoting.VersionReceiver handleMessage
INFO: EJBCLIENT000017: Received server version 2 and marshalling strategies [river]
sty 18, 2017 3:13:56 PM org.jboss.ejb.client.remoting.RemotingConnectionEJBReceiver associate
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@527740a2, receiver=Remoting connection EJB receiver [connection=Remoting connection <5fc52344> on endpoint "config-based-naming-client-endpoint" <13a5fe33>,channel=jboss.ejb,nodename=lenovo-g580]} on channel Channel ID 8f1e528b (outbound) of Remoting connection 3701eaf6 to localhost/127.0.0.1:8080 of endpoint "config-based-naming-client-endpoint" <13a5fe33>
ejb:/EJBComponent//LibrarySessionBean!com.ejb.stateless.LibrarySessionBeanRemote -- service jboss.naming.context.java.jboss.exported.ejb:.EJBComponent."LibrarySessionBean!com.ejb.stateless.LibrarySessionBeanRemote"
javax.naming.NameNotFoundException: ejb:/EJBComponent//LibrarySessionBean!com.ejb.stateless.LibrarySessionBeanRemote -- service jboss.naming.context.java.jboss.exported.ejb:.EJBComponent."LibrarySessionBean!com.ejb.stateless.LibrarySessionBeanRemote"
at org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:106)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:207)
at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
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:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
BUILD SUCCESSFUL (total time: 1 second)
我的代码:
LibrarySessionBean.java
package com.ejb.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> bookShelf;
public LibrarySessionBean(){
bookShelf = new ArrayList<>();
}
@Override
public void addBook(String bookName) {
bookShelf.add(bookName);
}
@Override
public List<String> getBooks() {
return bookShelf;
}
}
LibrarySessionBeanRemote.java
package com.ejb.stateless;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibrarySessionBeanRemote {
void addBook(String bookName);
List getBooks();
}
客户端应用程序在不同的项目中进行。
EJBTester.java
package com.ejb.test;
import com.ejb.stateless.LibrarySessionBean;
import com.ejb.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;
final String appName = "";
final String moduleName = "EJBComponent";
final String distinctName = "";
final String beanName = LibrarySessionBean.class.getSimpleName();
final String viewClassName = LibrarySessionBeanRemote.class.getName();
{
props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, org.jboss.naming.remote.client.InitialContextFactory.class.getName());
props.put(Context.PROVIDER_URL, "http-remoting://localhost:8080"); //http-remoting://127.0.0.1:8080
// props.put(Context.SECURITY_PRINCIPAL, "testuser");
// props.put(Context.SECURITY_CREDENTIALS, "test");
props.put("jboss.naming.client.ejb.context", true);
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("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
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("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
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());
}
}
}
}
答案 0 :(得分:1)
更改代码以符合此处说明的内容:https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
检查&#34; jboss-ejb-client.jar&#34;在您的客户端类路径中可用吗?