我有一段代碼,我試圖理解,我從來沒有使用過運行時監聽器,所以我會很感激如果有人能指點我一個很好的教程/幫助我明白這段代碼的作用。無法清楚地瞭解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