Skip to content

Commit

Permalink
Add ItemWriter for Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
santfirax authored and fmbenhassine committed Sep 12, 2023
1 parent 70e8a1c commit 1bd0e0c
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<spring-data-commons.version>3.2.0-SNAPSHOT</spring-data-commons.version>
<spring-data-jpa.version>3.2.0-SNAPSHOT</spring-data-jpa.version>
<spring-data-mongodb.version>4.2.0-SNAPSHOT</spring-data-mongodb.version>
<spring-data-redis.version>2.5.2-SNAPSHOT</spring-data-redis.version>
<spring-kafka.version>3.0.11-SNAPSHOT</spring-kafka.version>
<spring-amqp.version>3.0.9-SNAPSHOT</spring-amqp.version>
<spring-ldap.version>3.2.0-SNAPSHOT</spring-ldap.version>
Expand Down
6 changes: 6 additions & 0 deletions spring-batch-infrastructure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring-data-redis.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
Expand Down
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;
}
}
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;
}
}
}

0 comments on commit 1bd0e0c

Please sign in to comment.