Skip to content

Commit

Permalink
HSEARCH-2632 Use only one job-level listener
Browse files Browse the repository at this point in the history
The execution order of multiple job-level listeners are not sure, so
it's preferable to use only one listener.
  • Loading branch information
mincong-h authored and Sanne committed Oct 25, 2017
1 parent d0a0dca commit aa61a06
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 76 deletions.

This file was deleted.

Expand Up @@ -12,13 +12,20 @@
import javax.inject.Inject;
import javax.inject.Named;

import org.hibernate.search.exception.SearchException;
import org.hibernate.search.jsr352.context.jpa.EntityManagerFactoryRegistry;
import org.hibernate.search.jsr352.inject.scope.HibernateSearchJobScoped;
import org.hibernate.search.jsr352.logging.impl.Log;
import org.hibernate.search.jsr352.massindexing.MassIndexingJobParameters;
import org.hibernate.search.jsr352.massindexing.impl.util.JobContextUtil;
import org.hibernate.search.util.logging.impl.LoggerFactory;

import static org.hibernate.search.jsr352.massindexing.impl.util.ValidationUtil.validateCheckpointInterval;
import static org.hibernate.search.jsr352.massindexing.impl.util.ValidationUtil.validatePositive;

/**
* Listener before the start of the job. It aims to setup the job context data, shared by all the steps.
* Listener before the start of the job. It aims to validate all the job
* parameters and setup the job context data, shared by all the steps.
*
* @author Mincong Huang
*/
Expand All @@ -32,6 +39,8 @@
@HibernateSearchJobScoped
public class JobContextSetupListener extends AbstractJobListener {

private static final Log log = LoggerFactory.make( Log.class );

@Inject
private JobContext jobContext;

Expand All @@ -51,14 +60,46 @@ public class JobContextSetupListener extends AbstractJobListener {
@BatchProperty(name = MassIndexingJobParameters.CUSTOM_QUERY_CRITERIA)
private String serializedCustomQueryCriteria;

@Inject
@BatchProperty(name = MassIndexingJobParameters.CHECKPOINT_INTERVAL)
private String serializedCheckpointInterval;

@Inject
@BatchProperty(name = MassIndexingJobParameters.ROWS_PER_PARTITION)
private String serializedRowsPerPartition;

@Inject
private EntityManagerFactoryRegistry emfRegistry;

@Override
public void beforeJob() throws Exception {
validateParameters();
JobContextUtil.getOrCreateData( jobContext,
emfRegistry, entityManagerFactoryScope, entityManagerFactoryReference,
entityTypes, serializedCustomQueryCriteria );
}

/**
* Validates job parameters.
*
* @throws SearchException if any validation fails.
*/
private void validateParameters() throws SearchException {
int checkpointInterval = parseInt( MassIndexingJobParameters.CHECKPOINT_INTERVAL, serializedCheckpointInterval );
int rowsPerPartition = parseInt( MassIndexingJobParameters.ROWS_PER_PARTITION, serializedRowsPerPartition );

validatePositive( MassIndexingJobParameters.CHECKPOINT_INTERVAL, checkpointInterval );
validatePositive( MassIndexingJobParameters.ROWS_PER_PARTITION, rowsPerPartition );
validateCheckpointInterval( checkpointInterval, rowsPerPartition );
}

private int parseInt(String parameterName, String parameterValue) {
try {
return Integer.parseInt( parameterValue );
}
catch (NumberFormatException e) {
throw log.unableToParseJobParameter( parameterName, parameterValue, e );
}
}

}
Expand Up @@ -9,18 +9,14 @@
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/jobXML_1_0.xsd" version="1.0">

<listeners>
<listener ref="org.hibernate.search.jsr352.massindexing.JobParameterValidationListener">
<properties>
<property name="checkpointInterval" value="#{jobParameters['checkpointInterval']}?:200;" />
<property name="rowsPerPartition" value="#{jobParameters['rowsPerPartition']}?:250;" />
</properties>
</listener>
<listener ref="org.hibernate.search.jsr352.massindexing.impl.JobContextSetupListener">
<properties>
<property name="entityManagerFactoryScope" value="#{jobParameters['entityManagerFactoryScope']}" />
<property name="entityManagerFactoryReference" value="#{jobParameters['entityManagerFactoryReference']}" />
<property name="entityTypes" value="#{jobParameters['entityTypes']}" />
<property name="customQueryCriteria" value="#{jobParameters['customQueryCriteria']}" />
<property name="checkpointInterval" value="#{jobParameters['checkpointInterval']}?:200;" />
<property name="rowsPerPartition" value="#{jobParameters['rowsPerPartition']}?:250;" />
</properties>
</listener>
</listeners>
Expand Down

0 comments on commit aa61a06

Please sign in to comment.