[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-11-03
14
Medium Priority
?
238 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:vasanthv16
  • 9
  • 4
14 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 12490560
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
 
LVL 92

Expert Comment

by:objects
ID: 12490564
For PropertyChangeEvents have a look at the PropertyChangeSupport class.
The EventListenerList class can also be used.
0
 
LVL 92

Expert Comment

by:objects
ID: 12490569
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 12490571
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
 

Author Comment

by:vasanthv16
ID: 12490592
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
 

Author Comment

by:vasanthv16
ID: 12490599
I am looking for -> I am not looking for
0
 
LVL 92

Expert Comment

by:objects
ID: 12490616
> 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
 
LVL 92

Expert Comment

by:objects
ID: 12490667
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
 
LVL 92

Expert Comment

by:objects
ID: 12490700
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
 
LVL 21

Accepted Solution

by:
MogalManic earned 1000 total points
ID: 12492442
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
 

Author Comment

by:vasanthv16
ID: 12494445
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
 
LVL 92

Expert Comment

by:objects
ID: 12499244
> 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
 

Author Comment

by:vasanthv16
ID: 12499505
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
 
LVL 92

Expert Comment

by:objects
ID: 12499526
Well I assumed you knew how to start a thread :)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question