开发者

How to use EJB 3.1 DI in Servlet ? (Could not inject session bean by @EJB from web application)

I am tying to merging web application(gwt, jpa) to an separate 2 application(business login in ejb/jpa and web client in gwt). Currently i can`t inject my beans from web application (simple servlet)

I am using glassfish v3. module limbo(ejb jar) is in dependency of module lust (war).

If I use lust with compiler output of limbo everything work perfect (if ejb in web application and the are deploying together as one application).

Have I messed some container configuration ?

Here is my steps:

I have some limbo.jar (ejb-jar) deployed to ejb container. I do not use any ejb-jar.xml, only annotations.


package ua.co.inferno.limbo.persistence.beans;

import javax.ejb.Remote;

@Remote
public interface IPersistentServiceRemote {
ArrayList<String> getTerminalACPList();

ArrayList<String> getBoxACPList();

ArrayList<String> getCNPList();

ArrayList<String> getCNSList();

String getProductNamebyID(int boxid);

ArrayList<String> getRegionsList(String lang);

long getSequence();    

void persistEntity (Object ent);
}

package ua.co.inferno.limbo.persistence.beans;

import ua.co.inferno.limbo.persistence.entitis.EsetChSchemaEntity;
import ua.co.inferno.limbo.persistence.entitis.EsetKeyActionsEntity;
@Local
public interface IPersistentService {
ArrayList<String> getTerminalACPList();

ArrayList<String> getBoxACPList();

ArrayList<String> getCNPList();

ArrayList<String> getCNSList();

String getProductNamebyID(int boxid);

ArrayList<String> getRegionsList(String lang);

long getSequence();

long persistPurchaseBox(EsetRegPurchaserEntity rp);

void removePurchaseTempBox(EsetRegPurchaserTempEntity rpt);

EsetRegionsEntity getRegionsById(long rid);

void persistEntity (Object ent);
}

package ua.co.inferno.limbo.persistence.beans;

import ua.co.inferno.limbo.persistence.entitis.EsetChSchemaEntity;
import ua.co.inferno.limbo.persistence.entitis.EsetKeyActionsEntity;
import ua.co.inferno.limbo.persistence.entitis.EsetRegBoxesEntity;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless(name = "PersistentService")
public class PersistentServiceEJB
    implements
    IPersistentService, IPersistentServiceRemote{
@PersistenceContext(unitName = "Limbo")
EntityManager em;

public PersistentServiceEJB() {
}
.........
}

Than i trying to use PersistentService session bean(included in limbo.jar) from web application in lust.war (the limbo.jar & lust.war is not in ear)


package ua.co.lust;

import ua.co.inferno.limbo.persistence.beans.IPersistentService;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletTest",
    urlPatterns = {"/"})
public class ServletTest extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws            ServletException, IOException {

    service(request, response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    service(request, response);
}



@EJB
private IPersistentService pService;


public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String hi = pService.getCNPList().toString();

    System.out.println("testBean.hello method returned: " + hi);
    System.out.println("In MyServlet::init()");
    System.out.println("all regions" + pService.getRegionsList("ua"));
    System.out.println("all regions" + pService.getBoxACPList());


    }
 }

web.xm


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">

    <servlet>
        <servlet-name>ServletTest</servlet-name>
        <servlet-class>ua.co.lust.ServletTest</servlet-class>
    </servlet>

</web-app>

When servelt is loading i ge 404 eror (The requested resource () is not available.)

And errors in logs :

global

Log Level
SEVERE
Logger
global
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=31}
Record Number
1421
Message ID
Complete Message
Class [ Lua/co/inferno/limbo/persistence/beans/IPersistentService; ] not found. Error while loading [ class ua.co.lust.ServletTest ]

javax.enterprise.system.tools.deployment.org.glassfish.deployment.common

Log Level
WARNING
Logger
javax.enterprise.system.tools.deployment.org.glassfish.deployment.common
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=31}
Record Number
1422
Message ID
Error in annotation processing
Complete Message
java.lang.NoClassDefFoundError: Lua/co/inferno/limbo/persistence/beans/IPersistentService;

ejb jar was deployed with this info log :


Log Level
INFO
Logger
javax.enterprise.system.container.ejb.com.sun.ejb.containers
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=26}
Record Number
1436
Message ID
Glassfish-specific (Non-portable) JNDI names for EJB PersistentService
Complete Message
[ua.co.inferno.limbo.persistence.beans.IPersistentServiceRemote#ua.co.inferno.limbo.persistence.beans.IPersistentServiceRemote, ua.co.inferno.limbo.persistence.beans.IPersistentServiceRemote]

Log Level
INFO
Logger
javax.enterprise.system.tools.admin.org.glassfish.deployment.admin
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=26}
Record Number
1445
Message ID
Complete Message
limbo was successfully deployed in 610 milliseconds.

Do i nee to add some additional configuration in a case of injections from others application?

Some ideas?


in a case of @Remote

package ua.co.lust;

import ua.co.inferno.limbo.persistence.beans.PersistentServiceRemote;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "ServletTest",
        urlPatterns = {"/"})
public class WebTestServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        service(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        service(request, response);
    }


//    @EJB
//    private PersistentServiceRemote pService;   <<-- DI not working :( - same errs as with Local interface (class not found)

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {

        Context ctx = null;
        try {
            ctx = new InitialContext();
            PersistentServiceRemote pService =
                    (PersistentServiceRemote) ctx.lookup("java:global/limbo/PersistentServiceBean!ua.co.inferno.limbo.persistence.beans.PersistentServiceRemote");

            String hi = pService.getCNPList().toString();


            System.out.println("testBean.hello method returned: " + hi);
            System.out.println("In MyServlet::init()");
            System.out.println("all regions" + pService.getRegionsList("ua"));
            System.out.println("all regions" + pService.getBoxACPList());

        } catch (NamingException e) {
            e.printStackTrace();
  开发者_StackOverflow社区      }


    }
}

Log :

        Log Level
        SEVERE
        Logger
        javax.enterprise.system.std.com.sun.enterprise.v3.services.impl
        Name-Value Pairs
        {_ThreadName=Thread-1, _ThreadID=32}
        Record Number
        1963
        Message ID
        javax.naming.NamingException
        Complete Message
        Lookup failed for 'java:global/limbo/PersistentServiceBean!ua.co.inferno.limbo.persistence.beans.PersistentServiceRemote' in SerialContext [Root exception is javax.naming.NamingException: ejb ref resolution error for remote business 
    interfaceua.co.inferno.limbo.persistence.beans.PersistentServiceRemote [Root exception is java.lang.ClassNotFoundException: ua.co.inferno.limbo.persistence.beans.PersistentServiceRemote]] at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442) at 
    javax.naming.InitialContext.lookup(InitialContext.java:392) at javax.naming.InitialContext.lookup(InitialContext.java:392) at 
ua.co.lust.WebTestServlet.service(WebTestServlet.java:45) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188) at 
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97) at 
    com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipel
ine.java:85) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185) at 
    org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165) 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:619) Caused by: javax.naming.NamingException: ejb ref resolution error for remote business interfaceua.co.inferno.limbo.persistence.beans.PersistentServiceRemote [Root exception is java.lang.ClassNotFoundException: ua.co.inferno.limbo.persistence.beans.PersistentServiceRemote] at com.sun.ejb.EJBUtils.lookupRemote30BusinessObject(EJBUtils.java:430) at 
com.sun.ejb.containers.RemoteBusinessObjectFactory.getObjectInstance(RemoteBusinessObjectFac
tory.java:70) at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304) at 
com.sun.enterprise.naming.impl.SerialContext.getObjectInstance(SerialContext.java:472) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:437) ... 28 more 
Caused by: java.lang.ClassNotFoundException: ua.co.inferno.limbo.persistence.beans.PersistentServiceRemote at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at 
java.net.URLClassLoader.findClass(URLClassLoader.java:188) at org.glassfish.web.loader.WebappClassLoader.findClass(WebappClassLoader.java:959) at 
org.glassfish.web.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1430) at com.sun.ejb.EJBUtils.getBusinessIntfClassLoader(EJBUtils.java:678) at 
com.sun.ejb.EJBUtils.loadGeneratedRemoteBusinessClasses(EJBUtils.java:459) at com.sun.ejb.EJBUtils.lookupRemote30BusinessObject(EJBUtils.java:410) ... 32 more

Info deploy log

Log Level
INFO
Logger
javax.enterprise.system.container.ejb.com.sun.ejb.containers
Name-Value Pairs
{_ThreadName=Thread-1, _ThreadID=11}
Record Number
1899
Message ID
Portable JNDI names for EJB PersistentServiceBean
Complete Message
[java:global/limbo/PersistentServiceBean!ua.co.inferno.limbo.persistence.beans.PersistentServiceRemote, java:global/limbo/PersistentServiceBean!ua.co.inferno.limbo.persistence.beans.PersistentServiceLocal]

Do I need include my ejb jar into WEB=INF/lib if I am using Remote business logic call? Why DI not working in a case of remote?

and it work prefect if I include compile output from limbo module and provide :

PersistentServiceRemote pService =
                    (PersistentServiceRemote) ctx.lookup("java:global/lust/PersistentServiceBean!ua.co.inferno.limbo.persistence.beans.PersistentServiceRemote");

Why I couldn`t look up for the PersistentServiceRemote (Remote interface of PersistentServiceBean ) for another web application ?

By sample`s code from glassfish It should work (.

Any ideas?


Ok. So as Pascal said. We need to package remote interface in webapp. We can use Local interface injection in different application. If you need use Local interface you need to include ejb jar in your application. Current design - package remote interface to webapp


If you don't deploy your ejb-jar as a lib of your war (i.e. under WEB-INF/lib), I don't think you'll be able to use the Local interface (and you'll have to package the remote interface in your webapp).

PS: you don't really need the web.xml here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