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<org.springframework.integration.store.MessageGroup>, org.springframework.integration.store.MessageGroupStore, org.springframework.integration.store.MessageStore

@ManagedResource
public class JdbcMessageStore
extends org.springframework.integration.store.AbstractMessageGroupStore
implements org.springframework.integration.store.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. Notice: Starting with Spring Integration 3.0, this class will move to package: org.springframework.integration.jdbc.store. If you intend backing a MessageChannel using a JDBC-based Message Store, please consider using the channel-specific JdbcChannelMessageStore instead.

Since:
2.0

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> org.springframework.integration.Message<T>
addMessage(org.springframework.integration.Message<T> message)
           
 org.springframework.integration.store.MessageGroup addMessageToGroup(java.lang.Object groupId, org.springframework.integration.Message<?> message)
           
 void afterPropertiesSet()
          Check mandatory properties (data source and incrementer).
 void completeGroup(java.lang.Object groupId)
           
protected  org.springframework.integration.Message<?> doPollForMessage(java.lang.String groupIdKey)
          This method executes a call to the DB to get the oldest Message in the MessageGroup Override this method if need to.
protected  org.springframework.jdbc.core.JdbcOperations getJdbcOperations()
          To be used to get a reference to JdbcOperations in case this class is subclassed
 org.springframework.integration.Message<?> getMessage(java.util.UUID id)
           
 long getMessageCount()
           
 int getMessageCountForAllMessageGroups()
           
 org.springframework.integration.store.MessageGroup getMessageGroup(java.lang.Object groupId)
           
 int getMessageGroupCount()
           
protected  java.lang.String getQuery(org.springframework.integration.jdbc.JdbcMessageStore.Query base)
          Replace patterns in the input to produce a valid SQL query.
 java.util.Iterator<org.springframework.integration.store.MessageGroup> iterator()
           
 int messageGroupSize(java.lang.Object groupId)
           
 org.springframework.integration.Message<?> pollMessageFromGroup(java.lang.Object groupId)
           
 org.springframework.integration.Message<?> removeMessage(java.util.UUID id)
           
 org.springframework.integration.store.MessageGroup removeMessageFromGroup(java.lang.Object groupId, org.springframework.integration.Message<?> messageToRemove)
           
 void removeMessageGroup(java.lang.Object groupId)
           
 void setDataSource(javax.sql.DataSource dataSource)
          The JDBC DataSource to use when interacting with the database.
 void setDeserializer(org.springframework.core.serializer.Deserializer<? extends org.springframework.integration.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)
           
 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 org.springframework.integration.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

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 org.springframework.integration.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 org.springframework.integration.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 org.springframework.integration.Message<?> removeMessage(java.util.UUID id)
Specified by:
removeMessage in interface org.springframework.integration.store.MessageStore

getMessageCount

@ManagedAttribute
public long getMessageCount()
Specified by:
getMessageCount in interface org.springframework.integration.store.MessageStore

getMessage

public org.springframework.integration.Message<?> getMessage(java.util.UUID id)
Specified by:
getMessage in interface org.springframework.integration.store.MessageStore

addMessage

public <T> org.springframework.integration.Message<T> addMessage(org.springframework.integration.Message<T> message)
Specified by:
addMessage in interface org.springframework.integration.store.MessageStore

addMessageToGroup

public org.springframework.integration.store.MessageGroup addMessageToGroup(java.lang.Object groupId,
                                                                            org.springframework.integration.Message<?> message)
Specified by:
addMessageToGroup in interface org.springframework.integration.store.MessageGroupStore

getMessageGroupCount

@ManagedAttribute
public int getMessageGroupCount()
Specified by:
getMessageGroupCount in interface org.springframework.integration.store.MessageGroupStore
Overrides:
getMessageGroupCount in class org.springframework.integration.store.AbstractMessageGroupStore

getMessageCountForAllMessageGroups

@ManagedAttribute
public int getMessageCountForAllMessageGroups()
Specified by:
getMessageCountForAllMessageGroups in interface org.springframework.integration.store.MessageGroupStore
Overrides:
getMessageCountForAllMessageGroups in class org.springframework.integration.store.AbstractMessageGroupStore

messageGroupSize

@ManagedAttribute
public int messageGroupSize(java.lang.Object groupId)
Specified by:
messageGroupSize in interface org.springframework.integration.store.MessageGroupStore

getMessageGroup

public org.springframework.integration.store.MessageGroup getMessageGroup(java.lang.Object groupId)
Specified by:
getMessageGroup in interface org.springframework.integration.store.MessageGroupStore

removeMessageFromGroup

public org.springframework.integration.store.MessageGroup removeMessageFromGroup(java.lang.Object groupId,
                                                                                 org.springframework.integration.Message<?> messageToRemove)
Specified by:
removeMessageFromGroup in interface org.springframework.integration.store.MessageGroupStore

removeMessageGroup

public void removeMessageGroup(java.lang.Object groupId)
Specified by:
removeMessageGroup in interface org.springframework.integration.store.MessageGroupStore

completeGroup

public void completeGroup(java.lang.Object groupId)
Specified by:
completeGroup in interface org.springframework.integration.store.MessageGroupStore

setLastReleasedSequenceNumberForGroup

public void setLastReleasedSequenceNumberForGroup(java.lang.Object groupId,
                                                  int sequenceNumber)
Specified by:
setLastReleasedSequenceNumberForGroup in interface org.springframework.integration.store.MessageGroupStore

pollMessageFromGroup

public org.springframework.integration.Message<?> pollMessageFromGroup(java.lang.Object groupId)
Specified by:
pollMessageFromGroup in interface org.springframework.integration.store.MessageGroupStore

iterator

public java.util.Iterator<org.springframework.integration.store.MessageGroup> iterator()
Specified by:
iterator in interface java.lang.Iterable<org.springframework.integration.store.MessageGroup>
Specified by:
iterator in interface org.springframework.integration.store.MessageGroupStore

getQuery

protected java.lang.String getQuery(org.springframework.integration.jdbc.JdbcMessageStore.Query base)
Replace patterns in the input to produce a valid SQL query. This implementation lazily initializes a simple map-based cache, only replacing the table prefix on the first access to a named query. Further accesses will be resolved from the cache.

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

getJdbcOperations

protected org.springframework.jdbc.core.JdbcOperations getJdbcOperations()
To be used to get a reference to JdbcOperations in case this class is subclassed

Returns:
the JdbcOperations implementation

doPollForMessage

protected org.springframework.integration.Message<?> doPollForMessage(java.lang.String groupIdKey)
This method executes a call to the DB to get the oldest Message in the MessageGroup Override this method if need to. For example if you DB supports advanced function such as FIRST etc.

Parameters:
groupIdKey - String representation of message group ID
Returns:
a message; could be null if query produced no Messages