views:

41

answers:

1

I am unable to get dependency injection to work for my remote service and I cannot figure out why. I want an instance of RemoteService so I wrote.

@EJB(name="RemoteService")
private RemoteService service;

And the Bean itself is defined with mappedName="RemoteService:

@Stateless(mappedName = "RemoteService")
public class RemoteServiceBean implements RemoteService

When I try to run this code I get an InjectionException:

EJB5070: Exception creating stateless session bean : [{0}]
com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref RemoteService@jndi: service.remote.RemoteService@[email protected]@Session@null into class service.OrderServiceBean
        at com.sun.enterprise.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:387)
        at com.sun.enterprise.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:206)
        at com.sun.enterprise.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:127)
        at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:538)
        at com.sun.ejb.containers.StatelessSessionContainer.access$100(StatelessSessionContainer.java:111)
        at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:783)
        at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:199)
        at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:489)
        at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:1709)
        at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1238)
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:195)
        at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:83)
        at $Proxy766.size(Unknown Source)
        at 

I have no ejb config files but I shouldn't need any, right? The remote service is running on the same glassfish instance as the service trying to reference it. Checking the JNDI browser in glassfish admin verifies that the EJB has been deployed with the correct jndi name and if I remove the @EJB annotation and do the lookup manually in the constructor it also works:

public OrderServiceBean()
  {
    try
    {
      final Properties properties = new Properties();
      properties.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
      properties.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
      properties.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
      properties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
      properties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

      final InitialContext initialContext = new InitialContext(properties);
      this.service = (RemoteService) initialContext.lookup("RemoteService");
    }

Ideas?

A: 

I can't believe I hadn't tried it but it seems I hadn't, I obviously hadn't tried referencing the EJB through mappedName. So changing the reference to this makes it work:

@EJB(mappedName="RemoteService")
private RemoteService service;
Kristofer