我有一段我想要理解的代码,我以前从未使用过运行时监听器,所以如果有人能指点我一个好的教程/帮助我理解这段代码的作用,我将不胜感激。< / p>
下面给出了一个类的代码 -
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'类中的一些代码 - 我只给出下面引用我上面给出的代码的代码(刮刀运行时监听器类)...
首先,类成员声明中有一节 -
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);
}
}
答案 0 :(得分:3)
代码正在使用observer pattern。刮刀通过调用方法,启动时,完成时,暂停时等通知听众(或观察者)。
答案 1 :(得分:0)
我不完全确定我理解你的意思,但是如果你编写一个实现这个ScraperRuntimeListener的类并且同一个类实现Runnable,那么你可以将它提交到池中。在内部运行中,您可以注册(可能有一种方法可以执行此操作)此类可以作为侦听器。