I'm working on a project with JAX-WS.

I need to do like this. (I'm even not sure it's possible or not)

Lets say there are three EJB like this.

public abstract class AbstractEjb<T extends A>

@Stateless
@LocalBean
public class ConcreteEjbB extends AbstractEjb<B>

@Stateless
@LocalBean
public class ConcreteEjbC extends AbstractEjb<C>

Currently my endpoints looks like this

@WebService
public class ConcretEndpointB {

    @Ejb private ConcretEjbB;
}

@WebService
public class ConcretEndpointC {

    @Ejb private ConcretEjbC;
}

Can I do like this?

public class AbstractEndpoint<T extends A> {

    public AbstractEndpoint(final Class<T> ejbType) {
        super();
        this.ejbType = ejbType;
    }

    @WebMethod
    public String echo(@WebParam final String message) {

        //return ejb.echo(message); // :(

        // now can I look up the actual ejb instance with 'ejbType'?

        lookup(ejbType).echo(message);
    }

    private final Class<T> ejbType;

    @EJB protected AbstractEjb<T> ejb; // I don't think this gonna work.
}

@WebService
public class ConcretEndpointB extends AbstractEndpoint<B> {

    public ConcreteEndpointB() {
        super(ConcreteEjbB.class);
    }

    //public String echo(final String message) {
    //    return ejbB.echo(message);
    //}

    //@Ejb private ConcretEjbB ejbB;
}

@WebService
public class ConcretEndpointC extends AbstractEndpoint<C> {

    public ConcreteEndpointC() {
        super(ConcreteEjbC.class);
    }

    //public String echo(final String message) {
    //    return ejbC.echo(message);
    //}

    //@Ejb private ConcretEjbC ejbC;
}

I think it won't work.

My real question is; is there any portable and standard way to locate the actual concrete bean with specifed ejbClass?

有帮助吗?

解决方案

The injection point "@EJB protected AbstractEjb ejb" won't work the way you might expect. You have to remember that type erasure occurs so you can't be certain that the injected EJB is of the expected type. In such case a ClassCastException will be thrown. You might want to refactor your code to avoid generics on the actual EJB implementations and have a Local interface extend the generic abstraction instead. A more thorough explanation of EJB and generics can be found at http://blog.diabol.se/?p=353.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top