Spring Redis
Since Camel 2.11
Both producer and consumer are supported
This component allows sending and receiving messages from Redis. Redis is advanced key-value store where keys can contain strings, hashes, lists, sets and sorted sets. In addition it provides pub/sub functionality for inter-app communications. Camel provides a producer for executing commands, consumer for subscribing to pub/sub messages an idempotent repository for filtering out duplicate messages.
Prerequisites In order to use this component, you must have a Redis server running. |
URI Format
spring-redis://host:port[?options]
You can append query options to the URI in the following format,
?options=value&option2=value&…
URI Options
The Spring Redis component supports 4 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
redisTemplate (common) |
Autowired Reference to a pre-configured RedisTemplate instance to use. |
RedisTemplate |
|
bridgeErrorHandler (consumer) |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
lazyStartProducer (producer) |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
autowiredEnabled (advanced) |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
boolean |
The Spring Redis endpoint is configured using URI syntax:
spring-redis:host:port
with the following path and query parameters:
Path Parameters (2 parameters):
Name | Description | Default | Type |
---|---|---|---|
host |
Required The host where Redis server is running. |
String |
|
port |
Required Redis server port number |
Integer |
Query Parameters (10 parameters):
Name | Description | Default | Type |
---|---|---|---|
channels (common) |
List of topic names or name patterns to subscribe to. Multiple names can be separated by comma. |
String |
|
command (common) |
Default command, which can be overridden by message header. Notice the consumer only supports the following commands: PSUBSCRIBE and SUBSCRIBE. There are 129 enums and the value can be one of: PING, SET, GET, QUIT, EXISTS, DEL, TYPE, FLUSHDB, KEYS, RANDOMKEY, RENAME, RENAMENX, RENAMEX, DBSIZE, EXPIRE, EXPIREAT, TTL, SELECT, MOVE, FLUSHALL, GETSET, MGET, SETNX, SETEX, MSET, MSETNX, DECRBY, DECR, INCRBY, INCR, APPEND, SUBSTR, HSET, HGET, HSETNX, HMSET, HMGET, HINCRBY, HEXISTS, HDEL, HLEN, HKEYS, HVALS, HGETALL, RPUSH, LPUSH, LLEN, LRANGE, LTRIM, LINDEX, LSET, LREM, LPOP, RPOP, RPOPLPUSH, SADD, SMEMBERS, SREM, SPOP, SMOVE, SCARD, SISMEMBER, SINTER, SINTERSTORE, SUNION, SUNIONSTORE, SDIFF, SDIFFSTORE, SRANDMEMBER, ZADD, ZRANGE, ZREM, ZINCRBY, ZRANK, ZREVRANK, ZREVRANGE, ZCARD, ZSCORE, MULTI, DISCARD, EXEC, WATCH, UNWATCH, SORT, BLPOP, BRPOP, AUTH, SUBSCRIBE, PUBLISH, UNSUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, ZCOUNT, ZRANGEBYSCORE, ZREVRANGEBYSCORE, ZREMRANGEBYRANK, ZREMRANGEBYSCORE, ZUNIONSTORE, ZINTERSTORE, SAVE, BGSAVE, BGREWRITEAOF, LASTSAVE, SHUTDOWN, INFO, MONITOR, SLAVEOF, CONFIG, STRLEN, SYNC, LPUSHX, PERSIST, RPUSHX, ECHO, LINSERT, DEBUG, BRPOPLPUSH, SETBIT, GETBIT, SETRANGE, GETRANGE, PEXPIRE, PEXPIREAT, GEOADD, GEODIST, GEOHASH, GEOPOS, GEORADIUS, GEORADIUSBYMEMBER |
SET |
Command |
connectionFactory (common) |
Reference to a pre-configured RedisConnectionFactory instance to use. |
RedisConnectionFactory |
|
redisTemplate (common) |
Reference to a pre-configured RedisTemplate instance to use. |
RedisTemplate |
|
serializer (common) |
Reference to a pre-configured RedisSerializer instance to use. |
RedisSerializer |
|
bridgeErrorHandler (consumer) |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
exceptionHandler (consumer) |
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
ExceptionHandler |
|
exchangePattern (consumer) |
Sets the exchange pattern when the consumer creates an exchange. There are 3 enums and the value can be one of: InOnly, InOut, InOptionalOut |
ExchangePattern |
|
listenerContainer (consumer) |
Reference to a pre-configured RedisMessageListenerContainer instance to use. |
RedisMessageListenerContainer |
|
lazyStartProducer (producer) |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
Usage
See also the unit tests available at https://github.com/apache/camel/tree/main/components/camel-spring-redis/src/test/java/org/apache/camel/component/redis.
Message headers evaluated by the Redis producer
The producer issues commands to the server and each command has different set of parameters with specific types. The result from the command execution is returned in the message body.
Hash Commands | Description | Parameters | Result |
---|---|---|---|
|
Set the string value of a hash field |
CamelRedis.Key (String), CamelRedis.Field (String), CamelRedis.Value (Object) |
void |
|
Get the value of a hash field |
CamelRedis.Key (String), CamelRedis.Field (String) |
String |
|
Set the value of a hash field, only if the field does not exist |
CamelRedis.Key (String), CamelRedis.Field (String), CamelRedis.Value (Object) |
void |
|
Set multiple hash fields to multiple values |
CamelRedis.Key (String), CamelRedis.Values(Map<String, Object>) |
void |
|
Get the values of all the given hash fields |
CamelRedis.Key (String), CamelRedis.Fields (Collection<String>) |
Collection<Object> |
|
Increment the integer value of a hash field by the given number |
CamelRedis.Key (String), CamelRedis.Field (String), CamelRedis.Value (Long) |
Long |
|
Determine if a hash field exists |
CamelRedis.Key (String), CamelRedis.Field (String) |
Boolean |
|
Delete one or more hash fields |
CamelRedis.Key (String), CamelRedis.Field (String) |
void |
|
Get the number of fields in a hash |
CamelRedis.Key (String) |
Long |
|
Get all the fields in a hash |
CamelRedis.Key (String) |
Set<String> |
|
Get all the values in a hash |
CamelRedis.Key (String) |
Collection<Object> |
|
Get all the fields and values in a hash |
CamelRedis.Key (String) |
Map<String, Object> |
List Commands | Description | Parameters | Result |
---|---|---|---|
|
Append one or multiple values to a list |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Long |
|
Append a value to a list, only if the list exists |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Long |
|
Prepend one or multiple values to a list |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Long |
|
Get the length of a list |
CamelRedis.Key (String) |
Long |
|
Get a range of elements from a list |
CamelRedis.Key (String), CamelRedis.Start (Long), CamelRedis.End (Long) |
List<Object> |
|
Trim a list to the specified range |
CamelRedis.Key (String), CamelRedis.Start (Long), CamelRedis.End (Long) |
void |
|
Get an element from a list by its index |
CamelRedis.Key (String), CamelRedis.Index (Long) |
String |
|
Insert an element before or after another element in a list |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Pivot (String), CamelRedis.Position (String) |
Long |
|
Set the value of an element in a list by its index |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Index (Long) |
void |
|
Remove elements from a list |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Count (Long) |
Long |
|
Remove and get the first element in a list |
CamelRedis.Key (String) |
Object |
|
Remove and get the last element in a list |
CamelRedis.Key (String) |
String |
|
Remove the last element in a list, append it to another list and return it |
CamelRedis.Key (String), CamelRedis.Destination (String) |
Object |
|
Pop a value from a list, push it to another list and return it; or block until one is available |
CamelRedis.Key (String), CamelRedis.Destination (String), CamelRedis.Timeout (Long) |
Object |
|
Remove and get the first element in a list, or block until one is available |
CamelRedis.Key (String), CamelRedis.Timeout (Long) |
Object |
|
Remove and get the last element in a list, or block until one is available |
CamelRedis.Key (String), CamelRedis.Timeout (Long) |
String |
Set Commands | Description | Parameters | Result |
---|---|---|---|
|
Add one or more members to a set |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Boolean |
|
Get all the members in a set |
CamelRedis.Key (String) |
Set<Object> |
|
Remove one or more members from a set |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Boolean |
|
Remove and return a random member from a set |
CamelRedis.Key (String) |
String |
|
Move a member from one set to another |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Destination (String) |
Boolean |
|
Get the number of members in a set |
CamelRedis.Key (String) |
Long |
|
Determine if a given value is a member of a set |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Boolean |
|
Intersect multiple sets |
CamelRedis.Key (String), CamelRedis.Keys (String) |
Set<Object> |
|
Intersect multiple sets and store the resulting set in a key |
CamelRedis.Key (String), CamelRedis.Keys (String), CamelRedis.Destination (String) |
void |
|
Add multiple sets |
CamelRedis.Key (String), CamelRedis.Keys (String) |
Set<Object> |
|
Add multiple sets and store the resulting set in a key |
CamelRedis.Key (String), CamelRedis.Keys (String), CamelRedis.Destination (String) |
void |
|
Subtract multiple sets |
CamelRedis.Key (String), CamelRedis.Keys (String) |
Set<Object> |
|
Subtract multiple sets and store the resulting set in a key |
CamelRedis.Key (String), CamelRedis.Keys (String), CamelRedis.Destination (String) |
void |
|
Get one or multiple random members from a set |
CamelRedis.Key (String) |
String |
Ordered set Commands | Description | Parameters | Result |
---|---|---|---|
|
Add one or more members to a sorted set, or update its score if it already exists |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Score (Double) |
Boolean |
|
Return a range of members in a sorted set, by index |
CamelRedis.Key (String), CamelRedis.Start (Long), CamelRedis.End (Long), CamelRedis.WithScore (Boolean) |
Object |
|
Remove one or more members from a sorted set |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Boolean |
|
Increment the score of a member in a sorted set |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Increment (Double) |
Double |
|
Determine the index of a member in a sorted set |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Long |
|
Determine the index of a member in a sorted set, with scores ordered from high to low |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Long |
|
Return a range of members in a sorted set, by index, with scores ordered from high to low |
CamelRedis.Key (String), CamelRedis.Start (Long), CamelRedis.End (Long), CamelRedis.WithScore (Boolean) |
Object |
|
Get the number of members in a sorted set |
CamelRedis.Key (String) |
Long |
|
Count the members in a sorted set with scores within the given values |
CamelRedis.Key (String), CamelRedis.Min (Double), CamelRedis.Max (Double) |
Long |
|
Return a range of members in a sorted set, by score |
CamelRedis.Key (String), CamelRedis.Min (Double), CamelRedis.Max (Double) |
Set<Object> |
|
Return a range of members in a sorted set, by score, with scores ordered from high to low |
CamelRedis.Key (String), CamelRedis.Min (Double), CamelRedis.Max (Double) |
Set<Object> |
|
Remove all members in a sorted set within the given indexes |
CamelRedis.Key (String), CamelRedis.Start (Long), CamelRedis.End (Long) |
void |
|
Remove all members in a sorted set within the given scores |
CamelRedis.Key (String), CamelRedis.Start (Long), CamelRedis.End (Long) |
void |
|
Add multiple sorted sets and store the resulting sorted set in a new key |
CamelRedis.Key (String), CamelRedis.Keys (String), CamelRedis.Destination (String) |
void |
|
Intersect multiple sorted sets and store the resulting sorted set in a new key |
CamelRedis.Key (String), CamelRedis.Keys (String), CamelRedis.Destination (String) |
void |
String Commands | Description | Parameters | Result |
---|---|---|---|
|
Set the string value of a key |
CamelRedis.Key (String), CamelRedis.Value (Object) |
void |
|
Get the value of a key |
CamelRedis.Key (String) |
Object |
|
Get the length of the value stored in a key |
CamelRedis.Key (String) |
Long |
|
Append a value to a key |
CamelRedis.Key (String), CamelRedis.Value (String) |
Integer |
|
Sets or clears the bit at offset in the string value stored at key |
CamelRedis.Key (String), CamelRedis.Offset (Long), CamelRedis.Value (Boolean) |
void |
|
Returns the bit value at offset in the string value stored at key |
CamelRedis.Key (String), CamelRedis.Offset (Long) |
Boolean |
|
Overwrite part of a string at key starting at the specified offset |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Offset (Long) |
void |
|
Get a substring of the string stored at a key |
CamelRedis.Key (String), CamelRedis.Start (Long), CamelRedis.End (Long) |
String |
|
Set the value of a key, only if the key does not exist |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Boolean |
|
Set the value and expiration of a key |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Timeout (Long), SECONDS |
void |
|
Decrement the integer value of a key by the given number |
CamelRedis.Key (String), CamelRedis.Value (Long) |
Long |
|
Decrement the integer value of a key by one |
CamelRedis.Key (String), |
Long |
|
Increment the integer value of a key by the given amount |
CamelRedis.Key (String), CamelRedis.Value (Long) |
Long |
|
Increment the integer value of a key by one |
CamelRedis.Key (String) |
Long |
|
Get the values of all the given keys |
CamelRedis.Fields (Collection<String>) |
List<Object> |
|
Set multiple keys to multiple values |
CamelRedis.Values(Map<String, Object>) |
void |
|
Set multiple keys to multiple values, only if none of the keys exist |
CamelRedis.Key (String), CamelRedis.Value (Object) |
void |
|
Set the string value of a key and return its old value |
CamelRedis.Key (String), CamelRedis.Value (Object) |
Object |
Key Commands | Description | Parameters | Result |
---|---|---|---|
|
Determine if a key exists |
CamelRedis.Key (String) |
Boolean |
|
Delete a key |
CamelRedis.Keys (String) |
void |
|
Determine the type stored at key |
CamelRedis.Key (String) |
DataType |
|
Find all keys matching the given pattern |
CamelRedis.Pattern (String) |
Collection<String> |
|
Return a random key from the keyspace |
CamelRedis.Pattern (String), CamelRedis.Value (String) |
String |
|
Rename a key |
CamelRedis.Key (String) |
void |
|
Rename a key, only if the new key does not exist |
CamelRedis.Key (String), CamelRedis.Value (String) |
Boolean |
|
Set a key’s time to live in seconds |
CamelRedis.Key (String), CamelRedis.Timeout (Long) |
Boolean |
|
Sort the elements in a list, set or sorted set |
CamelRedis.Key (String) |
List<Object> |
|
Remove the expiration from a key |
CamelRedis.Key (String) |
Boolean |
|
Set the expiration for a key as a UNIX timestamp |
CamelRedis.Key (String), CamelRedis.Timestamp (Long) |
Boolean |
|
Set a key’s time to live in milliseconds |
CamelRedis.Key (String), CamelRedis.Timeout (Long) |
Boolean |
|
Set the expiration for a key as a UNIX timestamp specified in milliseconds |
CamelRedis.Key (String), CamelRedis.Timestamp (Long) |
Boolean |
|
Get the time to live for a key |
CamelRedis.Key (String) |
Long |
|
Move a key to another database |
CamelRedis.Key (String), CamelRedis.Db (Integer) |
Boolean |
Geo Commands | Description | Parameters | Result |
---|---|---|---|
|
Adds the specified geospatial items (latitude, longitude, name) to the specified key |
CamelRedis.Key (String), CamelRedis.Latitude (Double), CamelRedis.Longitude (Double), CamelRedis.Value (Object) |
Long |
|
Return the distance between two members in the geospatial index for the specified key |
CamelRedis.Key (String), CamelRedis.Values (Object[]) |
Distance |
|
Return valid Geohash strings representing the position of an element in the geospatial index for the specified key |
CamelRedis.Key (String), CamelRedis.Value (Object) |
List<String> |
|
Return the positions (longitude,latitude) of an element in the geospatial index for the specified key |
CamelRedis.Key (String), CamelRedis.Value (Object) |
List<Point> |
|
Return the element in the geospatial index for the specified key which are within the borders of the area specified with the center location and the maximum distance from the center (the radius) |
CamelRedis.Key (String), CamelRedis.Latitude (Double), CamelRedis.Longitude (Double), CamelRedis.Radius (Double), CamelRedis.Count (Integer) |
GeoResults |
|
This command is exactly like GEORADIUS with the sole difference that instead of taking, as the center of the area to query, a longitude and latitude value, it takes the name of a member already existing inside the geospatial index for the specified key |
CamelRedis.Key (String), CamelRedis.Value (Object), CamelRedis.Radius (Double), CamelRedis.Count (Integer) |
GeoResults |
Other Command | Description | Parameters | Result |
---|---|---|---|
|
Mark the start of a transaction block |
none |
void |
|
Discard all commands issued after MULTI |
none |
void |
|
Execute all commands issued after MULTI |
none |
void |
|
Watch the given keys to determine execution of the MULTI/EXEC block |
CamelRedis.Keys (String) |
void |
|
Forget about all watched keys |
none |
void |
|
Echo the given string |
CamelRedis.Value (String) |
String |
|
Ping the server |
none |
String |
|
Close the connection |
none |
void |
|
Post a message to a channel |
CamelRedis.Channel (String), CamelRedis.Message (Object) |
void |
Dependencies
Maven users will need to add the following dependency to their pom.xml.
pom.xml
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-redis</artifactId>
<version>${camel-version}</version>
</dependency>
where ${camel-version
} must be replaced by the actual version of Camel.
Spring Boot Auto-Configuration
When using spring-redis with Spring Boot make sure to use the following Maven dependency to have support for auto configuration:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-redis-starter</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency>
The component supports 5 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
camel.component.spring-redis.autowired-enabled |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
Boolean |
camel.component.spring-redis.bridge-error-handler |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
Boolean |
camel.component.spring-redis.enabled |
Whether to enable auto configuration of the spring-redis component. This is enabled by default. |
Boolean |
|
camel.component.spring-redis.lazy-start-producer |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
Boolean |
camel.component.spring-redis.redis-template |
Reference to a pre-configured RedisTemplate instance to use. The option is a org.springframework.data.redis.core.RedisTemplate type. |
RedisTemplate |