Spring Integration

org.springframework.integration.jdbc
Class JdbcMessageStore

java.lang.Object
  extended by org.springframework.integration.store.AbstractMessageGroupStore
      extended by org.springframework.integration.jdbc.JdbcMessageStore
All Implemented Interfaces:
java.lang.Iterable<MessageGroup>, MessageGroupStore, MessageStore

@ManagedResource
public class JdbcMessageStore
extends AbstractMessageGroupStore
implements MessageStore

Implementation of MessageStore using a relational database via JDBC. SQL scripts to create the necessary tables are packaged as org/springframework/integration/jdbc/schema-*.sql, where * is the target database type.

Since:
2.0
Author:
Dave Syer, Oleg Zhurakousky

Field Summary
static java.lang.String CREATED_DATE_KEY
          The name of the message header that stores a timestamp for the time the message was inserted.
static int DEFAULT_LONG_STRING_LENGTH
           
static java.lang.String DEFAULT_TABLE_PREFIX
          Default value for the table prefix property.
static java.lang.String SAVED_KEY
          The name of the message header that stores a flag to indicate that the message has been saved.
 
Constructor Summary
JdbcMessageStore()
          Convenient constructor for configuration use.
JdbcMessageStore(javax.sql.DataSource dataSource)
          Create a MessageStore with all mandatory properties.
 
Method Summary
<T> Message<T>
addMessage(Message<T> message)
          Put the provided Message into the MessageStore.
 MessageGroup addMessageToGroup(java.lang.Object groupId, Message<?> message)
          Store a message with an association to a group id.
 void afterPropertiesSet()
          Check mandatory properties (data source and incrementer).
 void completeGroup(java.lang.Object groupId)
          Completes this MessageGroup.
 Message<?> getMessage(java.util.UUID id)
          Return the Message with the given id, or null if no Message with that id exists in the MessageStore.
 long getMessageCount()
          Optional attribute giving the number of messages in the store.
 int getMessageCountForAllMessageGroups()
          Optional attribute giving the number of messages in the store over all groups.
 MessageGroup getMessageGroup(java.lang.Object groupId)
          Return all Messages currently in the MessageStore that were stored using MessageGroupStore.addMessageToGroup(Object, Message) with this group id.
 int getMessageGroupCount()
          Optional attribute giving the number of message groups.
protected  java.lang.String getQuery(java.lang.String base)
          Replace patterns in the input to produce a valid SQL query.
 java.util.Iterator<MessageGroup> iterator()
          Returns the iterator of currently accumulated MessageGroups
 int messageGroupSize(java.lang.Object groupId)
          Returns the size of this MessageGroup
 Message<?> pollMessageFromGroup(java.lang.Object groupId)
          Polls Message from this MessageGroup (in FIFO style if supported by the implementation) while also removing the polled Message
 Message<?> removeMessage(java.util.UUID id)
          Remove the Message with the given id from the MessageStore, if present, and return it.
 MessageGroup removeMessageFromGroup(java.lang.Object groupId, Message<?> messageToRemove)
          Persist a deletion on a single message from the group.
 void removeMessageGroup(java.lang.Object groupId)
          Remove the message group with this id.
 void setDataSource(javax.sql.DataSource dataSource)
          The JDBC DataSource to use when interacting with the database.
 void setDeserializer(org.springframework.core.serializer.Deserializer<? extends Message<?>> deserializer)
          A converter for deserializing byte arrays to messages.
 void setJdbcTemplate(org.springframework.jdbc.core.JdbcOperations jdbcTemplate)
          The JdbcOperations to use when interacting with the database.
 void setLastReleasedSequenceNumberForGroup(java.lang.Object groupId, int sequenceNumber)
          Allows you to set the sequence number of the last released Message.
 void setLobHandler(org.springframework.jdbc.support.lob.LobHandler lobHandler)
          Override the LobHandler that is used to create and unpack large objects in SQL queries.
 void setRegion(java.lang.String region)
          A unique grouping identifier for all messages persisted with this store.
 void setSerializer(org.springframework.core.serializer.Serializer<? super Message<?>> serializer)
          A converter for serializing messages to byte arrays for storage.
 void setTablePrefix(java.lang.String tablePrefix)
          Public setter for the table prefix property.
 
