• Post Reply Bookmark Topic Watch Topic
  • New Topic

Communication between observer and observable  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am a requirement for which I have used the observer pattern. Now that solves the problem but I am come across another blocker. That is the sequence at which the observer and observable initialize.

Here are the classes:







Now the problem is this, no matter which class initializes first, certain sequences should not change.

Sequence flow is like this:



But what is happening is this:


Now the instance of Subject is being passed to Tracker, that part is not specified in this code. But in actual scenario, they do get the references properly.

Please let me know how to control the sequence of events. Should I use a CountDownLatch or any other blocking construct to wait till registerItems() is called ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:Please let me know how to control the sequence of events. Should I use a CountDownLatch or any other blocking construct to wait till registerItems() is called ?

Don't worry about the mechanics (blocking constructs, countdown latches, etc) right now; you'll tie yourself in knots. Concentrate on what needs to happen.

What is an Observer? How does it work? What creates an Observer?

Same questions for Observable.

One possible idea: Forget about Observer and Observable for a minute, and just write a class that includes a Scanner that continually reads a line from the keyboard.
If the line is "x", print out a message "got x"; if it's "y", print out "got y"; if it's "z", print out "got z"; and if it's "q", exit. And make sure it works.

Now, instead of printing the messages directly, create three new classes: XMessage, YMessage and ZMessage that print them for you - you could even test them with your class before you go any further to make sure they work.

And now try to tie them together as Observers and Observables.
Which one(s) need to be the Observer and which one(s) the Observable?
What do you have to do to "connect" your objects together so that they behave exactly like your original class?

I think that'll help you to understand what the responsibility of each one is and how they link together, and NOW you should be in a better position to implement your Tracker and Subject classes.

HIH

Winston
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I am clear with what is supposed to happen. Most of the logic is working and complete in itself. I have updated my code to reflect the logical issue that I have.








Here is my problem. The Tracker has a method call registerItems(), which can be called after the service is up. But when it will be called is not deterministic. Also, its completion time is also not fixed.

Subject has a method loadInitialItems(), which fills the initial item data. The call to this method loadInitialItems() is fixed. It is called when subject is initialized.

Problem comes when loadInitialItems() is called before registerItems() at Tracker. Unless I have the itemReference, I cannot process those product. Hence, the nullpointer exception which I get.
 
Salil Wadnerkar
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Subject ov = new Subject(0);
Tracker to = new Tracker(ov);
ov.addObserver(to);
ov.loadInitialItems();


The problem is your main method makes the decision of registering the observer with the obeservable, without checking whether your observer (Tracker) is ready.
Only the observer knows when it is ready, which is, after the registerItems is complete. So, your Tracker should register itself as an observer only after this is complete.

I am not sure whether you really need Observer pattern here. You seem to have only one instance of Observer and Observable. Observer pattern is used when the Observable does not need to care for how many Observers are there (if any). And Observers can register/deregister themselves whenever they wish to.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When is registerItems ever called?
The code should test the value returned by the get method. null is a valid value to be returned.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know there is this dependency which I did not thought of, when I decided to use the observer pattern. The requirement is communication between two applications that will be started from a single main, but on different threads. And once both the applications are up and running, application 2 needs to send events to application 1 as and when it receives it.
That's why I thought I will put application 2 on hold till application 1 is ready to take the events.

Registeritems() is called when application 1 is initialized. This call may take up to 30 seconds to complete. When exactly it will call, that is hard to tell.

I can test for null values, but that doesn't solve my problem. The initial data that will be sent to application, 1 will be called when application 2 is coming up. So, if application 1 has a null check, and it misses these initial entries, I will not get this data again until the next restart.

The main objective is to fire events from application 2 which reaches application 1.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method here is just an example. Like I said before, the registering of observer with observable is already handled. It is linked when application 1 and application 2 are coming up. There is no static binding. What I cannot do is stop the processing at application 1 till registeritems is complete, as I would miss the data application 2 has passed on.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Registeritems() is called when application 1 is initialized.

Can you post the code that has that in it? I don't see any calls to regiserItems in the posted code.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Registeritems() is called when application 1 is initialized.

Can you post the code that has that in it? I don't see any calls to regiserItems in the posted code.


Do not have the code with me right now. The registeritem calls a socket application. once connected it will receive some feed parameters which I need to processed the item references.

This is the closest reference to the actual workflow:

 
Salil Wadnerkar
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have two applications - where application1 is firing events and application2 is consuming those. And application1 can start firing events when application2 may not be ready, then you need to store these events in a queue either at the producer or at the consumer (since you don't want to lose them). I suggest you store them at the consumer and create another thread to actually consumer these messages. For this kind of producer-consumer of messages problem, check out BlockingQueue
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not have the code with me right now

Ok. But without a call to that method the posted code can't work.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Do not have the code with me right now

Ok. But without a call to that method the posted code can't work.


Actually it does. You see, when the Tracker application initializes, it will initiate the socket call. Once we get the response from it, we are all locked and loaded.

I was also thinking of some sort of queuing of events triggered before it can be consumed. But my idea was to use a sort of list which will store all events not consumed by tracker. once the application is ready to process events, I will call a method which will consume all unprocessed events in one shot. this is non blocking, but shd work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!