Threaded Java Event Handling ? (Not UI but just regular event handling)

Hi,

I need to use event handling in my development task. I have event listeners and an event source. If an event is generated, the source notifies the listeners. What I need here is, how do I make the listeners receive the event in separate threads?

I can do custom coding and make the clients run on individual threads, but I would like to know whether there is an existing framework to do this. In short, my question is, is there threaded event handling support in Java.

I would also like to mention that I do not want to use the awt package. My concern is not with UI but with normal event handling.

Thanks,
Silent Killer
vasanthv16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
typically the listener is just called (on the same thread)
If a listener wants to handle the event in a seperate thread then it is responsible for handling that.
0
objectsCommented:
For PropertyChangeEvents have a look at the PropertyChangeSupport class.
The EventListenerList class can also be used.
0
objectsCommented:
All you really need though is List (such as a Vector) to store your listeners in, then when you want to fire an event you loop thru the list calling the relevant method on each of the registered listeners.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

objectsCommented:
EventListenerList xxxListeners = new EventListnerList();

public void addXXXListener(XXXListener listener)
{
     xxxListeners.add(XXXListener.class, listener);
}

public void removeXXXListener(XXXListener listener)
{
     xxxListeners.remove(XXXListener.class, listener);
}

protected void fireXXX(XXXEvent xxxEvent)
{
     Object[] listeners = xxxListeners.getListenerList();

     // loop through each listener and pass on the event if needed
     Int numListeners = listeners.length;
     for (int i = 0; i      {
          if (listeners[i]==XXXListener.class)
          {
               // pass the event to the listeners event dispatch method
                ((XXXListener)listeners[i+1]).dispatchXXX(xxxEvent);
          }            
     }
}

0
vasanthv16Author Commented:
Object,

Glad to receive so many comments within minutes of posting my question.

But I would like to say that most of your comments did not help me.

PropertyChangeEvent is in the bean package and the EventListenerList is in the awt package. Both these packages are for other specific purposes. But what I am looking for is some assistance for event handling in code Java.

I do know how to pass events from a source to listeners. My question, again, is is there a frameword for threaded event handling. As I had clearly posted in my question, I can do custom coding but I want to know whether there is an existing framework.

You did mention that the listener is responsible for handling the event in a separate thread. If thats the case, then custom coding it is!

I think I am looking for a quick fix but a logical answer.

Thanks,
Silent Killer
0
vasanthv16Author Commented:
I am looking for -> I am not looking for
0
objectsCommented:
> PropertyChangeEvent is in the bean package and the EventListenerList is in the awt package. Both these packages are
> for other specific purposes.

PropertyChangeSupport is for a specific purpose as I stated.
EventListenerList can be used with any EventListener subclass, it is not limited to dealing with AWT.

I also mentioned that a List could also be used

> I do know how to pass events from a source to listeners.

You call the relevent method of the listener as I showed in the code above

> My question, again, is is there a frameword for threaded event handling.

No, except for the class I already mentioned.
0
objectsCommented:
the following discusses event handing and though it is referring to component events the concepts are the same for any type of events.
http://www.javaworld.com/javaworld/javaqa/2000-08/01-qa-0804-events.html
0
objectsCommented:
Here's a generic example of how to handle listener notification:


public interface MyListener
{
   public void eventHappened(A a, MyEvent event);
}

public class A
{
   Vector Listeners = new Vector();

   public void addListener(MyListener l)
   {
      Listeners.add(l);
   }

   public void removeListener(MyListener l)
   {
      Listeners.remove(l);
   }

   protected void fireEvent(MyEvent e)
   {
      Iterator i = Listeners.iterator();
      while (i.hasNext())
      {
         MyListener l = (MyListener) i.next();
         l.eventHappened(this, e);
      }
   }
}
0
MogalManicCommented:
To do what you want all you need to do is make an abstract threaded listener.  Maybe something like this:

public class AbstractThreadedListener
{
   class ThreadHandler implements Runnable {
     public void run()
     {
          try {
              this.handleEvent();
          catch(Exception e)
          {
              log.info("Handler through exception, e);
          }
      }
   }

   abstract void handleEvent();

   public void eventHappend(Object o, Event e)
   {
      Thread handleThread=new Thread(new ThreadHandler(o,e));
      handleThread.start();
   }
}

The event is fired just like in Object's example.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vasanthv16Author Commented:
Object,

I feel that your reply again was a little offtrack. The example which you had given was for a generic event handling mechanism. I already know that. What I wanted was event handling that can run in separate threads.

MogalManic,

Your reply gave me the solution I was looking for. Since it turns out that there is no support for threaded (non-gui) approach to event handling, it has to be custom coding eh.

But when I started doing that, I made the event listener class runnable and that was ugly. Because conceptually I did not want the listener class to be a thread. Having an inner class seems a perfect way to avoid that, which I didnt think of.

Thanks Object and MogalManic.

I would like to split the points since Object gave quite a few suggestions initially and MogalManic finished it off. Any problem guys?

Thanks,
Silent Killer
0
objectsCommented:
> What I wanted was event handling that can run in separate threads.

As I first mention, your listener just needs to process the event in whatever thread it wants.

> Since it turns out that there is no support for threaded (non-gui) approach to event handling

Which I already stated.
(gui or non-gui makes no difference, Swing/AWT event handling is also non-threaded).
0
vasanthv16Author Commented:
Object,

Yeah you said that the event listener will have to process the thread in whatever way it wants.

At that point, I was still hoping that there would be some framework for threaded event handler. But after MogalManic replied, I decided to go with custom coding. And he also gave a good example of how to do it the threaded way.

Your example was for a normal event handling mechanism which I already knew. It was not what I wanted.

As an aside, are you sure that Swing/AWT event handling is not threaded? I was thinking it was threaded (event listeners received it in separate threads). Not that I need it but I just want to get the facts straight.

-Silent Killer
0
objectsCommented:
Well I assumed you knew how to start a thread :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.