BATCHADM-81: add step context manipulation for async processors

- AsyncItemProcessor creates step context for its runnable
 - We also provide StepContextInterceptor for supplying StepExecution to downsteam consumers
This commit is contained in:
Dave Syer
2010-11-27 14:32:53 +00:00
committed by Michael Minella
parent fa3b9f86d3
commit 5b646c4edf
8 changed files with 374 additions and 26 deletions

View File

@@ -1,9 +1,27 @@
/*
* 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.integration.async;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.task.SyncTaskExecutor;
@@ -65,13 +83,36 @@ public class AsyncItemProcessor<I, O> implements ItemProcessor<I, Future<O>>, In
* @see ItemProcessor#process(Object)
*/
public Future<O> process(final I item) throws Exception {
final StepExecution stepExecution = getStepExecution();
FutureTask<O> task = new FutureTask<O>(new Callable<O>() {
public O call() throws Exception {
return delegate.process(item);
if (stepExecution != null) {
StepSynchronizationManager.register(stepExecution);
}
try {
return delegate.process(item);
}
finally {
if (stepExecution != null) {
StepSynchronizationManager.close();
}
}
}
});
taskExecutor.execute(task);
return task;
}
/**
* @return the current step execution if there is one
*/
private StepExecution getStepExecution() {
StepContext context = StepSynchronizationManager.getContext();
if (context==null) {
return null;
}
StepExecution stepExecution = context.getStepExecution();
return stepExecution;
}
}

View File

@@ -1,3 +1,18 @@
/*
* 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.integration.async;
import java.util.ArrayList;

View File

@@ -0,0 +1,53 @@
/*
* 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.integration.async;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.channel.ChannelInterceptor;
import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter;
import org.springframework.integration.support.MessageBuilder;
/**
* A {@link ChannelInterceptor} that adds the current {@link StepExecution} (if
* there is one) as a header to the message. Downstream asynchronous handlers
* can then take advantage of the step context without needing to be step
* scoped, which is a problem for handlers executing in another thread because
* the scope context is not available.
*
* @author Dave Syer
*
*/
public class StepExecutionInterceptor extends ChannelInterceptorAdapter {
/**
* The name of the header
*/
public static final String STEP_EXECUTION = "stepExecution";
@Override
public Message<?> preSend(Message<?> message, MessageChannel channel) {
StepContext context = StepSynchronizationManager.getContext();
if (context == null) {
return message;
}
return MessageBuilder.fromMessage(message).setHeader(STEP_EXECUTION, context.getStepExecution()).build();
}
}