Java event handling and code execution - EventObject

What happens when custom EventListener

processes custom EventObject

?

Once the handler is done:

  • Is there a fallback to standard stream code? or,
  • Is the stream closed?
+3


source to share


2 answers


EventListeneris only an interface (actually a very low level) and it only defines what needs to be implemented. How this is implemented is determined by the class that implements the interface. So the question has to be some specific class. It can be run in a separate thread or create a new thread or use the current one. Different classes handle it differently. I believe most of them just call the listener method on the current thread. This is the reason why it is not recommended to do anything for a long time (in fact, nothing at all but a few commands to notify the event) inside the listener itself. What when flexibility comes - you decide what to do and when. Start a new thread and end it when the work is done, or use an existing thread, or quickly complete your work, and hopethat the current thread will not get stuck and, for example, the GUI window will not be frozen.



+1


source


I am creating my own EventListener interface instead of using the standard Java EventListener interface. I am doing this to define the method (s). In this case, I created a handleEvent method that receives a ResultSetEventObject object.

package com.ggl.event.listener;

public interface EventListener {

    public void handleEvent(ResultSetEventObject eo);

}

      

So let's take a look at the ResultSetEventObject class.

package com.ggl.event.listener;

import java.sql.ResultSet;
import java.util.EventObject;

public class ResultSetEventObject extends EventObject {

    private static final long serialVersionUID = 6904165475135245131L;

    private ResultSet resultSet;

    public ResultSetEventObject(Object source) {
        super(source);
    }

    public ResultSet getResultSet() {
        return resultSet;
    }

    public void setResultSet(ResultSet resultSet) {
        this.resultSet = resultSet;
    }

}

      

This class contains SQL ResultSet and has getter and setter for ResultSet. You can have whatever variables you want in the EventObject along with the getters and setters variables.



Finally, let's use the interface in a real class, ResultSetListenerHandler.

package com.ggl.event.listener;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ResultSetListenerHandler {

    private List<EventListener> listeners;

    public ResultSetListenerHandler() {
        listeners = new ArrayList<EventListener>();
    }

    // Add method(s) to generate a ResultSet and perform the
    // fireEvent method.

    public void addListener(EventListener listener) {
        listeners.add(listener);
    }

    public void removeListener(EventListener listener) {
        for (int i = listeners.size() - 1; i >= 0; i--) {
            EventListener instance = listeners.get(i);
            if (instance.equals(listener)) {
                listeners.remove(i);
            }
        }
    }

    public void fireEvent(final ResultSet resultSet) {
        for (int i = 0; i < listeners.size(); i++) {
            final EventListener instance = listeners.get(i);
            Runnable runnable = new Runnable() {
                public void run() {
                    ResultSetEventObject eo = new ResultSetEventObject(
                            resultSet);
                    eo.setResultSet(resultSet);
                    instance.handleEvent(eo);
                }

            };
            new Thread(runnable).start();
        }
    }
}

      

There is a method for adding listeners and a method for removing listeners.

The fireEvent method is the method that actually executes the listener code for each of the listeners.

In this case, I created a separate thread for each of the listeners. So the fireEvent method ends quickly. The code in the ResultSetListenerHandler does not have to wait for the listener code to complete.

0


source







All Articles