nobody
nobody

Reputation: 1949

Two EJBs with same interface. Is it a good practice ?

My use case requires me to have a class hierarchy as shown below

public interface ServiceA{ public void doSomething();}


public abstract class AbstractClass implements ServiceA{

     @Override
     public void doSomething(){
        getMetaValue();        
        .. do common Things applicable to all EJBS...
     }

     public abstract MeataValue getMetaValue();
}

@Stateless(mappedName="EJBBeanImlJ")
public EJBBeanImplJ extends AbstractClass{
   public MetaValue getMetaValue(){
      return new MetaValue(x, y);
   }
}

@Stateless(mappedName="EJBBeanImplK")
public EJBBeanImplK extends AbstractClass{

   public MetaValue getMetaValue(){
       return new MetaValue(a,b);
   }
}

Questions :

  1. Is it a good EJB practice to have same interface for two EJB implementations ?

  2. Do you see any other drawbacks in class design/hierarchy ?

Note : My appserver is Weblogic

Thanks

Upvotes: 4

Views: 4457

Answers (2)

AlexR
AlexR

Reputation: 115328

EJB is just a kind of special class. It is special because its life cycle is managed by container. It is just a class because it is written in java and may implement any interface and any business logic it wants.

So, I think that it is a good practice to have several implementations of the same interface. It allows separation of implementation of specific service from its usage. For example you can create interface Sender that can send some content and 2 implementations: EmailSender and SmsSender. Both implement the same interface, both are EJBs.

The only problem is that in this case you cannot bind reference to sender by its interface only, but have to use mappedName as you did.

Upvotes: 5

home
home

Reputation: 12538

  1. I can't see any technical problem with this as you defined different names for the 2 EJBs, should work...
  2. I do not understand your concrete requirements, so I may be wrong: If the service interface is implemented in AbstractClass and not overridden in one of the EJBs, it seems to be practical to have one separate EJB implementing ServiceA. Second, instead of having 2 different EJB implementations for the same interface getMetaValue, I'd rather parameterize the interface and let the EJB decide whether it wants to implement new MetaValue(x, y) or new MetaValue(a, b), e.g. abstract public MetaValue getMetaValue(SomeParameter param);

Upvotes: 2

Related Questions