public class

RedisModule

extends Object
implements MuleContextAware PartitionableObjectStore<T extends Serializable>
java.lang.Object
   ↳ org.mule.module.redis.RedisModule
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

Redis is an open-source, networked, in-memory, persistent, journaled, key-value data store. Provides Redis connectivity to Mule:

  • Supports Redis Publish/Subscribe model for asynchronous message exchanges,
  • Allows direct reading and writing operations in Redis collections,
  • Allows using Redis as a ObjectStore for Mule components that require persistence.

Summary

Nested Classes
enum RedisModule.ListPushSide  
enum RedisModule.SortedSetOrder  
Constants
String FALLBACK_PARTITION_NAME
Fields
private static final Log LOGGER
private int connectionTimeout Connection timeout in milliseconds.
private String defaultPartitionName The PartitionableObjectStore partition to use in case methods from ObjectStore are used.
private String host Redis host.
private JedisPool jedisPool
private MuleContext muleContext
private String password Redis password
private GenericObjectPool.Config poolConfig Object pool configuration.
private int port Redis port.
private int reconnectionFrequency Reconnection frequency in milliseconds.
private boolean running
Public Constructors
RedisModule()
Public Methods
byte[] addToSet(String key, boolean mustSucceed, String value, MuleEvent muleEvent)
Add the message payload to the set stored at the specified key.
byte[] addToSortedSet(String key, double score, boolean mustSucceed, String value, MuleEvent muleEvent)
Add the message payload with the desired score to the sorted set stored at the specified key.
List<Serializable> allKeys(String partitionName)
List<Serializable> allKeys()
List<String> allPartitions()
void close()
void close(String partitionName)
boolean contains(Serializable key, String partitionName)
boolean contains(Serializable key)
Long decrement(String key, long step)
Decrements the number stored at key by step.
void destroyJedis()
void disposePartition(String partitionName)
Boolean exists(String key)
Test if the specified key exists.
Boolean expire(String key, int seconds)
Set a timeout on the specified key.
Boolean expireAt(String key, long unixTime)
Set a timeout in the form of a UNIX timestamp (Number of seconds elapsed since 1 Jan 1970) on the specified key.
byte[] get(String key)
Get the value of the specified key.
int getConnectionTimeout()
String getDefaultPartitionName()
byte[] getFromHash(String key, String field)
Get the value stored at the specified field in the hash at the specified key.
String getHost()
JedisPool getJedisPool()
String getPassword()
GenericObjectPool.Config getPoolConfig()
int getPort()
Set<byte[]> getRangeByIndex(String key, int start, int end, RedisModule.SortedSetOrder order)
Retrieve a range of values from the sorted set stored at the specified key.
Set<byte[]> getRangeByScore(String key, double min, double max, RedisModule.SortedSetOrder order)
Retrieve a range of values from the sorted set stored at the specified key.
int getReconnectionFrequency()
Long getTtl(String key)
Get the remaining time to live in seconds of a volatile key.
Long increment(String key, long step)
Increments the number stored at key by step.
Long incrementHash(String key, String field, long step)
Increments the number stored at field in the hash stored at key by increment.
Double incrementSortedSet(String key, double step, String value, MuleEvent muleEvent)
Increments the score of member in the sorted set stored at key by increment.
void initializeJedis()
boolean isPersistent()
void open(String partitionName)
void open()
Boolean persist(String key)
Undo an expire or expireAt ; turning the volatile key into a normal key.
byte[] popFromList(String key, RedisModule.ListPushSide side)
Pop a value from the desired side of the list stored at the specified key.
byte[] popFromSet(String key)
Pops a random value from the set stored at the specified key.
byte[] publish(String channel, boolean mustSucceed, String message, MuleEvent muleEvent)
Publish the message payload to the specified channel.
byte[] pushToList(String key, RedisModule.ListPushSide side, boolean ifExists, String value, MuleEvent muleEvent)
Push the message payload to the desired side (LEFT or RIGHT) of the list stored at the specified key.
byte[] randomMemberFromSet(String key)
Reads a random value from the set stored at the specified key.
Serializable remove(Serializable key)
Serializable remove(Serializable key, String partitionName)
Serializable retrieve(Serializable key)
Serializable retrieve(Serializable key, String partitionName)
byte[] set(String key, Integer expire, boolean ifNotExists, String value, MuleEvent muleEvent)
Set key to hold the payload.
void setConnectionTimeout(int connectionTimeout)
void setDefaultPartitionName(String defaultPartitionName)
void setHost(String host)
byte[] setInHash(String key, String field, boolean ifNotExists, String value, MuleEvent muleEvent)
Set the specified hash field to the message payload.
void setMuleContext(MuleContext muleContext)
void setPassword(String password)
void setPoolConfig(GenericObjectPool.Config poolConfig)
void setPort(int port)
void setReconnectionFrequency(int reconnectionFrequency)
void store(Serializable key, Serializable value, String partitionName)
void store(Serializable key, Serializable value)
void subscribe(List<String> channels, SourceCallback callback)
Subscribe to the specified channels.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.mule.api.context.MuleContextAware
From interface org.mule.api.store.ListableObjectStore
From interface org.mule.api.store.ObjectStore
From interface org.mule.api.store.PartitionableObjectStore

