gpt4 book ai didi

java - 无法清楚理解java中的运行时监听器

转载 作者:行者123 更新时间:2023-12-02 00:30:46 31 4
gpt4 key购买 nike

我有一段代码想要理解,我以前从未使用过运行时监听器,所以如果有人能给我指出一个很好的教程/帮助我理解这段代码的作用,我将不胜感激。

下面给出一个类的代码--

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);

}

下面给出的是“Scraper”类的一些代码 - 我仅在下面给出引用我上面给出的代码的类(scraper 运行时监听器类)的代码...

首先有一个类成员声明部分--

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

然后是一些利用上面类的函数---

 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);
}
}

最佳答案

代码使用 observer pattern 。抓取器通过调用监听器(或观察者)的方法来通知监听器(或观察者)何时开始、何时完成、何时暂停等。

关于java - 无法清楚理解java中的运行时监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9139285/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com