简体   繁体   中英

Access EJB from different WAR in JBoss AS via injection

My Project structure is as follows

JBoss AS 7.1.3.Final-redhat-4

A war VWeb.war which contains an EJB , when VWeb is deployed I get the following

 java:global/VWeb/QServiceImpl!com.vi.qciapi.QService
java:app/VWeb/QServiceImpl!com.vi.qciapi.QService
java:module/QServiceImpl!com.vi.qciapi.QService
java:global/VWeb/QServiceImpl
java:app/VWeb/QServiceImpl
java:module/QServiceImpl

I have got another war (ControlWeb-portlet.war) which is a liferay project and I am trying to access the above EJB within a bean from ControlWeb-portlet . The bean is @ViewScoped This has been done based on a link found here

I have tried all these combinations but nothing seems to work

@Inject
QService qService;

@EJB
QService qService;

@EJB(mappedName="java:global/VWeb/QServiceImpl!com.vi.qciapi.QService")
QService qService;

@EJB(beanInterface=QService.class)
QService qService;

@EJB(mappedName="java:global/VWeb/QServiceImpl!com.vi.qciapi.QService")
QService qService;

I kept an EJB in the same project ControlWeb-portlet which is deployed as follows

java:global/ControlWeb-portlet/CdiServiceImpl!com.clink.cdi.CdiServiceImpl
    java:app/ControlWeb-portlet/CdiServiceImpl!com.clink.cdi.CdiServiceImpl
    java:module/CdiServiceImpl!com.clink.cdi.CdiServiceImpl
    java:global/ControlWeb-portlet/CdiServiceImpl
    java:app/ControlWeb-portlet/CdiServiceImpl
    java:module/CdiServiceImpl

and I am able to inject the EJB with

@EJB
CdiServiceImpl cdiServiceImpl;

Is there anything specific that I have to while accessing an EJB from a different WAR

Thanks in advance

Charlie

Apparently I answered this question on the JBoss forums a while ago. Since it was marked as the correct answer there, I thought I'd reproduce it here as well.


I got it to work using the following approach:

First, define EJBs as resources using the @Produces annotation:

public class Resources {
          @Produces
          @EJB(lookup = "java:global/.../WhateverService")
          private WhateverService whateverService;
}

Then, inject the EJB resources in your bean using the @Inject annotation:

// ...
@Inject
private WhateverService whateverService;
// ...

Works like a charm. I'm also using a similar CDI view scope approach.

如果我理解正确,则应继续将@EJB用于远程bean。

One possible fix is to add a dependency to VWeb.war into the jboss-deployment-structure.xml file in the second war. Which would look like this:

<jboss-deployment-structure>
  <deployment>
    <dependencies>
      <module name="deployment.VWeb.war" />
    </dependencies>
  </deployment>
</jboss-deployment-structure>

If that doesn't work you could also try a workaround described here .
(Create a @Stateless EJB in ControlWeb-portlet.war which injects the required bean via @EJB and then exposes this via producer methods, which allow you to use @Inject in the final destination.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM