OPEN - issue BATCH-378: RepeatListener is confusing and too generic to use for 'intercepting' a step

http://jira.springframework.org/browse/BATCH-378

Add JobListener to SimpleJob.  Change StepListener method names.
This commit is contained in:
dsyer
2008-02-28 16:56:36 +00:00
parent e9f0b41eed
commit 04f641d321
12 changed files with 305 additions and 28 deletions

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2006-2008 the original author or authors.
*
* Licensed 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.springframework.batch.core.domain;
/**
* @author Dave Syer
*
*/
public interface JobListener {
/**
* Initialise the state of the listener with the {@link JobExecution} from
* the current scope.
* @param jobExecution
*/
void beforeJob(JobExecution jobExecution);
/**
*
*/
void afterJob();
}

View File

@@ -29,7 +29,7 @@ public interface StepListener {
* the current scope.
* @param stepExecution
*/
void open(StepExecution stepExecution);
void beforeStep(StepExecution stepExecution);
/**
* The value returned will be combined with the normal exit status using
@@ -38,7 +38,7 @@ public interface StepListener {
* @param e an exception thrown by the step execution
* @return an exit status to be combined with the normal one, or null
*/
ExitStatus onError(Throwable e);
ExitStatus onErrorInStep(Throwable e);
/**
* Give a listener a chance to modify the exit status from a step. The value
@@ -48,5 +48,5 @@ public interface StepListener {
* @return an {@link ExitStatus} to combine with the normal value. Return
* null to leave the old value unchanged.
*/
ExitStatus close();
ExitStatus afterStep();
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed 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.springframework.batch.core.interceptor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobListener;
import org.springframework.batch.core.domain.StepListener;
/**
* @author Dave Syer
*
*/
public class CompositeJobListener implements JobListener {
private List listeners = new ArrayList();
/**
* Public setter for the listeners.
*
* @param listeners
*/
public void setListeners(JobListener[] listeners) {
this.listeners = Arrays.asList(listeners);
}
/**
* Public setter for the listeners. The result will be as if
* {@link #setListeners(StepListener[])} was called with an array of length
* one.
*
* @param listener
*/
public void setListener(JobListener listener) {
setListeners(new JobListener[] {listener});
}
/**
* Register additional listener.
*
* @param stepListener
*/
public void register(JobListener stepListener) {
if (!listeners.contains(stepListener)) {
listeners.add(stepListener);
}
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepListener#close()
*/
public void afterJob() {
for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
JobListener listener = (JobListener) iterator.next();
listener.afterJob();
}
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepListener#open(org.springframework.batch.core.domain.JobParameters)
*/
public void beforeJob(JobExecution jobExecution) {
for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
JobListener listener = (JobListener) iterator.next();
listener.beforeJob(jobExecution);
}
}
}

View File

@@ -66,11 +66,11 @@ public class CompositeStepListener implements StepListener {
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepListener#close()
*/
public ExitStatus close() {
public ExitStatus afterStep() {
ExitStatus status = null;
for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
StepListener listener = (StepListener) iterator.next();
ExitStatus close = listener.close();
ExitStatus close = listener.afterStep();
status = status!=null ? status.and(close): close;
}
return status;
@@ -79,21 +79,21 @@ public class CompositeStepListener implements StepListener {
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepListener#open(org.springframework.batch.core.domain.JobParameters)
*/
public void open(StepExecution stepExecution) {
public void beforeStep(StepExecution stepExecution) {
for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
StepListener listener = (StepListener) iterator.next();
listener.open(stepExecution);
listener.beforeStep(stepExecution);
}
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepListener#onError(java.lang.Throwable)
*/
public ExitStatus onError(Throwable e) {
public ExitStatus onErrorInStep(Throwable e) {
ExitStatus status = null;
for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
StepListener listener = (StepListener) iterator.next();
ExitStatus close = listener.onError(e);
ExitStatus close = listener.onErrorInStep(e);
status = status!=null ? status.and(close): close;
}
return status;

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed 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.springframework.batch.core.interceptor;
import org.springframework.batch.core.domain.JobExecution;
import org.springframework.batch.core.domain.JobListener;
/**
* @author Dave Syer
*
*/
public class JobListenerSupport implements JobListener {
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.JobListener#afterJob()
*/
public void afterJob() {
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.JobListener#beforeJob(org.springframework.batch.core.domain.JobExecution)
*/
public void beforeJob(JobExecution jobExecution) {
}
}

View File

@@ -28,20 +28,20 @@ public class StepListenerSupport implements StepListener {
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepListener#close()
*/
public ExitStatus close() {
public ExitStatus afterStep() {
return null;
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepListener#open(org.springframework.batch.item.ExecutionContext)
*/
public void open(StepExecution stepExecution) {
public void beforeStep(StepExecution stepExecution) {
}
/* (non-Javadoc)
* @see org.springframework.batch.core.domain.StepListener#onError(java.lang.Throwable)
*/
public ExitStatus onError(Throwable e) {
public ExitStatus onErrorInStep(Throwable e) {
return null;
}
}