Constants

private static final String FALLBACK_PARTITION_NAME

Constant Value: "_default"

Fields

private static final Log LOGGER

private int connectionTimeout

Connection timeout in milliseconds.

private String defaultPartitionName

The PartitionableObjectStore partition to use in case methods from ObjectStore are used.

private String host

Redis host.

private JedisPool jedisPool

private MuleContext muleContext

private String password

Redis password

private GenericObjectPool.Config poolConfig

Object pool configuration.

private int port

Redis port.

private int reconnectionFrequency

Reconnection frequency in milliseconds.

private boolean running

Public Constructors

public RedisModule ()

Public Methods

public byte[] addToSet (String key, boolean mustSucceed, String value, MuleEvent muleEvent)

Add the message payload to the set stored at the specified key. If key does not exist, a new key holding a set is created.

Parameters
key Key that will be used for SADD
mustSucceed If true, ensures that adding to the set was successful (ie no pre-existing identical value in the set)
value The value to set.
muleEvent The current MuleEvent.
Returns
  • If no new entry has been added to the set and mustSucceed is true, null is returned. Otherwise the message is returned.

public byte[] addToSortedSet (String key, double score, boolean mustSucceed, String value, MuleEvent muleEvent)

Add the message payload with the desired score to the sorted set stored at the specified key. If key does not exist, a new key holding a sorted set is created.

Parameters
key Key that will be used for ZADD
score Score to use for the value
mustSucceed If true, ensures that adding to the sorted set was successful (ie no pre-existing identical value in the set)
value The value to set.
muleEvent The current MuleEvent.
Returns
  • If no new entry has been added to the sorted set and mustSucceed is true, null is returned. Otherwise the message is returned.

public List<Serializable> allKeys (String partitionName)

Parameters
partitionName
Throws
ObjectStoreException

public List<Serializable> allKeys ()

Throws
ObjectStoreException

public List<String> allPartitions ()

Throws
ObjectStoreException

public void close ()

Throws
ObjectStoreException

public void close (String partitionName)

Parameters
partitionName
Throws
ObjectStoreException

public boolean contains (Serializable key, String partitionName)

Parameters
key
partitionName
Throws
ObjectStoreException

public boolean contains (Serializable key)

Parameters
key
Throws
ObjectStoreException

public Long decrement (String key, long step)

Decrements the number stored at key by step. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains data that can not be represented as integer.

Parameters
key Key that will be used for DECR.
step Step used for the increment.
Returns
  • A byte array with the content of the key

public void destroyJedis ()

public void disposePartition (String partitionName)

Parameters
partitionName
Throws
ObjectStoreException

public Boolean exists (String key)

Test if the specified key exists.

Parameters
key Key that will be used for EXISTS
Returns
  • A boolean that represents the existence of the key.

public Boolean expire (String key, int seconds)

