Solved

Injecting an EJB in a simple java class

Posted on 2011-03-07
3
798 Views
Last Modified: 2012-08-14
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
Comment
Question by:Emilie
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
a_b earned 500 total points
Comment Utility
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
 

Author Comment

by:Emilie
Comment Utility
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
 

Author Closing Comment

by:Emilie
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now