Methods inherited from class org.springframework.integration.store.AbstractMessageGroupStore
expireMessageGroups, isTimeoutOnIdle, registerMessageGroupExpiryCallback, setExpiryCallbacks, setTimeoutOnIdle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_TABLE_PREFIX

public static final java.lang.String DEFAULT_TABLE_PREFIX
Default value for the table prefix property.

See Also:
Constant Field Values

DEFAULT_LONG_STRING_LENGTH

public static final int DEFAULT_LONG_STRING_LENGTH
See Also:
Constant Field Values

SAVED_KEY

public static final java.lang.String SAVED_KEY
The name of the message header that stores a flag to indicate that the message has been saved. This is an optimization for the put method.


CREATED_DATE_KEY

public static final java.lang.String CREATED_DATE_KEY
The name of the message header that stores a timestamp for the time the message was inserted.

Constructor Detail

JdbcMessageStore

public JdbcMessageStore()
Convenient constructor for configuration use.


JdbcMessageStore

public JdbcMessageStore(javax.sql.DataSource dataSource)
Create a MessageStore with all mandatory properties.

Parameters:
dataSource - a DataSource
Method Detail

getQuery

protected java.lang.String getQuery(java.lang.String base)
Replace patterns in the input to produce a valid SQL query. This implementation replaces the table prefix.

Parameters:
base - the SQL query to be transformed
Returns:
a transformed query with replacements

setTablePrefix

public void setTablePrefix(java.lang.String tablePrefix)
Public setter for the table prefix property. This will be prefixed to all the table names before queries are executed. Defaults to DEFAULT_TABLE_PREFIX.

Parameters:
tablePrefix - the tablePrefix to set

setRegion

public void setRegion(java.lang.String region)
A unique grouping identifier for all messages persisted with this store. Using multiple regions allows the store to be partitioned (if necessary) for different purposes. Defaults to DEFAULT.

Parameters:
region - the region name to set

setDataSource

public void setDataSource(javax.sql.DataSource dataSource)
The JDBC DataSource to use when interacting with the database. Either this property can be set or the jdbcTemplate.

Parameters:
dataSource - a DataSource

setJdbcTemplate

public void setJdbcTemplate(org.springframework.jdbc.core.JdbcOperations jdbcTemplate)
The JdbcOperations to use when interacting with the database. Either this property can be set or the dataSource.

Parameters:
jdbcTemplate - a JdbcOperations

setLobHandler

public void setLobHandler(org.springframework.jdbc.support.lob.LobHandler lobHandler)
Override the LobHandler that is used to create and unpack large objects in SQL queries. The default is fine for almost all platforms, but some Oracle drivers require a native implementation.

Parameters:
lobHandler - a LobHandler

setSerializer

public void setSerializer(org.springframework.core.serializer.Serializer<? super Message<?>> serializer)
A converter for serializing messages to byte arrays for storage.

Parameters:
serializer - the serializer to set

setDeserializer

public void setDeserializer(org.springframework.core.serializer.Deserializer<? extends Message<?>> deserializer)
A converter for deserializing byte arrays to messages.

Parameters:
deserializer - the deserializer to set

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Check mandatory properties (data source and incrementer).

Throws:
java.lang.Exception

removeMessage

public Message<?> removeMessage(java.util.UUID id)
Description copied from interface: MessageStore
Remove the Message with the given id from the MessageStore, if present, and return it. If no Message with that id is present in the store, this will return null.

Specified by:
removeMessage in interface MessageStore

getMessageCount

@ManagedAttribute
public long getMessageCount()
Description copied from interface: MessageStore
Optional attribute giving the number of messages in the store. Implementations may decline to respond by throwing an exception.

Specified by:
getMessageCount in interface MessageStore
Returns:
the number of messages

getMessage

public Message<?> getMessage(java.util.UUID id)
Description copied from interface: MessageStore
Return the Message with the given id, or null if no Message with that id exists in the MessageStore.

Specified by:
getMessage in interface MessageStore

addMessage

