0

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...

2
  • You should think about the reason why a Stateless bean is called "Stateless". You're trying to put conversational state into a bean which is precisely designedto not allow conversational state (hence the term "stateless"). Commented Oct 12, 2014 at 15:11
  • Thanks for pointing that out JB. I've since thought about it and changed it to a Singleton but the results are still the same. The EventProxy gets the notification from the bean without any issues but the EventProxy never notifies any of its subscribers because its subscriber list is empty even after registering multiple listeners successfully. I'm actually trying to remove the conversational state from the bean and let the EventProxy POJO deal with that. Since the EventProxy gets the update successfully from the bean I think the problem lies within the EventProxy. Commented Oct 12, 2014 at 21:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.