Solved

Injecting an EJB in a simple java class

Posted on 2011-03-07
3
801 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
ID: 35066043
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
ID: 35068880
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
ID: 35069911
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to open debugger port in Intellij idea 6 140
groovy example issue 10 89
JDeveloper 12c for 32 bit 4 68
Java / Linux and Regular Expressions 11 69
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

919 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

21 Experts available now in Live!

Get 1:1 Help Now