Greetings,

First, you can learn more about JavaBeans at
http://java.sun.com/products/javabeans/.
For information about property change listeners, see section 7.4.1
"Bound Properties" of the JavaBeans API Specification
(http://java.sun.com/products/javabeans/docs/beans.101.pdf)


Second, the approach of using property change listeners may not work for
you.

With JavaBean property change listeners, the listeners are local objects
to the bean.  When a property on the bean changes, the bean notifies the
listeners via a local method call-back.  That may not be appropriate for
you since the beans want to watch are ENTERPRISE JavaBeans.

The limitation is that the EJB can not invoke methods on objects outside
of itself.  It must make an invocation through an intermediary mechanism
such as EJB, RMI, CORBA, Web Services, etc. - or it must send a
notification message through some intermediary mechanism such as JMS,
JavaMail, etc.


If the listener is another EJB, you may use the property change listener
scheme.  Say that an instance of EJB1 wants to watch for changes to
properties on an instance of EJB2.  You'd have to use EJB1's *>STUB<* as
the registered property change listener for EJB2 and you'd have to use
EJB2's STUB as the source of the change event.  Also, you'd have to
ensure that EJB1's remote interface extends the
java.beans.PropertyChangeListener interface.  When a property on the
instance of EJB2 changes, it notifies all of the registered
PropertyChangeListeners by locally invoking their propertyChange(...)
method - thus, it would call the propertyChange(...) method on the stub
for the EJB1 ejb object.

So, for EJB1 (the listener), it's EJBObject and Bean code would contain
code like...

    public interface EJB1
    extends EJBObject, java.beans.PropertyChangeListener
    {
        ...
    }

    public class EJB1Bean
    implements SessionBean
    {
        public void someMethod(...)
        {
            ...
            EJB2 watchedEJB = ...
            watchedEJB.addPropertyChangeListener(
(EJB1)ctx.getEJBObject() );
            // assumes EJB1 interface extends
            //   PropertyChangeListener
            ...
        }

        // required for java.beans.PropertyChangeListener
        public void propertyChange( PropertyChangeEvent evt )
        {
            EJB2 watchedEJB = (EJB2) evt.getSource();
            String name = evt.getPropertyName();
            Object oldValue = evt.getOldValue();
            Object newValue = evt.getNewValue();
            // ... do stuff ...
        }
        ...
    }

And EJB2's EJBObject and Bean code would contain code like...

    public interface EJB2
    extends EJBObject
    {
        // property that can be watched
        public void setX( String value );
        public String getX();

        public void addPropertyChangeListener( PropertyChangeListener
listener );
        public void removePropertyChangeListener( PropertyChangeListener
listener );
    }

    public class EJB2Bean
    implements SessionBean
    {
        private PropertyChangeSupport watchers
            = new PropertyChangeSupport(
                ctx.getEJBObject() ); // <-- source for all events

        private String x;

        public void setX( String newX )
        {
            String oldX = x; // old value for event
            x = newX;

            watchers.firePropertyChange( "x", oldX, x );
        }

        public void addPropertyChangeListener( PropertyChangeListener
listener )
        {
            watchers.addPropertyChangeListener( listener );
        }

        public void removePropertyChangeListener( PropertyChangeListener
listener )
        {
            watchers.removePropertyChangeListener( listener );
        }
    }

Note, you could do something similar with other remote listeners such as
simple RMI or CORBA.

DISCLAIMER:  I haven't tried this approach.  This code is untested.
You might have problems with method arguments not being Serializable.
The EJB compiler might not let you compile your EJB2 remote interface
since the arguments to the add/removePropertyChangeListener methods
(i.e. PropertyChangeListener) are not Serializable.  In that event, I
would create a new interface like ...

    public interface SerializablePropertyChangeListener
    extends java.io.Serializable,
            java.beans.PropertyChangeListener
    {
    }

... and use it where ever I would have used PropertyChangeListener.


If the listener is not an EJB (or RMI) object, then you might consider
having the EJB send notifications via JMS
(messaging-oriented-middleware) by posting a message on a queue/topic
where other components can retrieve it.


Louis W. Lembcke, Principal
Chicago Systems Group
180 N. Stetson, Suite 3200
Chicago, IL 60601 USA
Main Tele: +1 312.444.2760
Facsimile: +1 425.969.6391
[EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>
http://www.ChicagoSystemsGroup.com/


-----Original Message-----
From: Chitrapandian N [mailto:[EMAIL PROTECTED]]
Sent: Sunday, May 26, 2002 12:37 AM
To: JDJList
Subject: [jdjlist] Re: Observers in EJB..


Hi..,
        Any body known about .. bean property change listeners ..??
        Is there any good document available ..??

Chithuu

Tomm Carr wrote:

> Chitrapandian N wrote:
>
> >Hi..!
> >    My requirement is ..
> >
> >            How to notify observers.. when something changes in
> >bean..??..
> >
> >For eg.: I have a MusicCD Bean.., whenever delete MusicCD is called,
> >observer, who wants notification.. while deleting should be
notified..??
> >
> >Is there any observer notification framework available ??
> >
> Any reason standard bean property change listeners wouldn't work?
>
> Tomm
>
> To change your membership options, refer to:
> http://www.sys-con.com/java/list.cfm


To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm


To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm

Reply via email to