没有可用的EJB接收器创建EJB Remote Client时

时间:2017-07-27 11:20:22

标签: java-ee ejb wildfly

我知道很多人已经问过这个错误,但我仍然找不到我的案例的解决方案,我按照this tutorial创建了一个远程客户端,访问驻留在Wildfly 10服务器上的Bean,在地址运行:localhost:8082。这是我的代码:

我的界面:

package ejb.remote.stateless;
import javax.ejb.Remote;

@Remote
public interface RemoteCalculator {
    int add(int a, int b);
    int subtract(int a, int b);
}

我的豆:

package ejb.remote.stateless;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
@Remote(RemoteCalculator.class)
public class CalculatorBean implements RemoteCalculator {

    @Override
    public int add(int a, int b) {
        return a+b;
    }

    @Override
    public int subtract(int a, int b) {
        return a-b;
    }

}

我的远程客户端:

package ejb.remote.client;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;


import ejb.remote.stateful.CounterBean;
import ejb.remote.stateful.RemoteCounter;
import ejb.remote.stateless.CalculatorBean;
import ejb.remote.stateless.RemoteCalculator;

public class RemoteEJBClient {

    public static void main(String[] args) throws NamingException {

         // Invoke a stateless bean
        invokeStatelessBean();
    }

    /**
     * Looks up a stateless bean and invokes on it
     *
     * @throws NamingException
     */
    private static void invokeStatelessBean() throws NamingException {

        final RemoteCalculator statelessRemoteCalculator = lookupRemoteStatelessCalculator();
        System.out.println("Obtained a remote stateless calculator for invocation");

        int a = 204;
        int b = 340;
        System.out.println("Adding " + a + " and " + b + " via the remote stateless calculator deployed on the server");
        int sum = statelessRemoteCalculator.add(a, b);
        System.out.println("Remote calculator returned sum = " + sum);
        if (sum != a + b) {
            throw new RuntimeException(
                    "Remote stateless calculator returned an incorrect sum " + sum + " ,expected sum was " + (a + b));
        }

        int num1 = 3434;
        int num2 = 2332;
        System.out.println("Subtracting " + num2 + " from " + num1
                + " via the remote stateless calculator deployed on the server");
        int difference = statelessRemoteCalculator.subtract(num1, num2);
        System.out.println("Remote calculator returned difference = " + difference);
        if (difference != num1 - num2) {
            throw new RuntimeException("Remote stateless calculator returned an incorrect difference " + difference
                    + " ,expected difference was " + (num1 - num2));
        }
    }

    private static RemoteCalculator lookupRemoteStatelessCalculator() throws NamingException {

        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        jndiProperties.put("jboss.naming.client.ejb.context", true);

        final InitialContext context = new InitialContext(jndiProperties);

        final String appName = "";

        final String moduleName = "jboss-as-ejb-remote-app";

        final String distinctName = "";

        final String beanName = CalculatorBean.class.getSimpleName();

        final String viewClassName = RemoteCalculator.class.getName();


        return (RemoteCalculator) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);

    }

}

然后我还将 jboss-ejb-client.properties 文件添加到项目的 src / 文件夹中,如下所示:

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false

remote.connections=default

remote.connection.default.host=localhost
remote.connection.default.port = 8082
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

我做的与教程完全一样,但我仍然得到错误:

Exception in thread "main" java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:, moduleName:jboss-as-ejb-remote-app, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@4d41cee
    at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:798)
    at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:128)
    at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)
    at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:255)
    at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:200)
    at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)
    at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)
    at com.sun.proxy.$Proxy0.add(Unknown Source)
    at ejb.remote.client.RemoteEJBClient.invokeStatelessBean(RemoteEJBClient.java:36)
    at ejb.remote.client.RemoteEJBClient.main(RemoteEJBClient.java:20)

任何人都可以帮我解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

代码没问题。如本教程中所述,您必须确保jboss-ejb-client.properties文件位于真正的类路径中。那它应该有效! (通过将jboss-ejb-client.properties移出类路径,我甚至可以使用相同的堆栈跟踪重现您的问题)

提示:将这些文件放在src/main/resources下是非常常见的(特别是对于maven项目)。

有关详细信息,请参阅github.com/wildfly/quickstart/ejb-remote,这与教程中的项目相同。