Solved

Injecting an EJB in a simple java class

Posted on 2011-03-07
3
808 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb entity bean example 2 35
Need Help! Getting a syntax error and don't understand why 3 39
wild fly 8 startup error 2 40
Html split(text) 2 28
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

749 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