Reputation: 4853
I'm trying to inject a EJB singleton bean into a CDI bean using @Inject. But the injected instance is sometime local and sometime it's remote instance (changes over restart). It's not consistent.
In the following sample, the TestService ejb's remote instance is injected into the GetTestData cdi managed bean. But I'm expecting the local instance. And it's also not consistent, sometimes it injects the local instance. I've figured out that by printing the injected bean in GetTestData class.
As a note, my test classes are excluded in the deployment package.
Any idea how to make it consistently inject local instance?
EJB Singleton Bean:
@Singleton
@Local(TestService.class)
@Remote(TestServiceRemote.class)
public class TestServiceImpl implements TestService {
..
}
CDI Bean:
public class GetTestData {
@Inject
TestService testService;
....
}
Unit Test:
@RunWith(CdiRunner.class)
public class GetTestDataTest {
@Inject
private GetTestData getTestData;
@Produces
@ProducesAlternative
@Mock
private TestService testService;
.....
}
I'm using JBoss 7.1.1-Final version (Application Server) and the Weld core version is 1.1.23-Final.
I'm not able to use @EJB because I'm using cdi-unit for testing.
Upvotes: 3
Views: 5641
Reputation: 4980
First, keep in mind that CDI injection doesn't support remote EJB out of the box (thru @Inject
annotation). When using an EJB as a CDI bean, it's remote interface is excluded from the set of type for the bean as stated here.
To be able to use a remote EJB you'll have to declare it as a resource like this.
public class ResourceProducers {
@Produces @EJB(ejbLink="../their.jar#TestServiceRemote")
TestServiceRemote remoteService;
}
So the behaviour you're experiencing probably comes from a CDI-unit since it is impossible that CDI use a remote EJB interface in an injection point.
My advice:
Upvotes: 4