JNDI没有EJB接收器可用于处理

时间:2016-03-29 19:21:15

标签: java error-handling ejb jndi wildfly-10

我的EJBTest存在问题。

我已经安装了WildFly并配置了用户管理和应用程序管理。

我编写了一个EJB 3.0并进行了部署:

@Stateless
@Remote(NewSessionBeanRemote.class)
public class NewSessionBean implements NewSessionBeanRemote {

    List<String> bookShielf;

    /**
     * Default constructor. 
     */
    public NewSessionBean() {
        bookShielf = new ArrayList<String>();
    }

    @Override
    public void addBook(String bookName) {
        bookShielf.add(bookName);
    }

    @Override
    public List getBook() {
        return bookShielf;
    }
}

之后,我写了一个简单的客户端来连接它:

private static void invokeStatelessBean() throws NamingException {
    // Let's lookup the remote stateless calculator
    NewSessionBeanRemote remoteEjb = lookupRemoteSessionBean();
    System.out.println("Obtained a remote stateless calculator for invocation");
    String bookName = "TEST book";
    remoteEjb.addBook(bookName);
}

private static NewSessionBeanRemote lookupRemoteSessionBean() throws NamingException {

    final Hashtable jndiProperties = new Hashtable();
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
    jndiProperties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:10090");
    jndiProperties.put(Context.SECURITY_PRINCIPAL, "ejb"); //this is the application user name, not management! it's correct?
    jndiProperties.put(Context.SECURITY_CREDENTIALS, "ejb");//this is the application password, not management! it's correct?
    jndiProperties.put("jboss.naming.client.ejb.context", true);
    final Context context = new InitialContext(jndiProperties);
    final String appName = "";
    final String moduleName = "EjbComponent";
    final String distinctName = "";
    final String beanName = NewSessionBean.class.getSimpleName();
    final String viewClassName = NewSessionBeanRemote.class.getName();
    System.out.println("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
    return (NewSessionBeanRemote) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
}

用户名和密码都是应用程序用户凭证,而不是管理!这是对的吗?

我收到此错误:

  

线程“main”中的异常java.lang.IllegalStateException:EJBCLIENT000025:没有EJB接收器可用于处理调用上下文的[appName:,moduleName:EjbComponent,distinctName:]组合org.jboss.ejb.client.EJBClientInvocationContext@5034c75a       在org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:798)       at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:128)       在org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)       at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:255)       在org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:200)       在org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)       在org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)       在com.sun.proxy。$ Proxy2.addBook(未知来源)       在com.studio.java.client.EjbTester.invokeStatelessBean(EjbTester.java:34)       在com.studio.java.client.EjbTester.main(EjbTester.java:21)

我不知道为什么!有人有想法吗?

1 个答案:

答案 0 :(得分:2)

您收到了上述错误,因为您尝试使用绝对JNDI名称访问远程EJB。

正如documentation所说:

  

http-remoting 客户端假定远程查找中的JNDI名称与java:jboss / exported命名空间相关,绝对JNDI名称的查找将失败。

因此,在WildFly上部署应用程序后,您应该在服务器控制台上看到类似的内容:

JNDI bindings for session bean named NewSessionBean in deployment unit deployment <your_deployment_unit> are as follows:

    java:global[/<application_name>]/<module_name>/<ejb_name>[!<interface_name>]
    java:app[/<module_name>]/<ejb_name>[!<interface_name>]
    java:module/<ejb_name>[!<interface_name>]
    java:jboss/exported[/<application_name>]/<module_name>/<ejb_name>[!<interface_name>]
    java:global/[/<application_name>]/<module_name>/<ejb_name>
    java:app[/<module_name>]/<ejb_name>
    java:module/<ejb_name>

因此,考虑到 java:jboss / exported 上下文,获取EJB的正确方法应该是:

// Normally the appName is the EAR name
// Leave it empty if your application isn't packaged in a EAR
String appName = "your_application_name/";
// The EJB module name
String moduleName = "ejb_module_name/";
String beanName = NewSessionBean.class.getSimpleName();
String viewClassName = NewSessionBeanRemote.class.getName();

(NewSessionBeanRemote) context.lookup(appName + moduleName + beanName + "!" + viewClassName);

如需进一步阅读,建议您同时查看Java EE JNDI Syntax以及WildFly's JNDI Reference

关于您的凭据,它们不是必需的,因为您的EJB不需要任何类型的身份验证,以便您访问它。通常,只有当您拥有使用LDAP service provider的应用程序时,才需要这样做。