diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/SynchronizedItemStreamWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/SynchronizedItemStreamWriter.java index 3bd40a187..f9d4cd991 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/SynchronizedItemStreamWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/SynchronizedItemStreamWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2020 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. @@ -25,9 +25,24 @@ import java.util.List; /** * An {@link ItemStreamWriter} decorator with a synchronized - * {@link SynchronizedItemStreamWriter#write write()} method + * {@link SynchronizedItemStreamWriter#write write()} method. + * + * This decorator is useful when using a non thread-safe item writer + * in a multi-threaded step. Typical delegate examples are the + * {@link org.springframework.batch.item.json.JsonFileItemWriter JsonFileItemWriter} + * and {@link org.springframework.batch.item.xml.StaxEventItemWriter StaxEventItemWriter}. + * + *

+ * It should be noted that synchronizing writes might introduce + * some performance degradation, so this decorator should be used + * wisely and only when necessary. For example, using a + * {@link org.springframework.batch.item.file.FlatFileItemWriter FlatFileItemWriter} in + * a multi-threaded step does NOT require synchronizing writes, so using + * this decorator in such use case might be counter-productive. + *

* * @author Dimitrios Liapis + * @author Mahmoud Ben Hassine * * @param type of object being written */ @@ -35,12 +50,17 @@ public class SynchronizedItemStreamWriter implements ItemStreamWriter, Ini private ItemStreamWriter delegate; + /** + * Set the delegate {@link ItemStreamWriter}. + * + * @param delegate the delegate to set + */ public void setDelegate(ItemStreamWriter delegate) { this.delegate = delegate; } /** - * This delegates to the write method of the delegate + * This method delegates to the {@code write} method of the {@code delegate}. */ @Override public synchronized void write(List items) throws Exception { diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilder.java index c2d5fe819..18e08dba7 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2020 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. @@ -23,19 +23,31 @@ import org.springframework.util.Assert; * Creates a fully qualified {@link SynchronizedItemStreamWriter}. * * @author Dimitrios Liapis + * @author Mahmoud Ben Hassine */ public class SynchronizedItemStreamWriterBuilder { private ItemStreamWriter delegate; + /** + * Set the delegate {@link ItemStreamWriter}. + * + * @param delegate the delegate to set + * @return this instance for method chaining + */ public SynchronizedItemStreamWriterBuilder delegate(ItemStreamWriter delegate) { this.delegate = delegate; return this; } + /** + * Returns a fully constructed {@link SynchronizedItemStreamWriter}. + * + * @return a new {@link SynchronizedItemStreamWriter} + */ public SynchronizedItemStreamWriter build() { - Assert.notNull(this.delegate, "A delegate is required"); + Assert.notNull(this.delegate, "A delegate item writer is required"); SynchronizedItemStreamWriter writer = new SynchronizedItemStreamWriter<>(); writer.setDelegate(this.delegate); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractSynchronizedItemStreamWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractSynchronizedItemStreamWriterTests.java index c1574964a..7953de936 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractSynchronizedItemStreamWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/AbstractSynchronizedItemStreamWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2020 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. diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/SynchronizedItemStreamWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/SynchronizedItemStreamWriterTests.java index cdece0c6a..dca30c1c0 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/SynchronizedItemStreamWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/SynchronizedItemStreamWriterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2020 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. diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilderTests.java index b3d98eae1..1be4699dd 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/support/builder/SynchronizedItemStreamWriterBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 the original author or authors. + * Copyright 2020 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. @@ -37,7 +37,7 @@ public class SynchronizedItemStreamWriterBuilderTests extends AbstractSynchroniz @Test public void testBuilderDelegateIsNotNull() { expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("A delegate is required"); + expectedException.expectMessage("A delegate item writer is required"); new SynchronizedItemStreamWriterBuilder<>().build(); } }