I have a question about the Observer patterne.
I have tried to implement it im my application but without result. First my idea was to save all the client Object references in A linked List in a bean on the server. Then each time a certain event ocurs on my server I will call a method fire() which iterate through my linked list and finds all my client references. Then I will make a "call back" to all the clients which are in my linked list. But my teacher tells my that is not the way to do. He belives that there is a much easier way to make sure that all the clients get updates. But he does't know how to do it. So my question is how do I implements Observer Design patterns in the J2ee enviroment ??
Read this excellent pattern article from Greg,
It even has sample code, how easy can it be?
joost I have read this article. Greg uses a Set to save the references. But in his example he call update method on the Bean. What i need is a way to make a call back to all my clients and not the Bean. In RMI which is underneath the j2ee you use a interface whith a update method. The server use this interface to call a update method on the client. Is that possible in j2ee ?
Secondly My teacher claim that there should be a easier way to do this, instead of saving all the references in a Set. He claims that the j2ee enviroment has a better way to do this ?
first, I think you won't be able to call back a J2EE client using RMI, unless your client is a Java application. Even if it is an applet, the recommended design is to place a servlet between it and the EJB tier. That's because Web clients should talk to the server using HTTP, not RMI over IIOP (which is usually blocked by firewalls).
A better way to implement the Observer pattern under J2EE, in my opinion, is to make use of Message Driven Beans in publish-subscribe mode.
I think you'll need a stateful session bean to represent each client. The stateful session bean publishes a JMS message on behalf of the client. The message driven bean consumes the message and publishes the result. All clients subscribing to this topic will be notified about the just published result as if they were getting a call back.
Hope this helps,
I would recommend you read Tyler Jewels's MDB book chapter available online before you attempt using Cristina's suggestion. There are a number of common gotcha's in implementing such an approach (as well as interesting, similar alternatives) and they are covered quite thoroughly.
Cris would it be possible for you to show me some source code please. So far your suggestion seems to be a solotion to my problem eventhough it seems to be a little bit complicated. So if I see some code I would probearly be able to do it my self. :-)
first, I suggest you take a look at the MDB tutorial posted here at The Server Side:
Then, you can also study the proto-pattern I sent to The Server Side a few months ago:
Item 2.8, "Consequences and Related Ideas", explores a possible implementation of the Observer pattern using MDBs. Please notice it's just an idea: I haven't further developed and tested that code (although I will as soon as I have some spare time :-)).
Hope this helps,
Cristina I have just one question. JSM works with the Bean but when it comes to the client - callback it seems not to be working.