Half-hearted attempt to fix broken integration tests
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
package org.springframework.batch.integration.chunk;
|
||||
|
||||
|
||||
public interface ChunkHandler<T> {
|
||||
|
||||
ChunkResponse handleChunk(ChunkRequest<? extends T> chunk);
|
||||
ChunkResponse handleChunk(ChunkRequest<T> chunk);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.chunk;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public interface ChunkProcessor<S> {
|
||||
|
||||
// This is transactional with REQUIRES_NEW because we need to force rollback
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
int process(Collection<? extends S> items, int skipCount) throws Exception;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.springframework.batch.integration.chunk;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.integration.annotation.Handler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class ChunkProcessorChunkHandler<S> implements ChunkHandler<S>, InitializingBean {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ChunkProcessorChunkHandler.class);
|
||||
|
||||
private ChunkProcessor<S> chunkProcessor;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(chunkProcessor, "A ChunkProcessor must be provided");
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link ChunkProcessor}.
|
||||
* @param chunkProcessor the chunkProcessor to set
|
||||
*/
|
||||
public void setChunkProcessor(ChunkProcessor<S> chunkProcessor) {
|
||||
this.chunkProcessor = chunkProcessor;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.integration.batch.slave.ChunkHandler#handleChunk(java.util.Collection)
|
||||
*/
|
||||
@Handler
|
||||
public ChunkResponse handleChunk(ChunkRequest<S> chunkRequest) {
|
||||
|
||||
logger.debug("Handling chunk: " + chunkRequest);
|
||||
|
||||
int skipCount = 0;
|
||||
try {
|
||||
skipCount = chunkProcessor.process(chunkRequest.getItems(), chunkRequest.getSkipCount());
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.debug("Failed chunk", e);
|
||||
return new ChunkResponse(ExitStatus.FAILED.addExitDescription(e.getClass().getName() + ": "
|
||||
+ e.getMessage()), chunkRequest.getJobId(), skipCount);
|
||||
}
|
||||
|
||||
logger.debug("Completed chunk handling with " + skipCount + " skips");
|
||||
return new ChunkResponse(ExitStatus.CONTINUABLE, chunkRequest.getJobId(), skipCount);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package org.springframework.batch.integration.chunk;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.core.SkipListener;
|
||||
import org.springframework.batch.core.listener.CompositeSkipListener;
|
||||
import org.springframework.batch.core.step.skip.ItemSkipPolicy;
|
||||
import org.springframework.batch.core.step.skip.NeverSkipItemSkipPolicy;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.integration.annotation.Handler;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public class ItemWriterChunkHandler<T> implements ChunkHandler<T> {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ItemWriterChunkHandler.class);
|
||||
|
||||
private ItemWriter<? super T> itemWriter;
|
||||
|
||||
private ItemSkipPolicy itemSkipPolicy = new NeverSkipItemSkipPolicy();
|
||||
|
||||
private CompositeSkipListener skipListener = new CompositeSkipListener();
|
||||
|
||||
public void setItemSkipPolicy(ItemSkipPolicy itemSkipPolicy) {
|
||||
this.itemSkipPolicy = itemSkipPolicy;
|
||||
}
|
||||
|
||||
public void setItemWriter(ItemWriter<? super T> itemWriter) {
|
||||
this.itemWriter = itemWriter;
|
||||
}
|
||||
|
||||
public void registerSkipListener(SkipListener listener) {
|
||||
skipListener.register(listener);
|
||||
}
|
||||
|
||||
public void setSkipListeners(SkipListener[] skipListeners) {
|
||||
for (SkipListener listener : skipListeners) {
|
||||
registerSkipListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.integration.batch.slave.ChunkHandler#handleChunk(java.util.Collection)
|
||||
*/
|
||||
@Handler
|
||||
@Transactional
|
||||
public ChunkResponse handleChunk(ChunkRequest<? extends T> chunk) {
|
||||
|
||||
logger.debug("Handling chunk: " + chunk);
|
||||
|
||||
int parentSkipCount = chunk.getSkipCount();
|
||||
int skipCount = 0;
|
||||
|
||||
try {
|
||||
for (T item : chunk.getItems()) {
|
||||
try {
|
||||
itemWriter.write(Collections.singletonList(item));
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (itemSkipPolicy.shouldSkip(e, parentSkipCount + skipCount)) {
|
||||
logger.debug("Skipping item on exception", e);
|
||||
skipCount++;
|
||||
skipListener.onSkipInWrite(item, e);
|
||||
} else {
|
||||
logger.debug("Cannot skip, re-throwing");
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.debug("Failed chunk", e);
|
||||
// TODO: need to force rollback as well
|
||||
return new ChunkResponse(ExitStatus.FAILED.addExitDescription(e.getClass().getName() + ": "
|
||||
+ e.getMessage()), chunk.getJobId(), skipCount);
|
||||
}
|
||||
|
||||
logger.debug("Completed chunk handling with " + skipCount + " skips");
|
||||
return new ChunkResponse(ExitStatus.CONTINUABLE, chunk.getJobId(), skipCount);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* 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.chunk;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class SimpleChunkProcessor<S, T> implements ChunkProcessor<S>, InitializingBean {
|
||||
|
||||
private ItemProcessor<? super S, ? extends T> itemProcessor;
|
||||
|
||||
private ItemWriter<? super T> itemWriter;
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
Assert.notNull(itemProcessor, "An ItemProcessor must be provided");
|
||||
Assert.notNull(itemWriter, "An ItemWriter must be provided");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param itemWriter
|
||||
*/
|
||||
public void setItemWriter(ItemWriter<? super T> itemWriter) {
|
||||
this.itemWriter = itemWriter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public setter for the {@link ItemProcessor}.
|
||||
* @param itemProcessor the {@link ItemProcessor} to set
|
||||
*/
|
||||
public void setItemProcessor(ItemProcessor<? super S, ? extends T> itemProcessor) {
|
||||
this.itemProcessor = itemProcessor;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.batch.integration.chunk.ChunkProcessor#process(java.util.Collection,
|
||||
* int)
|
||||
*/
|
||||
public int process(Collection<? extends S> items, int parentSkipCount) throws Exception {
|
||||
|
||||
List<T> processed = new ArrayList<T>();
|
||||
for (S item : items) {
|
||||
processed.add(itemProcessor.process(item));
|
||||
}
|
||||
itemWriter.write(processed);
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.springframework.batch.integration.chunk;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class ChunkProcessorChunkHandlerTests {
|
||||
|
||||
private ChunkProcessorChunkHandler<Object> handler = new ChunkProcessorChunkHandler<Object>();
|
||||
|
||||
protected int count = 0;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testVanillaHandleChunk() {
|
||||
handler.setChunkProcessor(new ChunkProcessor<Object>() {
|
||||
public int process(Collection<? extends Object> items, int skipCount) throws Exception {
|
||||
count+=items.size();
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
ChunkResponse response = handler.handleChunk(new ChunkRequest(StringUtils.commaDelimitedListToSet("foo,bar"),
|
||||
12L, 10));
|
||||
assertEquals(0, response.getSkipCount());
|
||||
assertEquals(new Long(12L), response.getJobId());
|
||||
assertEquals(ExitStatus.CONTINUABLE, response.getExitStatus());
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
package org.springframework.batch.integration.chunk;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.SkipListener;
|
||||
import org.springframework.batch.core.listener.SkipListenerSupport;
|
||||
import org.springframework.batch.core.step.skip.AlwaysSkipItemSkipPolicy;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
public class ItemWriterChunkHandlerTests {
|
||||
|
||||
private ItemWriterChunkHandler<Object> handler = new ItemWriterChunkHandler<Object>();
|
||||
|
||||
protected int count = 0;
|
||||
|
||||
private SkipListenerSupport listener = new SkipListenerSupport() {
|
||||
@Override
|
||||
public void onSkipInWrite(Object item, Throwable t) {
|
||||
count++;
|
||||
}
|
||||
};
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testVanillaHandleChunk() {
|
||||
handler.setItemWriter(new ItemWriter<Object>() {
|
||||
public void write(List<? extends Object> items) throws Exception {
|
||||
count+=items.size();
|
||||
}
|
||||
});
|
||||
ChunkResponse response = handler.handleChunk(new ChunkRequest(StringUtils.commaDelimitedListToSet("foo,bar"),
|
||||
12L, 10));
|
||||
assertEquals(0, response.getSkipCount());
|
||||
assertEquals(new Long(12L), response.getJobId());
|
||||
assertEquals(ExitStatus.CONTINUABLE, response.getExitStatus());
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testSetItemSkipPolicy() {
|
||||
handler.setItemWriter(new ItemWriter<Object>() {
|
||||
public void write(List<? extends Object> items) throws Exception {
|
||||
count+=items.size();
|
||||
throw new RuntimeException("Planned failure");
|
||||
}
|
||||
});
|
||||
handler.setItemSkipPolicy(new AlwaysSkipItemSkipPolicy());
|
||||
ChunkResponse response = handler.handleChunk(new ChunkRequest(StringUtils.commaDelimitedListToSet("foo,bar"),
|
||||
12L, 10));
|
||||
assertEquals(2, response.getSkipCount());
|
||||
assertEquals(new Long(12L), response.getJobId());
|
||||
assertEquals(ExitStatus.CONTINUABLE, response.getExitStatus());
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testRegisterSkipListener() {
|
||||
handler.setItemWriter(new ItemWriter<Object>() {
|
||||
public void write(List<? extends Object> items) throws Exception {
|
||||
count+=items.size();
|
||||
throw new RuntimeException("Planned failure");
|
||||
}
|
||||
});
|
||||
handler.setItemSkipPolicy(new AlwaysSkipItemSkipPolicy());
|
||||
handler.registerSkipListener(listener);
|
||||
ChunkResponse response = handler.handleChunk(new ChunkRequest(StringUtils.commaDelimitedListToSet("foo,bar"),
|
||||
12L, 10));
|
||||
assertEquals(2, response.getSkipCount());
|
||||
assertEquals(4, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetSkipListeners() {
|
||||
handler.setSkipListeners(new SkipListener[] { listener });
|
||||
testRegisterSkipListener();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,8 +10,10 @@
|
||||
http://www.springframework.org/schema/context/spring-context-2.5.xsd
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
<message-bus auto-create-channels="true" />
|
||||
<message-bus/>
|
||||
<annotation-driven />
|
||||
<direct-channel id="smokein"/>
|
||||
<channel id="smokeout"/>
|
||||
<channel id="smokein"/>
|
||||
<channel id="smokeout">
|
||||
<queue capacity="UNBOUNDED"/>
|
||||
</channel>
|
||||
</beans:beans>
|
||||
@@ -8,7 +8,7 @@
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
|
||||
<message-bus auto-create-channels="true" />
|
||||
<message-bus/>
|
||||
<channel id="jobs" />
|
||||
|
||||
</beans:beans>
|
||||
@@ -11,7 +11,7 @@
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
|
||||
<message-bus auto-create-channels="true" />
|
||||
<message-bus/>
|
||||
|
||||
<annotation-driven />
|
||||
|
||||
@@ -22,9 +22,16 @@
|
||||
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
|
||||
<tx:annotation-driven />
|
||||
|
||||
<beans:bean id="chunkHandler" class="org.springframework.batch.integration.chunk.ItemWriterChunkHandler">
|
||||
<beans:property name="itemWriter">
|
||||
<beans:bean class="org.springframework.batch.integration.chunk.TestItemWriter"></beans:bean>
|
||||
<beans:bean id="chunkHandler" class="org.springframework.batch.integration.chunk.ChunkProcessorChunkHandler">
|
||||
<beans:property name="chunkProcessor">
|
||||
<beans:bean class="org.springframework.batch.integration.chunk.SimpleChunkProcessor">
|
||||
<beans:property name="itemWriter">
|
||||
<beans:bean class="org.springframework.batch.integration.chunk.TestItemWriter" />
|
||||
</beans:property>
|
||||
<beans:property name="itemProcessor">
|
||||
<beans:bean class="org.springframework.batch.item.support.PassthroughItemProcessor" />
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
</beans:property>
|
||||
</beans:bean>
|
||||
|
||||
|
||||
@@ -11,8 +11,10 @@
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
<integration:message-bus auto-create-channels="true" />
|
||||
<integration:message-bus/>
|
||||
<integration:annotation-driven/>
|
||||
<integration:channel id="resources" />
|
||||
<integration:channel id="requests" />
|
||||
<integration:channel id="requests">
|
||||
<integration:queue capacity="UNBOUNDED"/>
|
||||
</integration:channel>
|
||||
</beans>
|
||||
@@ -11,7 +11,7 @@
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
<integration:message-bus auto-create-channels="true" />
|
||||
<integration:message-bus/>
|
||||
<integration:channel id="requests" />
|
||||
<bean id="itemWriter" class="org.springframework.batch.integration.item.MessageChannelItemWriter">
|
||||
<property name="channel" ref="requests"/>
|
||||
|
||||
@@ -15,14 +15,15 @@
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
<import resource="classpath:/simple-job-launcher-context.xml" />
|
||||
<integration:annotation-driven/>
|
||||
<integration:message-bus auto-create-channels="true" />
|
||||
<integration:message-bus/>
|
||||
<integration:channel id="requests" />
|
||||
<integration:channel id="replies" />
|
||||
<integration:channel id="replies">
|
||||
<integration:queue capacity="UNBOUNDED"/>
|
||||
</integration:channel>
|
||||
<bean id="job" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<bean
|
||||
<bean id="stepController"
|
||||
class="org.springframework.batch.integration.job.MessageOrientedStep">
|
||||
<property name="name" value="TODO: I shouldn't have to set this"/>
|
||||
<property name="target" ref="requests" />
|
||||
<property name="source" ref="replies" />
|
||||
<property name="jobRepository" ref="jobRepository" />
|
||||
|
||||
@@ -14,13 +14,15 @@
|
||||
|
||||
<import resource="classpath:simple-job-launcher-context.xml" />
|
||||
|
||||
<integration:message-bus auto-create-channels="true" />
|
||||
<integration:message-bus/>
|
||||
<integration:annotation-driven />
|
||||
<integration:direct-channel id="requests" />
|
||||
<integration:direct-channel id="jobs" />
|
||||
<integration:thread-local-channel id="response" />
|
||||
<integration:channel id="requests" />
|
||||
<integration:channel id="jobs" />
|
||||
<integration:channel id="response">
|
||||
<integration:queue capacity="UNBOUNDED"/>
|
||||
</integration:channel>
|
||||
|
||||
<integration:service-activator input-channel="requests" ref="jobLaunchingHandler" return-address-overrides="true" />
|
||||
<integration:service-activator input-channel="requests" ref="jobLaunchingHandler"/>
|
||||
<integration:service-activator input-channel="none" output-channel="jobs" ref="jobRequestConverter" />
|
||||
|
||||
<bean id="jobRequestConverter" class="org.springframework.batch.integration.launch.JobRequestConverter" />
|
||||
|
||||
Reference in New Issue
Block a user