Search in sources :

Example 1 with RedisMap

use of org.springframework.data.redis.support.collections.RedisMap in project spring-integration by spring-projects.

the class RedisStoreOutboundChannelAdapterIntegrationTests method testMapToMapNoKey.

// key is not provided
@Test(expected = MessageHandlingException.class)
@RedisAvailable
public void testMapToMapNoKey() {
    RedisTemplate<String, Map<String, Map<String, String>>> redisTemplate = new RedisTemplate<String, Map<String, Map<String, String>>>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setConnectionFactory(getConnectionFactoryForTest());
    redisTemplate.afterPropertiesSet();
    RedisMap<String, Map<String, String>> redisMap = new DefaultRedisMap<String, Map<String, String>>("pepboys", redisTemplate);
    assertEquals(0, redisMap.size());
    Map<String, String> pepboys = new HashMap<String, String>();
    pepboys.put("1", "Manny");
    pepboys.put("2", "Moe");
    pepboys.put("3", "Jack");
    Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).build();
    this.mapToMapBChannel.send(message);
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) HashMap(java.util.HashMap) DefaultRedisMap(org.springframework.data.redis.support.collections.DefaultRedisMap) HashMap(java.util.HashMap) Map(java.util.Map) RedisMap(org.springframework.data.redis.support.collections.RedisMap) DefaultRedisMap(org.springframework.data.redis.support.collections.DefaultRedisMap) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 2 with RedisMap

use of org.springframework.data.redis.support.collections.RedisMap in project spring-integration by spring-projects.

the class RedisStoreWritingMessageHandler method handleMessageInternal.

/**
 * Will extract the payload from the Message and store it in the collection identified by the
 * key (which may be determined by an expression). The type of collection is specified by the
 * {@link #collectionType} property. The default CollectionType is LIST.
 * <p>
 * The rules for storing the payload are:
 * <p>
 * <b>LIST/SET</b>
 * If the payload is of type Collection and {@link #extractPayloadElements} is 'true' (default),
 * the payload will be added using the addAll() method. If {@link #extractPayloadElements}
 * is set to 'false', then regardless of the payload type, the payload will be added using add().
 * <p>
 * <b>ZSET</b>
 * In addition to the rules described for LIST/SET, ZSET allows 'score' information
 * to be provided. The score can be provided using the {@link RedisHeaders#ZSET_SCORE} message header
 * when the payload is not a Map, or by sending a Map as the payload where each Map 'key' is a
 * value to be saved and each corresponding Map 'value' is the score assigned to it.
 * If {@link #extractPayloadElements} is set to 'false' the map will be stored as a single entry.
 * If the 'score' can not be determined, the default value (1) will be used.
 * <p>
 * <b>MAP/PROPERTIES</b>
 * You can also add items to a Map or Properties based store.
 * If the payload itself is of type Map or Properties, it can be stored either as a batch or single
 * item following the same rules as described above for other collection types.
 * If the payload itself needs to be stored as a value of the map/property then the map key
 * must be specified via the mapKeyExpression (default {@link RedisHeaders#MAP_KEY} Message header).
 */
@SuppressWarnings("unchecked")
@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
    String key = this.keyExpression.getValue(this.evaluationContext, message, String.class);
    Assert.hasText(key, () -> "Failed to determine a key for the Redis store based on the message: " + message);
    RedisStore store = this.createStoreView(key);
    Assert.state(this.initialized, "handler not initialized - afterPropertiesSet() must be called before the first use");
    try {
        if (this.collectionType == CollectionType.ZSET) {
            writeToZset((RedisZSet<Object>) store, message);
        } else if (this.collectionType == CollectionType.SET) {
            writeToSet((RedisSet<Object>) store, message);
        } else if (this.collectionType == CollectionType.LIST) {
            writeToList((RedisList<Object>) store, message);
        } else if (this.collectionType == CollectionType.MAP) {
            writeToMap((RedisMap<Object, Object>) store, message);
        } else if (this.collectionType == CollectionType.PROPERTIES) {
            writeToProperties((RedisProperties) store, message);
        }
    } catch (Exception e) {
        throw new MessageHandlingException(message, "Failed to store Message data in Redis collection", e);
    }
}
Also used : RedisSet(org.springframework.data.redis.support.collections.RedisSet) RedisMap(org.springframework.data.redis.support.collections.RedisMap) RedisStore(org.springframework.data.redis.support.collections.RedisStore) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 3 with RedisMap

use of org.springframework.data.redis.support.collections.RedisMap in project spring-integration by spring-projects.

the class RedisStoreOutboundChannelAdapterIntegrationTests method testMapToMapAsSingleEntryWithKeyAsHeader.

@Test
@RedisAvailable
public void testMapToMapAsSingleEntryWithKeyAsHeader() {
    RedisTemplate<String, Map<String, Map<String, String>>> redisTemplate = new RedisTemplate<String, Map<String, Map<String, String>>>();
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setConnectionFactory(getConnectionFactoryForTest());
    redisTemplate.afterPropertiesSet();
    RedisMap<String, Map<String, String>> redisMap = new DefaultRedisMap<String, Map<String, String>>("pepboys", redisTemplate);
    assertEquals(0, redisMap.size());
    Map<String, String> pepboys = new HashMap<String, String>();
    pepboys.put("1", "Manny");
    pepboys.put("2", "Moe");
    pepboys.put("3", "Jack");
    Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).setHeader(RedisHeaders.KEY, "pepboys").setHeader(RedisHeaders.MAP_KEY, "foo").build();
    this.mapToMapBChannel.send(message);
    Map<String, String> pepboyz = redisMap.get("foo");
    assertEquals("Manny", pepboyz.get("1"));
    assertEquals("Moe", pepboyz.get("2"));
    assertEquals("Jack", pepboyz.get("3"));
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) HashMap(java.util.HashMap) DefaultRedisMap(org.springframework.data.redis.support.collections.DefaultRedisMap) HashMap(java.util.HashMap) Map(java.util.Map) RedisMap(org.springframework.data.redis.support.collections.RedisMap) DefaultRedisMap(org.springframework.data.redis.support.collections.DefaultRedisMap) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

RedisMap (org.springframework.data.redis.support.collections.RedisMap)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.Test)2 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)2 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)2 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)2 DefaultRedisMap (org.springframework.data.redis.support.collections.DefaultRedisMap)2 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)2 RedisSet (org.springframework.data.redis.support.collections.RedisSet)1 RedisStore (org.springframework.data.redis.support.collections.RedisStore)1 MessageHandlingException (org.springframework.messaging.MessageHandlingException)1