我有一段代码,我试图理解,我从来没有使用过运行时监听器,所以我会很感激如果有人能指点我一个很好的教程/帮助我明白这段代码的作用。无法清楚地了解java中的运行时监听器
一类的代码给出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);
}
现在下面给出的是“刮”讲座,我只给这指的是类的代码下面的一些代码为此我给上面的代码(刮板运行时监听器类)...
首先出现的是在类的声明中的部分members--
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);
}
}
你不明白什么?它非常直接地使用了一个接口。 – Mat 2012-02-04 07:45:46
@Arvind这看起来像某种异步回调。无论如何,你能描述你不明白的东西吗? – Eugene 2012-02-04 07:49:31
@Eugene目前多个刮板正在建立并运行上述代码......我只想一次运行一个进程(并且稍后使用线程池来执行代码,以便一次运行一个进程).. 。我无法弄清楚如何理解/提取运行一个刮板实例所需的代码。 – Arvind 2012-02-04 07:52:41