Add new module for Apache Geode support
Related to https://github.com/spring-projects/spring-batch/issues/4214
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2002-2022 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
|
||||
*
|
||||
* https://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.extensions.geode;
|
||||
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.item.KeyValueItemWriter;
|
||||
import org.springframework.data.gemfire.GemfireOperations;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* An {@link ItemWriter} that stores items in GemFire
|
||||
*
|
||||
* @author David Turanski
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public class GemfireItemWriter<K, V> extends KeyValueItemWriter<K, V> {
|
||||
|
||||
private GemfireOperations gemfireTemplate;
|
||||
|
||||
/**
|
||||
* @param gemfireTemplate the {@link GemfireTemplate} to set
|
||||
*/
|
||||
public void setTemplate(GemfireTemplate gemfireTemplate) {
|
||||
this.gemfireTemplate = gemfireTemplate;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.batch.item.KeyValueItemWriter#writeKeyValue(java.lang.Object,
|
||||
* java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
protected void writeKeyValue(K key, V value) {
|
||||
if (delete) {
|
||||
gemfireTemplate.remove(key);
|
||||
}
|
||||
else {
|
||||
gemfireTemplate.put(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.batch.item.KeyValueItemWriter#init()
|
||||
*/
|
||||
@Override
|
||||
protected void init() {
|
||||
Assert.notNull(gemfireTemplate, "A GemfireTemplate is required.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2002-2022 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
|
||||
*
|
||||
* https://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.extensions.geode;
|
||||
|
||||
import org.springframework.batch.item.SpELItemKeyMapper;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A convenient {@link GemfireItemWriter} implementation that uses a
|
||||
* {@link SpELItemKeyMapper}
|
||||
*
|
||||
* @author David Turanski
|
||||
* @since 2.2
|
||||
*/
|
||||
public class SpELMappingGemfireItemWriter<K, V> extends GemfireItemWriter<K, V> {
|
||||
|
||||
/**
|
||||
* A constructor that accepts a SpEL expression used to derive the key
|
||||
* @param keyExpression
|
||||
*/
|
||||
SpELMappingGemfireItemWriter(String keyExpression) {
|
||||
super();
|
||||
Assert.hasText(keyExpression, "a valid keyExpression is required.");
|
||||
setItemKeyMapper(new SpELItemKeyMapper<>(keyExpression));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright 2017-2022 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
|
||||
*
|
||||
* https://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.extensions.geode.builder;
|
||||
|
||||
import org.springframework.batch.extensions.geode.GemfireItemWriter;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A builder implementation for the {@link GemfireItemWriter}
|
||||
*
|
||||
* @author Glenn Renfro
|
||||
* @since 4.0
|
||||
* @see GemfireItemWriterBuilder
|
||||
*/
|
||||
public class GemfireItemWriterBuilder<K, V> {
|
||||
|
||||
private GemfireTemplate template;
|
||||
|
||||
private Converter<V, K> itemKeyMapper;
|
||||
|
||||
private boolean delete;
|
||||
|
||||
/**
|
||||
* Establishes the GemfireTemplate the writer should use.
|
||||
* @param template the {@link GemfireTemplate} to set.
|
||||
* @return The current instance of the builder.
|
||||
* @see GemfireItemWriter#setTemplate(GemfireTemplate)
|
||||
*/
|
||||
public GemfireItemWriterBuilder<K, V> template(GemfireTemplate template) {
|
||||
this.template = template;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link Converter} to use to derive the key from the item.
|
||||
* @param itemKeyMapper the Converter to use.
|
||||
* @return The current instance of the builder.
|
||||
* @see GemfireItemWriter#setItemKeyMapper(Converter)
|
||||
*/
|
||||
public GemfireItemWriterBuilder<K, V> itemKeyMapper(Converter<V, K> itemKeyMapper) {
|
||||
this.itemKeyMapper = itemKeyMapper;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates if the items being passed to the writer are to be saved or removed from
|
||||
* the data store. If set to false (default), the items will be saved. If set to true,
|
||||
* the items will be removed.
|
||||
* @param delete removal indicator.
|
||||
* @return The current instance of the builder.
|
||||
* @see GemfireItemWriter#setDelete(boolean)
|
||||
*/
|
||||
public GemfireItemWriterBuilder<K, V> delete(boolean delete) {
|
||||
this.delete = delete;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and builds a {@link GemfireItemWriter}.
|
||||
* @return a {@link GemfireItemWriter}
|
||||
*/
|
||||
public GemfireItemWriter<K, V> build() {
|
||||
Assert.notNull(this.template, "template is required.");
|
||||
Assert.notNull(this.itemKeyMapper, "itemKeyMapper is required.");
|
||||
|
||||
GemfireItemWriter<K, V> writer = new GemfireItemWriter<>();
|
||||
writer.setTemplate(this.template);
|
||||
writer.setItemKeyMapper(this.itemKeyMapper);
|
||||
writer.setDelete(this.delete);
|
||||
return writer;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright 2013-2022 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
|
||||
*
|
||||
* https://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.extensions.geode;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.batch.item.SpELItemKeyMapper;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class GemfireItemWriterTests {
|
||||
|
||||
private GemfireItemWriter<String, Foo> writer;
|
||||
|
||||
@Mock
|
||||
private GemfireTemplate template;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
writer = new GemfireItemWriter<>();
|
||||
writer.setTemplate(template);
|
||||
writer.setItemKeyMapper(new SpELItemKeyMapper<>("bar.val"));
|
||||
writer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAfterPropertiesSet() throws Exception {
|
||||
writer = new GemfireItemWriter<>();
|
||||
assertThrows(IllegalArgumentException.class, writer::afterPropertiesSet);
|
||||
|
||||
writer.setTemplate(template);
|
||||
assertThrows(IllegalArgumentException.class, writer::afterPropertiesSet);
|
||||
|
||||
writer.setItemKeyMapper(new SpELItemKeyMapper<>("foo"));
|
||||
writer.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicWrite() throws Exception {
|
||||
List<Foo> chunk = new ArrayList<>();
|
||||
chunk.add(new Foo(new Bar("val1")));
|
||||
chunk.add(new Foo(new Bar("val2")));
|
||||
writer.write(chunk);
|
||||
|
||||
verify(template).put("val1", chunk.get(0));
|
||||
verify(template).put("val2", chunk.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicDelete() throws Exception {
|
||||
List<Foo> chunk = new ArrayList<>();
|
||||
chunk.add(new Foo(new Bar("val1")));
|
||||
chunk.add(new Foo(new Bar("val2")));
|
||||
writer.setDelete(true);
|
||||
writer.write(chunk);
|
||||
|
||||
verify(template).remove("val1");
|
||||
verify(template).remove("val2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWriteWithCustomItemKeyMapper() throws Exception {
|
||||
List<Foo> chunk = new ArrayList<>();
|
||||
chunk.add(new Foo(new Bar("val1")));
|
||||
chunk.add(new Foo(new Bar("val2")));
|
||||
writer = new GemfireItemWriter<>();
|
||||
writer.setTemplate(template);
|
||||
writer.setItemKeyMapper(item -> {
|
||||
String index = item.bar.val.replaceAll("val", "");
|
||||
return "item" + index;
|
||||
});
|
||||
writer.afterPropertiesSet();
|
||||
writer.write(chunk);
|
||||
|
||||
verify(template).put("item1", chunk.get(0));
|
||||
verify(template).put("item2", chunk.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWriteNoTransactionNoItems() throws Exception {
|
||||
writer.write(null);
|
||||
verifyNoInteractions(template);
|
||||
}
|
||||
|
||||
static class Foo {
|
||||
|
||||
public Bar bar;
|
||||
|
||||
public Foo(Bar bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class Bar {
|
||||
|
||||
public String val;
|
||||
|
||||
public Bar(String b1) {
|
||||
this.val = b1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright 2017-2022 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
|
||||
*
|
||||
* https://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.extensions.geode.builder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.batch.item.SpELItemKeyMapper;
|
||||
import org.springframework.batch.extensions.geode.GemfireItemWriter;
|
||||
import org.springframework.data.gemfire.GemfireTemplate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* @author Glenn Renfro
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class GemfireItemWriterBuilderTests {
|
||||
|
||||
@Mock
|
||||
private GemfireTemplate template;
|
||||
|
||||
private SpELItemKeyMapper<String, GemfireItemWriterBuilderTests.Foo> itemKeyMapper;
|
||||
|
||||
private List<Foo> items;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
this.items = new ArrayList<>();
|
||||
this.items.add(new GemfireItemWriterBuilderTests.Foo(new GemfireItemWriterBuilderTests.Bar("val1")));
|
||||
this.items.add(new GemfireItemWriterBuilderTests.Foo(new GemfireItemWriterBuilderTests.Bar("val2")));
|
||||
this.itemKeyMapper = new SpELItemKeyMapper<>("bar.val");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicWrite() throws Exception {
|
||||
GemfireItemWriter<String, GemfireItemWriterBuilderTests.Foo> writer = new GemfireItemWriterBuilder<String, GemfireItemWriterBuilderTests.Foo>()
|
||||
.template(this.template).itemKeyMapper(this.itemKeyMapper).build();
|
||||
|
||||
writer.write(this.items);
|
||||
|
||||
verify(this.template).put("val1", items.get(0));
|
||||
verify(this.template).put("val2", items.get(1));
|
||||
verify(this.template, never()).remove("val1");
|
||||
verify(this.template, never()).remove("val2");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBasicDelete() throws Exception {
|
||||
GemfireItemWriter<String, GemfireItemWriterBuilderTests.Foo> writer = new GemfireItemWriterBuilder<String, GemfireItemWriterBuilderTests.Foo>()
|
||||
.template(this.template).delete(true).itemKeyMapper(this.itemKeyMapper).build();
|
||||
|
||||
writer.write(this.items);
|
||||
|
||||
verify(this.template).remove("val1");
|
||||
verify(this.template).remove("val2");
|
||||
verify(this.template, never()).put("val1", items.get(0));
|
||||
verify(this.template, never()).put("val2", items.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNullTemplate() {
|
||||
try {
|
||||
new GemfireItemWriterBuilder<String, GemfireItemWriterBuilderTests.Foo>()
|
||||
.itemKeyMapper(this.itemKeyMapper);
|
||||
} catch (Exception exception) {
|
||||
assertEquals("template is required.", exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNullItemKeyMapper() {
|
||||
try {
|
||||
new GemfireItemWriterBuilder<String, GemfireItemWriterBuilderTests.Foo>().template(this.template);
|
||||
} catch (Exception exception) {
|
||||
assertEquals("template is required.", exception.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
static class Foo {
|
||||
|
||||
public GemfireItemWriterBuilderTests.Bar bar;
|
||||
|
||||
public Foo(GemfireItemWriterBuilderTests.Bar bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class Bar {
|
||||
|
||||
public String val;
|
||||
|
||||
public Bar(String b1) {
|
||||
this.val = b1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user