2017-03-17 119 views
0

我遇到了調用遠程EJB的麻煩:第一次必須這樣做,我可能會錯過一些東西。我在網上閱讀了很多教程,在SO這裏給我幾個答案,但我解決不了。這就是我迄今爲止所做的。EJB遠程調用

我的情況是:

我有Wildfly 10.0.0.Final下部署了兩個EAR S:server-earclient-ear

server-ear我有server-apiserver-ejb,第一個是包含我EJB s接口,第二個是包含實現的EJB模塊的簡單的Java模塊。

這些將是

@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),它定義一個單身EJB這需要到DummyApi接口的引用:

@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] 

兩個EAR s都部署在相同的本地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$000(ComponentInstallProcessor.java:80) 
    at org.jboss.as.ee.component.deployers.ComponentInstallProcessor$1.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 

我缺少什麼或者做錯了嗎?

回答

1

@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

+0

非常感謝您的答覆,我測試它現在。你能告訴我,我的問題中描述的依賴關係層次結構是否正確?第一次我需要使用遠程EJB,並且沒有任何通過文檔和教程,我只是想猜測很多東西 –

+0

是的,似乎你的耳朵結構是正確的。剛剛編輯我的答案與上下文創建。 –

+0

非常感謝您的幫助! –