Hi,
I'm new to EJB development and currently search for the best/correct way to handle events between two EJB-JARs which are in one EAR.
I tried to use the Observer Pattern. The event gets fired in BeanA but does not reach BeanB (as stated, two different EJB-JARs = two different Eclipse Projects in one Workspace / EAR).
My Code currently looks kind of like this:
@DependsOn("BeanB")
@Startup
@Singleton
@WebService
public class BeanA implements BeanARemote, BeanBLocal{
@PersistenceContext
private EntityManager em;
@EJB
private BeanA beanA;
public void listenToReceivedEvent(@Observes ReceivedEvent events){
Query query = em.Query("some query");
//implementation
}
}
@Singleton
public class BeanB implements BeanBLocal {
@Inject
Event<ReceivedEvent> events;
public void fireEvent() {
//implementation
ReceivedEvent e = new ReceivedEvent(stringList);
event.fire(e);
}
}
public class ReceivedEvent {
private List<String> stringList;
public ReceivedEvent(final List<String> stringList) {
this.stringList= stringList;
}
public List<String> getStrings() {
return stringList;
}
}
Currently, when on BeanB the event is fired at BeanA nothing happens. My question now is: is the Observer Pattern the correct pattern in this case? Did I forgot something in this sample why the BeanA does not gets the event? Is the Observer Pattern made for event communication between two differen EJB-JARs?
Thanks