首页 > 解决方案 > Can I invoke a local bean into a ear file from a Javax-WS into a war file- apache-tomee-plus-1.7.4

问题描述

I am using Eclipse Mars 2, maven 3.3.9 and apache-tomee-plus-1.7.4.

I have 2 projects (A and B)

Project A is a Web Service compiled like a WAR using maven 3.3.9 and deployed into TOMEE_HOME/webapps

Project B is an EJB module compiled like a EAR using maven 3.3.9 and deployed into TOMEE_HOME/apps (this project include other project with ejb clasess and compiled like a jar file)

These projects do not depend on each other in the pom.xml but I need to lookup an EJB in project A from project B.

---------- Project B Implementation -----------

Local Bean Interface in project B:

package co.edu.uniquindio.model.ejb.interfaces;

import javax.ejb.Local;

import org.springframework.context.support.ClassPathXmlApplicationContext;

@Local
public interface IReporte {

    public Object generate1();

    public Object generate2();

    public void setContext(ClassPathXmlApplicationContext context);
}

Implement local bean interface in project B:

package co.edu.uniquindio.model.ejb;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import co.edu.uniquindio.model.ejb.interfaces.IReporte;

@Stateless
@EJB(beanInterface = IReporte.class, beanName="ReporteEJB", name="IReporte")
public class ReporteEJB implements IReporte{

    private ClassPathXmlApplicationContext context;

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public Object generate1(){
        // do somthing amazing  
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public Object generate2(){
        // do somthing amazing  
    }

    @Override
    public void setContext(ClassPathXmlApplicationContext context) {
        this.context = context;
    }

}

---------- Project A Implementation -----------

The way that I develop lookup is:

package co.swatit.rest.services;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import co.edu.uniquindio.model.ejb.interfaces.IReporte;

@Path("/ReporteWS")
public class ReporteWS {

    @POST
    @Path("generate1and2")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes({ MediaType.APPLICATION_JSON} )
    public Response generate1and2() {
        Properties props = new Properties();
        props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
        try {
            Context ctx = new InitialContext(props);
            // I do not know if use IReporte or ReporteEJB to cast. and I do not know how to import it.
            IReporte ejbLocal = (IReporte) ctx.lookup("java:global/Sac-report-ear-1.0.0/co.swatit-Sac-report-ejb-1.0.0/ReporteEJB");
            ejbLocal.generate1();
            ejbLocal.generate2();
        } catch (Exception exception) {
            exception.printStackTrace()
        }
        return Response.status(Status.OK)
                .entity(ejbLocal).build();
    }

}

I do not know if it is possible import the local bean in project A to lookup that bean:

import co.edu.uniquindio.model.ejb.interfaces.IReporte

I do not know if use IReporte or ReporteEJB to cast, and I do not know how to import it.

Thank you for your help.

标签: springeclipsemavenjakarta-eetomee-7

解决方案


推荐阅读