Arvind
Arvind

Reputation: 6474

cannot clearly understand run time listener in java

I have a section of code that I am trying to understand, I have never worked with a run time listener before so I would be grateful if somebody could point me to a good tutorial/help me understand what this code does.

The code for one class is given below--

public interface ScraperRuntimeListener {

  public void onExecutionStart(Scraper scraper);

  public void onExecutionPaused(Scraper scraper);

  public void onExecutionContinued(Scraper scraper);

  public void onNewProcessorExecution(Scraper scraper, BaseProcessor processor);

  public void onExecutionEnd(Scraper scraper);

  public void onProcessorExecutionFinished(Scraper scraper, BaseProcessor processor, Map properties);

  public void onExecutionError(Scraper scraper, Exception e);

}

Now given below is some code from 'Scraper' class- I am only giving below the code that refers to the class for which I have given code above (the scraper run time listener class)...

First there is a section in declaration of class members--

  private List<ScraperRuntimeListener> scraperRuntimeListeners = new LinkedList<ScraperRuntimeListener>();

Then there are some functions which utilise the above class---

 public Variable execute(List<IElementDef> ops) {
    this.setStatus(STATUS_RUNNING);

    // inform al listeners that execution is just about to start
    for (ScraperRuntimeListener listener: scraperRuntimeListeners) {
        listener.onExecutionStart(this);
    }

    try {
        for (IElementDef elementDef: ops) {
            BaseProcessor processor = ProcessorResolver.createProcessor(elementDef, this.configuration, this);
            if (processor != null) {
                processor.run(this, context);
            }
        }
    } finally {
        releaseDBConnections();
    }

    return new EmptyVariable();
}

public void setExecutingProcessor(BaseProcessor processor) {
    this.runningProcessors.push(processor);
    Iterator iterator = this.scraperRuntimeListeners.iterator();
    while (iterator.hasNext()) {
        ScraperRuntimeListener listener = (ScraperRuntimeListener) iterator.next();
        listener.onNewProcessorExecution(this, processor);
    }
}

 public void processorFinishedExecution(BaseProcessor processor, Map properties) {
    Iterator iterator = this.scraperRuntimeListeners.iterator();
    while (iterator.hasNext()) {
        ScraperRuntimeListener listener = (ScraperRuntimeListener) iterator.next();
        listener.onProcessorExecutionFinished(this, processor, properties);
    }
}


 public void addRuntimeListener(ScraperRuntimeListener listener) {
    this.scraperRuntimeListeners.add(listener);
}

 public void removeRuntimeListener(ScraperRuntimeListener listener) {
    this.scraperRuntimeListeners.remove(listener);
}

public synchronized int getStatus() {
    return status;
}

private synchronized void setStatus(int status) {
    this.status = status;
}

public void stopExecution() {
    setStatus(STATUS_STOPPED);
}

public void exitExecution(String message) {
    setStatus(STATUS_EXIT);
    this.message = message;
}


   public void continueExecution() {
    if (this.status == STATUS_PAUSED) {
        setStatus(STATUS_RUNNING);

        // inform al listeners that execution is continued
        Iterator listenersIterator = this.scraperRuntimeListeners.iterator();
        while (listenersIterator.hasNext()) {
            ScraperRuntimeListener listener = (ScraperRuntimeListener) listenersIterator.next();
            listener.onExecutionContinued(this);
        }
    }
}

/**
 * Inform all scraper listeners that an error has occured during scraper execution.
 */
  public void informListenersAboutError(Exception e) {
    setStatus(STATUS_ERROR);

    // inform al listeners that execution is continued
    Iterator listenersIterator = this.scraperRuntimeListeners.iterator();
    while (listenersIterator.hasNext()) {
        ScraperRuntimeListener listener = (ScraperRuntimeListener) listenersIterator.next();
        listener.onExecutionError(this, e);
    }
}

Upvotes: 0

Views: 317

Answers (2)

Eugene
Eugene

Reputation: 120848

I am not completely sure I understand what you mean, but how about if you code a class that implements this ScraperRuntimeListener and the same class implements Runnable for example, so that you could submit it to a pool. Inside run you could register (there is probably a way to do this) this class to be the listener for example.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691655

The code is using the observer pattern. The scraper notifies listeners (or observers), by calling their method, when it starts, when it finishes, when it pauses, etc.

Upvotes: 3

Related Questions