EJB 远程调用

EJB Remote Invokation

我在调用远程 EJB 时遇到问题:我第一次必须这样做,我可能会遗漏一些东西。我在网上阅读了很多教程,在 SO 上发布了几个答案,但我无法解决。这就是我到目前为止所做的。

我的场景是:

我在 Wildfly 10.0.0.Final 下部署了两个 EARserver-earclient-ear

server-ear 中,我有 server-apiserver-ejb,第一个是包含我的 EJBs 接口的简单 Java 模块,第二个作为包含实现的 EJB 模块。

那些会是

@Remote
public interface DummyApi {
    String getSomething();
}

及其实现

@Stateless
@Remote(DummyApi.class)
public class DummyApiImpl implements DummyApi {
    @Override
    public String getSomething() {
        return "SOMETHING";
    }
}

client-ear 中,我有一个简单的 EJB 模块 (client-ejb),它定义了一个引用 DummyApi 的单例 EJB接口:

@javax.ejb.Singleton
public class DummyClient {

    private static final Logger log = LoggerFactory.getLogger(DummyClient.class);

    private @EJB DummyApi dummyApi;

    @PostConstruct
    public void postConstruct() {
        log.debug("***** " + dummyApi.getSomething() + "******");
    }

}

client-ejb 中我也将 jboss-ejb-client.properties 文件放在了 src/main/resources

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

remote.connections=default

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

至于那些模块的相互依赖性(我使用的是 Maven):

server-ear
  |---- server-api [compile]
  |---- server-ejb [compile]
          |-- server-api [provided]

client-ear
  |---- server-api [compile]
  |---- client-ejb [compile]
          |-- server-api [provided]

两个 EARs 都部署在同一个本地 Wildfly 10.0.0.Final(服务器的全新安装,没有任何自定义)。当我启动服务器时,我在 server-ear.

client-ear 而不是失败并出现以下异常

12:05:22,592 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-5) MSC000001: Failed to start service jboss.deployment.subunit."client-ear-0.0.1-SNAPSHOT.ear"."client-ejb-0.0.1-SNAPSHOT.jar".INSTALL: org.jboss.msc.service.StartException in service jboss.deployment.subunit."client-ear-0.0.1-SNAPSHOT.ear"."client-ejb-0.0.1-SNAPSHOT.jar".INSTALL: WFLYSRV0153: Failed to process phase INSTALL of subdeployment "client-ejb-0.0.1-SNAPSHOT.jar" of deployment "client-ear-0.0.1-SNAPSHOT.ear"
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:154)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
    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)
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: WFLYEE0052: Failed to install component DummyClient
    at org.jboss.as.ee.component.deployers.ComponentInstallProcessor.deploy(ComponentInstallProcessor.java:109)
    at org.jboss.as.server.deployment.DeploymentUnitPhaseService.start(DeploymentUnitPhaseService.java:147)
    ... 5 more
Caused by: org.jboss.as.server.deployment.DeploymentUnitProcessingException: WFLYEJB0406: No EJB found with interface of type 'com.server.api.DummyApi' for binding com.client.DummyClient/dummyApi
    at org.jboss.as.ejb3.deployment.processors.EjbInjectionSource.getResourceValue(EjbInjectionSource.java:90)
    at org.jboss.as.ee.component.deployers.ComponentInstallProcessor.processBindings(ComponentInstallProcessor.java:263)
    at org.jboss.as.ee.component.deployers.ComponentInstallProcessor.access[=16=]0(ComponentInstallProcessor.java:80)
    at org.jboss.as.ee.component.deployers.ComponentInstallProcessor.handle(ComponentInstallProcessor.java:215)
    at org.jboss.as.ee.component.ClassDescriptionTraversal.run(ClassDescriptionTraversal.java:54)
    at org.jboss.as.ee.component.deployers.ComponentInstallProcessor.deployComponent(ComponentInstallProcessor.java:218)
    at org.jboss.as.ee.component.deployers.ComponentInstallProcessor.deploy(ComponentInstallProcessor.java:101)
    ... 6 more

我错过了什么或做错了什么?

@EJB 只有当 bean 在同一个包(在你的例子中是 .ear)时才会工作。

您将需要使用启动时服务器日志中显示的 java:app 名称进行查找。像这样:

DummyApi api = (DummyApi) context.lookup("/server-ear/server-ejb/DummyApiImpl!path.to.DummyApi")

创建上下文:

private static Context createContextWildfly(String provider) throws NamingException {
    final Hashtable<String, String> properties = new Hashtable<>();
    properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
    properties.put("jboss.naming.client.ejb.context", "true");  
    properties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
    return new InitialContext(properties);
}

使用这个你不需要jboss-ejb-client.properties