Search in sources :

Example 1 with RedisStore

use of org.springframework.data.redis.support.collections.RedisStore 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 2 with RedisStore

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

the class RedisStoreMessageSource method receive.

/**
 * Returns a Message with the view into a {@link RedisStore} identified
 * by {@link #keyExpression}
 */
@Override
@SuppressWarnings("unchecked")
public Message<RedisStore> receive() {
    String key = this.keyExpression.getValue(this.evaluationContext, String.class);
    Assert.hasText(key, "Failed to determine the key for the collection");
    RedisStore store = this.createStoreView(key);
    Object holder = TransactionSynchronizationManager.getResource(this);
    if (holder != null) {
        Assert.isInstanceOf(IntegrationResourceHolder.class, holder);
        ((IntegrationResourceHolder) holder).addAttribute("store", store);
    }
    if (store instanceof Collection<?> && ((Collection<Object>) store).size() < 1) {
        return null;
    } else {
        return this.getMessageBuilderFactory().withPayload(store).build();
    }
}
Also used : IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) RedisStore(org.springframework.data.redis.support.collections.RedisStore) Collection(java.util.Collection)

Aggregations

RedisStore (org.springframework.data.redis.support.collections.RedisStore)2 Collection (java.util.Collection)1 RedisMap (org.springframework.data.redis.support.collections.RedisMap)1 RedisSet (org.springframework.data.redis.support.collections.RedisSet)1 IntegrationResourceHolder (org.springframework.integration.transaction.IntegrationResourceHolder)1 MessageHandlingException (org.springframework.messaging.MessageHandlingException)1