Set a timeout on the specified key.

Parameters
key The key in the sorted set.
seconds The time to live in seconds.
Returns
  • true if EXPIRE was successful, false otherwise.

public Boolean expireAt (String key, long unixTime)

Set a timeout in the form of a UNIX timestamp (Number of seconds elapsed since 1 Jan 1970) on the specified key.

Parameters
key The key in the sorted set.
unixTime The UNIX timestamp in seconds.
Returns
  • true if EXPIREAT was successful, false otherwise.

public byte[] get (String key)

Get the value of the specified key. If the key does not exist null is returned.

Parameters
key Key that will be used for GET
Returns
  • A byte array with the content of the key

public int getConnectionTimeout ()

public String getDefaultPartitionName ()

public byte[] getFromHash (String key, String field)

Get the value stored at the specified field in the hash at the specified key. If the field or the hash don't exist, null is returned.

Parameters
key Key that will be used for HGET
field Field that will be used for HGET
Returns
  • The value or null.

public String getHost ()

public JedisPool getJedisPool ()

public String getPassword ()

public GenericObjectPool.Config getPoolConfig ()

public int getPort ()

public Set<byte[]> getRangeByIndex (String key, int start, int end, RedisModule.SortedSetOrder order)

Retrieve a range of values from the sorted set stored at the specified key. The range of values is defined by indices in the sorted set and sorted as desired.

Parameters
key Key that will be used for ZRANGE/ZREVRANGE
start Range start index
end Range end index
order Index order for sorting the range, either ASCENDING or DESCENDING
Returns
  • the values in the specified range in the desired order as Set

public Set<byte[]> getRangeByScore (String key, double min, double max, RedisModule.SortedSetOrder order)

Retrieve a range of values from the sorted set stored at the specified key. The range of values is defined by scores in the sorted set and sorted as desired.

Parameters
key Key that will be used for ZRANGEBYSCORE/ZREVRANGEBYSCORE
min Range start score
max Range end score
order Score order for sorting the range, either ASCENDING or DESCENDING
Returns
  • the values in the specified range in the desired order as Set

public int getReconnectionFrequency ()

public Long getTtl (String key)

Get the remaining time to live in seconds of a volatile key.

Parameters
key The key in the sorted set.
Returns
  • the remaining time to live in seconds, -2 when key does not exist or -1 when key does not have a timeout.

public Long increment (String key, long step)

Increments the number stored at key by step. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains data that can not be represented as integer.

Parameters
key Key that will be used for INCR.
step Step used for the increment.
Returns
  • the incremented number.

public Long incrementHash (String key, String field, long step)

Increments the number stored at field in the hash stored at key by increment. If key does not exist, a new key holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.

Parameters
key Key that will be used for HGET
field Field that will be used for HGET
step Step used for the increment.
Returns
  • the incremented number.

public Double incrementSortedSet (String key, double step, String value, MuleEvent muleEvent)

Increments the score of member in the sorted set stored at key by increment. If member does not exist in the sorted set, it is added with increment as its score (as if its previous score was 0.0). If key does not exist, a new sorted set with the specified member as its sole member is created.

Parameters
key The key in the sorted set.
step The step to use to increment the score.
value The value to set.
muleEvent The current MuleEvent.
Returns
  • the new score of the member.

public void initializeJedis ()

public boolean isPersistent ()

public void open (String partitionName)

Parameters
partitionName
Throws
ObjectStoreException

public void open ()

Throws
ObjectStoreException

public Boolean persist (String key)

Undo an expire or expireAt ; turning the volatile key into a normal key.

Parameters
key The key in the sorted set.
Returns
  • true if PERSIST was successful, false otherwise.

public byte[] popFromList (String key, RedisModule.ListPushSide side)

Pop a value from the desired side of the list stored at the specified key.

Parameters
key Key that will be used for LPOP/RPOP
side The side where to pop the value from, either LEFT or RIGHT
Returns
  • The popped value or null if either the list is empty or no list exists at the key

public byte[] popFromSet (String key)

