forked from spring-projects/spring-batch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...infrastructure/src/main/java/org/springframework/batch/item/database/RedisItemWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2019-2021 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.item.database; | ||
|
||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.batch.item.KeyValueItemWriter; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* <p> | ||
* An {@link ItemWriter} implementation for Redis using a | ||
* {@link RedisTemplate} . | ||
* </p> | ||
* | ||
* @author Santiago Molano | ||
* @since 4.2 | ||
*/ | ||
public class RedisItemWriter<K, T> extends KeyValueItemWriter<K, T> { | ||
private RedisTemplate<K, T> redisTemplate; | ||
|
||
|
||
@Override | ||
protected void writeKeyValue(K key, T value) { | ||
|
||
redisTemplate.opsForValue().set(key, value); | ||
} | ||
|
||
@Override | ||
protected void init() { | ||
Assert.notNull(redisTemplate, "RedisTemplate must not be null"); | ||
} | ||
|
||
public void setRedisTemplate(RedisTemplate<K, T> redisTemplate) { | ||
this.redisTemplate = redisTemplate; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...structure/src/test/java/org/springframework/batch/item/database/RedisItemWriterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright 2019-2021 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.item.database; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.core.ValueOperations; | ||
|
||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class RedisItemWriterTests { | ||
@Mock | ||
private RedisTemplate<String, String> redisTemplate; | ||
@Mock | ||
private ValueOperations<String, String> valueOperations; | ||
private RedisItemWriter<String, String> redisItemWriter = new RedisItemWriter<>(); | ||
private RedisItemKeyMapper redisItemKeyMapper; | ||
|
||
@Before | ||
public void setup() { | ||
MockitoAnnotations.openMocks(this); | ||
redisItemWriter.setRedisTemplate(this.redisTemplate); | ||
when(redisTemplate.opsForValue()).thenReturn(valueOperations); | ||
doNothing().when(valueOperations).set(any(), any()); | ||
this.redisItemKeyMapper = new RedisItemKeyMapper(); | ||
this.redisItemWriter.setItemKeyMapper(redisItemKeyMapper); | ||
|
||
} | ||
|
||
@Test | ||
public void shouldWriteToRedisDatabaseUsingKeyValue() { | ||
this.redisItemWriter.writeKeyValue("oneKey", "oneValue"); | ||
verify(this.redisTemplate.opsForValue()).set("oneKey", "oneValue"); | ||
} | ||
@Test | ||
public void shouldWriteAllItemsToRedis() throws Exception { | ||
List<String> items = Arrays.asList("val1", "val2"); | ||
this.redisItemWriter.write(items); | ||
verify(this.redisTemplate.opsForValue()).set(items.get(0), items.get(0)); | ||
verify(this.redisTemplate.opsForValue()).set(items.get(1), items.get(1)); | ||
} | ||
static class RedisItemKeyMapper implements Converter<String, String> { | ||
|
||
@Override | ||
public String convert(String source) { | ||
return source; | ||
} | ||
} | ||
} |