Got a strange issue with Observers disappearing from an Observable object's observer list. Here's a little background of my project.
I have a @Stateless Timer Session Bean that every 10 seconds broadcasts the current time and date on the server to all the observers that are observing it.
@Stateless public class TimeBroadcastService extends Observable implements TimeBroadcastServiceLocal { @Schedule(hour = "*", minute = "*", second = "*/10") @Override public void broadcast() { setChanged(); notifyObservers(new Date()); } @Override public void addNewObserver(Observer o) { this.addObserver(o); } } This is deployed to GlassFish (4.1) and the only thing to every subscribe to this is a POJO which I called EventProxy.
EventProxy is an Observer of the above mentioned TimeBroadcastService and at the same time is also an Observable for other clients (mostly Swing applications). Here is the code for EventProxy:
public class EventProxy extends Observable implements Observer, Serializable { @Override public void update(Observable o, Object arg) { System.out.println("[EventProxy] received update from TimeBroadcastService bean => " + arg.toString()); System.out.println("[EventProxy] notifying " + countObservers() + " listeners"); setChanged(); notifyObservers(arg.toString()); } @Override public void addObserver(Observer o) { super.addObserver(o); System.out.println("[EventProxy] adding new observer (" + countObservers() + ")"); } } When the TimeBroadcastService sends out an update with the new time, EventProxy.update() receives it and I can see it write the debug message along with the actual date send from the bean. However this message is never broadcast from EventProxy to any of its observers because countObservers() returns 0 indicating that there are no observers subscribed to EventProxy. The weird thing is that I actually have 3 observers subscribing to EventProxy; I can see it via the debug messages:
[EventProxy] adding new observer (1) [EventProxy] adding new observer (2) [EventProxy] adding new observer (3) However when the next update comes in from the EJB suddenly the list is empty and I get this:
[EventProxy] received update from TimeBroadcastService bean => Sun Oct 12 10:09:10 EDT 2014 [EventProxy] notifying 0 listeners [EventProxy] received update from TimeBroadcastService bean => Sun Oct 12 10:09:20 EDT 2014 [EventProxy] notifying 0 listeners [EventProxy] received update from TimeBroadcastService bean => Sun Oct 12 10:09:30 EDT 2014 [EventProxy] notifying 0 listeners [EventProxy] received update from TimeBroadcastService bean => Sun Oct 12 10:09:40 EDT 2014 [EventProxy] notifying 0 listeners [EventProxy] received update from TimeBroadcastService bean => Sun Oct 12 10:09:50 EDT 2014 [EventProxy] notifying 0 listeners Could anyone shed some light why all my registered Observers are disappearing from EventProxy's observer list after being successfully registered ? The goal here is to distribute events originating from the TimeBroadcastService through the EventProxy to multiple clients. I'm just starting out with Java EE so it's quite possible I'm looking over something trivial or just not thinking "the Java EE" way. Thanks in advance...