Pops a random value from the set stored at the specified key.

Parameters
key Key that will be used for SPOP
Returns
  • The popped value or null if either the set is empty or no set exists at the key

public byte[] publish (String channel, boolean mustSucceed, String message, MuleEvent muleEvent)

Publish the message payload to the specified channel.

Parameters
channel Destination of the published message
mustSucceed Enforces the fact that the message must have been delivered to at least one consumer
message The message to publish.
muleEvent The current MuleEvent.
Returns
  • If no consumer is subscribed to the channel and mustSucceed is true, null is returned. Otherwise the message is returned.

public byte[] pushToList (String key, RedisModule.ListPushSide side, boolean ifExists, String value, MuleEvent muleEvent)

Push the message payload to the desired side (LEFT or RIGHT) of the list stored at the specified key. If key does not exist, a new key holding a list is created as long as ifExists is not true.

Parameters
key Key that will be used for LPUSH/RPUSH/LPUSHX/RPUSH
side The side where to push the payload, either LEFT or RIGHT
ifExists If true execute LPUSHX/RPUSH otherwise LPUSH/RPUSH
value The value to push.
muleEvent The current MuleEvent.
Returns
  • If the key doesn't already exist and ifExists is true, null is returned. Otherwise the message is returned.

public byte[] randomMemberFromSet (String key)

Reads a random value from the set stored at the specified key.

Parameters
key Key that will be used for SRANDMEMBER
Returns
  • The random value or null if either the set is empty or no set exists at the key

public Serializable remove (Serializable key)

Parameters
key
Throws
ObjectStoreException

public Serializable remove (Serializable key, String partitionName)

Parameters
key
partitionName
Throws
ObjectStoreException

public Serializable retrieve (Serializable key)

Parameters
key
Throws
ObjectStoreException

public Serializable retrieve (Serializable key, String partitionName)

Parameters
key
partitionName
Throws
ObjectStoreException

public byte[] set (String key, Integer expire, boolean ifNotExists, String value, MuleEvent muleEvent)

Set key to hold the payload. If key already holds a value, it is overwritten, regardless of its type as long as ifNotExists is false.

Parameters
key Key used to store payload
expire Set a timeout on the specified key. After the timeout the key will be automatically deleted by the server. A key with an associated timeout is said to be volatile in Redis terminology.
ifNotExists If true, then execute SETNX on the Redis server, otherwise execute SET
value The value to set.
muleEvent The current MuleEvent.
Returns
  • If the key already exists and ifNotExists is true, null is returned. Otherwise the message is returned.

public void setConnectionTimeout (int connectionTimeout)

Parameters
connectionTimeout

public void setDefaultPartitionName (String defaultPartitionName)

Parameters
defaultPartitionName

public void setHost (String host)

Parameters
host

public byte[] setInHash (String key, String field, boolean ifNotExists, String value, MuleEvent muleEvent)

Set the specified hash field to the message payload. If key does not exist, a new key holding a hash is created as long as ifNotExists is true.

Parameters
key Key that will be used for HSET
field Field that will be used for HSET
ifNotExists If true execute HSETNX otherwise HSET
value The value to set.
muleEvent The current MuleEvent.
Returns
  • If the field already exists and ifNotExists is true, null is returned, otherwise if a new field is created the message is returned.

public void setMuleContext (MuleContext muleContext)

Parameters
muleContext

public void setPassword (String password)

Parameters
password

public void setPoolConfig (GenericObjectPool.Config poolConfig)

Parameters
poolConfig

public void setPort (int port)

Parameters
port

public void setReconnectionFrequency (int reconnectionFrequency)

Parameters
reconnectionFrequency

public void store (Serializable key, Serializable value, String partitionName)

Parameters
key
value
partitionName
Throws
ObjectStoreException

public void store (Serializable key, Serializable value)

Parameters
key
value
Throws
ObjectStoreException

public void subscribe (List<String> channels, SourceCallback callback)

Subscribe to the specified channels.

Parameters
channels A list of channel names or globbing patterns.
callback Called when messages arrive in any of the specified channels.