mirror of https://github.com/apache/lucene.git
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:
parent
d92209b51b
commit
0bae610c14
|
@ -45,7 +45,10 @@ New Features
|
|||
(David Smiley, Glen Newton, shalin)
|
||||
|
||||
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)
|
||||
(Ahmed Hammad via shalin)
|
||||
|
||||
10.SOLR-938: Add event listener API for import start and end.
|
||||
(Kay Kay, Noble Paul via shalin)
|
||||
|
||||
Optimizations
|
||||
----------------------
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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" +
|
||||
|
|
Loading…
Reference in New Issue