Jeenson Ephraim
Jeenson Ephraim

Reputation: 571

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

Upvotes: 1

Views: 2373

Answers (3)

Robby Cornelissen
Robby Cornelissen

Reputation: 97331

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.

Upvotes: 1

weaselflink
weaselflink

Reputation: 254

One possible fix is to add a dependency to VWeb.war into the jboss-deployment-structure.xmlfile 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 @Injectin the final destination.)

Upvotes: 0

LightGuard
LightGuard

Reputation: 5378

If I'm understanding correctly, you should keep using @EJB for a remote bean.

Upvotes: 0

Related Questions