[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Injecting an EJB in a simple java class

Posted on 2011-03-07
3
Medium Priority
?
828 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
a_b earned 2000 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

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!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

649 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