Thomas
Thomas

Reputation: 88757

Specialize EJB in JBoss

I asked this a while ago within a Jboss 4.2.3 (JavaEE 5) context and got the answer to try Spring for configuration, but I'd rather not add another framework when it isn't necessary.

Now with JavaEE 6 and 7 out I have the feeling that there's a simpler solution for my problem. However I wasn't lucky searching yet so if you can get me on the right track, I'd really appreciate your help.

Here's the problem:

Suppose we have an EJB interface IEjb and a library that provides an implentation EjbA of that interface. There are several other libraries which provide further EJBs and a couple of those contain @EJB IEjb ref, i.e. they need a reference to en EJB which exposes interface IEjb.

Now, there is the need to completely replace EjbA with an alternate implementation EjbB, i.e. EjbA should never be using in the entire application.

There are a few options that we can't use or which would require rather large changes:

So there are a few ways that I could think of:

Any ideas on this? Is this possible (I have the feeling that yes) and if so, how?


A note on the environment: we're currently running on JBoss 7.2.0, but if this would only be possible using Wildfly, this might be an option (although we'd prefer not to be forced to update already).

Upvotes: 0

Views: 545

Answers (2)

Sven
Sven

Reputation: 534

A littlebit late, but remember: EJB supports transactions, where as CDI-Beans not. But you could wrap the injection of an EJB in a corresponding CDI-bean, that proxies all methods of the EJB. To get enother EJB you'll write a @Specializes-CDI for it. Example (just to show, what I mean).

The Baseimplementation looks like this:

@Stateless // or whatever
class BaseEjb {
    public void doSomeMagic() {...}
}

@Named
class BaseCdi {
    @EJB
    private BaseEjb ejb;
    public void doSomeMagic() { ejb.doSomeMagic(); }
}

The customisation works like this:

@Stateless // or whatever
class SpecializedEjb extends BaseEjb {...}

@Specializes
class SpecializedCdi extends BaseCdi {
    @EJB
    private SpecializedEjb ejb;
    public void doSomeMagic() { ejb.doSomeMagic(); }
}

Upvotes: 1

Puce
Puce

Reputation: 38152

A couple of ideas:

  • delete EjbA if it mustn't be used anyway
  • remove the EJB annotations
  • use @Inject to inject local EJBs (recommended anyway). I think CDI lets you specify a default implementation, AFAIK.

Upvotes: 1

Related Questions