• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 831
  • Last Modified:

Injecting an EJB in a simple java class

Hi,

I have a RESTful web service set to call a simple Java class XMLProcessor whose function is to process an xml string and call an EJB that will then insert the parsed xml values into a database.

My problem is that if I am unable to inject the EJB in XMLProcessor.  I have tried adding @Stateless at the beginning of the class :

import javax.ejb.Stateless
@Stateless
public class XMLProcessor { ...}

but once I do that, I get the following error when trying to deploy my web service:

Input stream has been finalized or forced closed without being explicitly closed; stream instantiation reported in following stack trace:
java.lang.Throwable
        at com.sun.enterprise.loader.ASURLClassLoader$SentinelInputStream.<init>(ASURLClassLoader.java:1153)
        at com.sun.enterprise.loader.ASURLClassLoader$InternalJarURLConnection.getInputStream(ASURLClassLoader.java:1246)
        at java.net.URL.openStream(URL.java:1010)
        at org.jboss.weld.util.serviceProvider.DefaultServiceLoader.reload(DefaultServiceLoader.java:183)
        at org.jboss.weld.util.serviceProvider.DefaultServiceLoader.iterator(DefaultServiceLoader.java:305)
        at org.jboss.weld.bootstrap.ExtensionBeanDeployer.addExtensions(ExtensionBeanDeployer.java:86)
        at org.jboss.weld.bootstrap.WeldBootstrap.startInitialization(WeldBootstrap.java:352)
        at org.glassfish.weld.WeldDeployer.event(WeldDeployer.java:163)
        at org.glassfish.kernel.event.EventsImpl.send(EventsImpl.java:125)
        at org.glassfish.internal.data.ApplicationInfo.load(ApplicationInfo.java:224)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:338)
        at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:183)
        at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:272)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$1.execute(CommandRunnerImpl.java:305)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:320)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1176)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$900(CommandRunnerImpl.java:83)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1235)
        at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1224)
        at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:365)
        at com.sun.enterprise.v3.admin.AdminAdapter.service(AdminAdapter.java:204)
        at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166)
        at com.sun.enterprise.v3.server.HK2Dispatcher.dispath(HK2Dispatcher.java:100)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:245)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:680)

If I don't put @Stateless, the ejb is never injected and my ejb variable remains null.

Basically, here is the workflow:
- an @POST method gets called in my web service.  
- the above method creates an XMLProcessor instance and calls the processXMLHeader
- the XMLProcessor processXMLHeader method parses the xml and calls an insert method defined in the EJB.

How do I accomplish that?





@Path("myPath")
public class WebService {
    @Context
    private UriInfo context;

    public ErmResource() {
    }

    @POST
    @Produces("text/plain")
    public String loadFile(@Context HttpServletRequest request) {
         [..]
         XMLProcessor xmlp = new XMLProcessor();
         xmlp.setHeader(header);
         xmlp.processXMLHeader();
     }
}

@Stateless
public class XMLProcessor {
[...]

    @EJB
    private RESTwsEJB wsEJB;


    public XMLProcessor() {
    }

    public void processXMLHeader() {
          [...]
          wsEJB.insert(values);
    }


[...]
}



@Stateless
@LocalBean
public class RESTwsEJB extends AbstractEJB {
    public RESTwsEJB() {
        super();
    }

    public void insert(String[] values) {
       [...]
    }
}

Open in new window

0
Emilie
Asked:
Emilie
  • 2
1 Solution
 
a_bCommented:
I would suggest that since you cannot do an injection for -

@EJB
private RESTwsEJB wsEJB;

Try to do a lookup to the local interface instead iinstead -

 service = (RESTwsEJB Local) new InitialContext().lookup("RESTwsEJB /local");


Also here is an example of calling a EJB3 web service method - http://www.java2s.com/Code/Java/EJB3/EJBWithWebMethod.htm
0
 
EmilieAuthor Commented:
I keep getting a "lookup failed in SerialContext" error when trying to do the lookup.

I have tried adding a mappedName to the stateless injection in my ejb:
@Stateless(mappedName="ejb-ws")
public class RESTwsEJB extends AbstractEJB implements RESTwsEJBLocal { ...}

and then doing the lookup this way:
service = (RESTwsEJBLocal) new InitialContext().lookup("ejb-ws/local");

But that still gives me the same error message.  

I am not very familiar with jndi name lookups.  How do I find out what my bean's jndi name is?

0
 
EmilieAuthor Commented:
I was finally able to get it working.  It seems that I needed a Remote Interface rather than a Local one.   Using the context lookup on that type of interface worked.

Thanks for putting me on the right track!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now