public <T> Message<T> addMessage(Message<T> message)
Description copied from interface: MessageStore
Put the provided Message into the MessageStore. The store may need to mutate the message internally, and if it does then the return value can be different than the input. The id of the return value will be used as an index so that the MessageStore.getMessage(UUID) and MessageStore.removeMessage(UUID) behave properly. Since messages are immutable, putting the same message more than once is a no-op.

Specified by:
addMessage in interface MessageStore
Returns:
the message that was stored

addMessageToGroup

public MessageGroup addMessageToGroup(java.lang.Object groupId,
                                      Message<?> message)
Description copied from interface: MessageGroupStore
Store a message with an association to a group id. This can be used to group messages together.

Specified by:
addMessageToGroup in interface MessageGroupStore
Parameters:
groupId - the group id to store the message under
message - a message

getMessageGroupCount

@ManagedAttribute
public int getMessageGroupCount()
Description copied from interface: MessageGroupStore
Optional attribute giving the number of message groups. Implementations may decline to respond by throwing an exception.

Specified by:
getMessageGroupCount in interface MessageGroupStore
Overrides:
getMessageGroupCount in class AbstractMessageGroupStore
Returns:
the number message groups

getMessageCountForAllMessageGroups

@ManagedAttribute
public int getMessageCountForAllMessageGroups()
Description copied from interface: MessageGroupStore
Optional attribute giving the number of messages in the store over all groups. Implementations may decline to respond by throwing an exception.

Specified by:
getMessageCountForAllMessageGroups in interface MessageGroupStore
Overrides:
getMessageCountForAllMessageGroups in class AbstractMessageGroupStore
Returns:
the number of messages

messageGroupSize

@ManagedAttribute
public int messageGroupSize(java.lang.Object groupId)
Description copied from interface: MessageGroupStore
Returns the size of this MessageGroup

Specified by:
messageGroupSize in interface MessageGroupStore

getMessageGroup

public MessageGroup getMessageGroup(java.lang.Object groupId)
Description copied from interface: MessageGroupStore
Return all Messages currently in the MessageStore that were stored using MessageGroupStore.addMessageToGroup(Object, Message) with this group id.

Specified by:
getMessageGroup in interface MessageGroupStore
Returns:
a group of messages, empty if none exists for this key

removeMessageFromGroup

public MessageGroup removeMessageFromGroup(java.lang.Object groupId,
                                           Message<?> messageToRemove)
Description copied from interface: MessageGroupStore
Persist a deletion on a single message from the group. The group is modified to reflect that 'messageToRemove' is no longer present in the group.

Specified by:
removeMessageFromGroup in interface MessageGroupStore
Parameters:
groupId - the groupId for the group containing the message
messageToRemove - the message to be removed

removeMessageGroup

public void removeMessageGroup(java.lang.Object groupId)
Description copied from interface: MessageGroupStore
Remove the message group with this id.

Specified by:
removeMessageGroup in interface MessageGroupStore
Parameters:
groupId - the id of the group to remove

completeGroup

public void completeGroup(java.lang.Object groupId)
Description copied from interface: MessageGroupStore
Completes this MessageGroup. Completion of the MessageGroup generally means that this group should not be allowing any more mutating operation to be performed on it. For example any attempt to add/remove new Message form the group should not be allowed.

Specified by:
completeGroup in interface MessageGroupStore

setLastReleasedSequenceNumberForGroup

public void setLastReleasedSequenceNumberForGroup(java.lang.Object groupId,
                                                  int sequenceNumber)
Description copied from interface: MessageGroupStore
Allows you to set the sequence number of the last released Message. Used for Resequencing use cases

Specified by:
setLastReleasedSequenceNumberForGroup in interface MessageGroupStore

pollMessageFromGroup

public Message<?> pollMessageFromGroup(java.lang.Object groupId)
Description copied from interface: MessageGroupStore
Polls Message from this MessageGroup (in FIFO style if supported by the implementation) while also removing the polled Message

Specified by:
pollMessageFromGroup in interface MessageGroupStore

iterator

public java.util.Iterator<MessageGroup> iterator()
Description copied from interface: MessageGroupStore
Returns the iterator of currently accumulated MessageGroups

Specified by:
iterator in interface java.lang.Iterable<MessageGroup>
Specified by:
iterator in interface MessageGroupStore

Spring Integration