users@glassfish.java.net

Re: EJB3 Observer pattern

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Tue, 19 Feb 2008 01:28:28 +0100

Hi there,
my first though about how to improve my application was a JMS way. I
wanted to make it like this:

my Observable, when it has something that might be interesting for
others - it publishes it using JMS. Observers, in that case are MDBs
and they can do whatever they want with a message. Looks like an ideal
solution, easy to introduce, easy and straightforward to implement, no
boilerplate code, just publisher and subscribers invoked automatically
by JMS infrastructure when messages are available.There is but two
huge problems with this approach:

1) message driven beans are operating in its own context.They don't
continue publisher's transactions and they know nothing about
publisher's context, so for example, if that MDB throw an exception, I
will not be able to roll back changes made by my initial Observable. I
do not want my loan to be activated successfully with no documents
attached to it only because there was some problem with document
generator. I want to have loan activated with documents and everything
else or nothing. This is something JMS will not provide.

2) JMS is prepared to work in inter-JVM environment which means it
will serialize/deserialize messages, so I cannot pass just a pointers.
This is often not an issue, but I have a case like this: there is
LoanInspectorBean that is to be invoked once a day. It has to check
few thousands or sometimes few dozen thousands loans to see if each of
them should change status (was:Active, is:Late or was:Late, is:
Terminated). Moreover, this service is supposed to generate some
transactions if, for example, the loan can be the subject for
extinction or whatever. There are few services that might be
interested in the information I am processing in my LoanInspectorBean.
If I would use JMS to notify my observers, my extra uber huge message
would be serialized and send to them, they would have to de-serialize
it and process, which introduces waste of lot of CPU/memory resources.
And again (see no1) I do not want to allow few observers to accomplish
their task while others might failed.

So, this is how my initial idea about JMS died :(
There must be some other way to make EJB3 services easy to decouple...
or maybe not :/
What intrigue me the most is I did not see anything like this in
Spring examples. If Spring could provide some great solution and if
I'd manage to integrate it with my application easily, I could use
Spring beans for observers job.

Regards,
Witold Szczerba


2008/2/19, glassfish_at_javadesktop.org <glassfish_at_javadesktop.org>:
> <p>Hi, Witold; I have not yet done the kind of thing you're talking about, but I am thinking about it almost constantly. :-)</p>
>
> <p>My "in my head" solution is/was going to be as follows:
>
> <ol>
> <li>Write one parameterized Interceptor that can be configured to post a JMS message to a queue somewhere when a method completes (the JEE equivalent of firing a <tt>loadActivationEvent</tt> to a series of <tt>LoanActivationListeners</tt> in your case).</li>
> <li>Write each "observer" to be a JMS subscriber or message-driven bean that you hook up to that queue.</li>
> </ol></p>
>
> <p>I realize this is very high level--does that give you an idea on one way to proceed? Are there others out there who have tried this approach and found it either lacking or serviceable?</p>
>
> <p>Best,<br/>
> Laird</p>
> [Message sent by forum member 'ljnelson' (ljnelson)]
>
> http://forums.java.net/jive/thread.jspa?messageID=259580
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>