SOLR-938 -- Add event listener API for import start and end

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@732695 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-01-08 12:52:16 +00:00
parent d92209b51b
commit 0bae610c14
5 changed files with 89 additions and 2 deletions

View File

@ -47,6 +47,9 @@ New Features
9. SOLR-910: Add a few utility commands to the DIH admin page such as full import, delta import, status, reload config.
(Ahmed Hammad via shalin)
10.SOLR-938: Add event listener API for import start and end.
(Kay Kay, Noble Paul via shalin)
Optimizations
----------------------
1. SOLR-846: Reduce memory consumption during delta import by removing keys when used

View File

@ -57,11 +57,15 @@ public class DataConfig {
public List<Entity> entities = new ArrayList<Entity>();
public String onImportStart, onImportEnd;
public Document() {
}
public Document(Element element) {
this.deleteQuery = getStringAttribute(element, "deleteQuery", null);
this.onImportStart = getStringAttribute(element, "onImportStart", null);
this.onImportEnd = getStringAttribute(element, "onImportEnd", null);
List<Element> l = getChildNodes(element, "entity");
for (Element e : l)
entities.add(new Entity(e));

View File

@ -92,6 +92,21 @@ public class DocBuilder {
return resolver;
}
private void invokeEventListener(String className) {
try {
EventListener listener = (EventListener) loadClass(className, dataImporter.getCore()).newInstance();
int currentProcess = -1;
if (dataImporter.getStatus() == DataImporter.Status.RUNNING_DELTA_DUMP) {
currentProcess = Context.DELTA_DUMP;
} else {
currentProcess = Context.FULL_DUMP;
}
listener.onEvent(new ContextImpl(null, getVariableResolver(), null, currentProcess, session, null, this));
} catch (Exception e) {
DataImportHandlerException.wrapAndThrow(DataImportHandlerException.SEVERE, e, "Unable to load class : " + className);
}
}
@SuppressWarnings("unchecked")
public void execute() {
dataImporter.store(DataImporter.STATUS_MSGS, statusMessages);
@ -114,6 +129,11 @@ public class DocBuilder {
List<String> entities = requestParameters.entities;
// Trigger onImportStart
if (document.onImportStart != null) {
invokeEventListener(document.onImportStart);
}
for (DataConfig.Entity e : document.entities) {
if (entities != null && !entities.contains(e.name))
continue;
@ -137,10 +157,16 @@ public class DocBuilder {
} else if (requestParameters.commit) {
// Debug mode, commit if commit=true was specified
commit();
if (document.onImportEnd != null) {
invokeEventListener(document.onImportEnd);
}
}
} else {
// Finished operation normally, commit now
commit();
if (document.onImportEnd != null) {
invokeEventListener(document.onImportEnd);
}
}
statusMessages.remove(TIME_ELAPSED);

View File

@ -0,0 +1,36 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.handler.dataimport;
/**
* Event listener for DataImportHandler
*
* <b>This API is experimental and subject to change</b>
*
* @version $Id$
* @since solr 1.4
*/
public abstract class EventListener {
/**
* Event callback
*
* @param ctx the Context in which this event was called
*/
public abstract void onEvent(Context ctx);
}

View File

@ -78,6 +78,8 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTest {
super.runFullImport(dataConfigWithCaseInsensitiveFields);
assertQ(req("id:1"), "//*[@numFound='1']");
assertTrue("Start event listener was not called", StartEventListener.executed);
assertTrue("End event listener was not called", EndEventListener.executed);
}
@Test
@ -137,6 +139,22 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTest {
}
public static class StartEventListener extends EventListener {
public static boolean executed = false;
public void onEvent(Context ctx) {
executed = true;
}
}
public static class EndEventListener extends EventListener {
public static boolean executed = false;
public void onEvent(Context ctx) {
executed = true;
}
}
private final String requestParamAsVariable = "<dataConfig>\n" +
" <dataSource type=\"MockDataSource\" />\n" +
" <document>\n" +
@ -158,7 +176,7 @@ public class TestDocBuilder2 extends AbstractDataImportHandlerTest {
"</dataConfig>";
private final String dataConfigWithCaseInsensitiveFields = "<dataConfig>\n" +
" <document>\n" +
" <document onImportStart=\"TestDocBuilder2$StartEventListener\" onImportEnd=\"TestDocBuilder2$EndEventListener\">\n" +
" <entity name=\"books\" query=\"select * from x\">\n" +
" <field column=\"ID\" />\n" +
" <field column=\"Desc\" />\n" +