类 AbstractPollingMessageListenerContainer
- java.lang.Object
- org.springframework.jms.support.JmsAccessor
- org.springframework.jms.support.destination.JmsDestinationAccessor
- org.springframework.jms.listener.AbstractJmsListeningContainer
- org.springframework.jms.listener.AbstractMessageListenerContainer
- org.springframework.jms.listener.AbstractPollingMessageListenerContainer
- 所有已实现的接口:
Aware
,BeanNameAware
,DisposableBean
,InitializingBean
,Lifecycle
,Phased
,SmartLifecycle
,MessageListenerContainer
public abstract class AbstractPollingMessageListenerContainer extends AbstractMessageListenerContainer
Base class for listener container implementations which are based on polling. Provides support for listener handling based onMessageConsumer
, optionally participating in externally managed transactions.This listener container variant is built for repeated polling attempts, each invoking the
receiveAndExecute(java.lang.Object, javax.jms.Session, javax.jms.MessageConsumer)
method. The MessageConsumer used may be reobtained fo reach attempt or cached in between attempts; this is up to the concrete implementation. The receive timeout for each attempt can be configured through the"receiveTimeout"
property.The underlying mechanism is based on standard JMS MessageConsumer handling, which is perfectly compatible with both native JMS and JMS in a Java EE environment. Neither the JMS
MessageConsumer.setMessageListener
facility nor the JMS ServerSessionPool facility is required. A further advantage of this approach is full control over the listening process, allowing for custom scaling and throttling and of concurrent message processing (which is up to concrete subclasses).Message reception and listener execution can automatically be wrapped in transactions through passing a Spring
PlatformTransactionManager
into the"transactionManager"
property. This will usually be aJtaTransactionManager
in a Java EE environment, in combination with a JTA-aware JMS ConnectionFactory obtained from JNDI (check your application server's documentation).This base class does not assume any specific mechanism for asynchronous execution of polling invokers. Check out
DefaultMessageListenerContainer
for a concrete implementation which is based on Spring'sTaskExecutor
abstraction, including dynamic scaling of concurrent consumers and automatic self recovery.
嵌套类概要
从类继承的嵌套类/接口 org.springframework.jms.listener.AbstractJmsListeningContainer
AbstractJmsListeningContainer.SharedConnectionNotInitializedException
字段概要
字段 修饰符和类型 字段 说明 static long
DEFAULT_RECEIVE_TIMEOUT
The default receive timeout: 1000 ms = 1 second.从类继承的字段 org.springframework.jms.listener.AbstractJmsListeningContainer
lifecycleMonitor, sharedConnectionMonitor
从类继承的字段 org.springframework.jms.support.destination.JmsDestinationAccessor
RECEIVE_TIMEOUT_INDEFINITE_WAIT, RECEIVE_TIMEOUT_NO_WAIT
从类继承的字段 org.springframework.jms.support.JmsAccessor
logger
从接口继承的字段 org.springframework.context.SmartLifecycle
DEFAULT_PHASE
构造器概要
构造器 构造器 说明 AbstractPollingMessageListenerContainer()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected MessageConsumer
createListenerConsumer(Session session)
Create a MessageConsumer for the given JMS Session, registering a MessageListener for the specified listener.protected boolean
doReceiveAndExecute(Object invoker, Session session, MessageConsumer consumer, TransactionStatus status)
Actually execute the listener for a message received from the given consumer, fetching all requires resources and invoking the listener.protected Connection
getConnection(JmsResourceHolder holder)
Fetch an appropriate Connection from the given JmsResourceHolder.protected long
getReceiveTimeout()
Return the receive timeout (ms) configured for this listener container.protected Session
getSession(JmsResourceHolder holder)
Fetch an appropriate Session from the given JmsResourceHolder.protected PlatformTransactionManager
getTransactionManager()
Return the Spring PlatformTransactionManager to use for transactional wrapping of message reception plus listener execution.void
initialize()
Initialize this container.protected boolean
isSessionLocallyTransacted(Session session)
This implementation checks whether the Session is externally synchronized.protected void
messageReceived(Object invoker, Session session)
Template method that gets called right when a new message has been received, before attempting to process it.protected void
noMessageReceived(Object invoker, Session session)
Template method that gets called when no message has been received, before returning to the receive loop again.protected boolean
receiveAndExecute(Object invoker, Session session, MessageConsumer consumer)
Execute the listener for a message received from the given consumer, wrapping the entire operation in an external transaction if demanded.protected Message
receiveMessage(MessageConsumer consumer)
Receive a message from the given consumer.void
setReceiveTimeout(long receiveTimeout)
Set the timeout to use for receive calls, in milliseconds.void
setSessionTransacted(boolean sessionTransacted)
Set the transaction mode that is used when creating a JMSSession
.void
setTransactionManager(PlatformTransactionManager transactionManager)
Specify the SpringPlatformTransactionManager
to use for transactional wrapping of message reception plus listener execution.void
setTransactionName(String transactionName)
Specify the transaction name to use for transactional wrapping.void
setTransactionTimeout(int transactionTimeout)
Specify the transaction timeout to use for transactional wrapping, in seconds.protected boolean
shouldCommitAfterNoMessageReceived(Session session)
Determine whether to trigger a commit after no message has been received.从类继承的方法 org.springframework.jms.listener.AbstractMessageListenerContainer
checkMessageListener, commitIfNecessary, createConsumer, doExecuteListener, doInvokeListener, doInvokeListener, executeListener, getDefaultSubscriptionName, getDestination, getDestinationDescription, getDestinationName, getDurableSubscriptionName, getErrorHandler, getExceptionListener, getMessageConverter, getMessageListener, getMessageSelector, getReplyQosSettings, getSubscriptionName, handleListenerException, invokeErrorHandler, invokeExceptionListener, invokeListener, isAcceptMessagesWhileStopping, isExposeListenerSession, isPubSubNoLocal, isReplyPubSubDomain, isSubscriptionDurable, isSubscriptionShared, rollbackIfNecessary, rollbackOnExceptionIfNecessary, setAcceptMessagesWhileStopping, setConcurrency, setDestination, setDestinationName, setDurableSubscriptionName, setErrorHandler, setExceptionListener, setExposeListenerSession, setMessageConverter, setMessageListener, setMessageSelector, setPubSubNoLocal, setReplyPubSubDomain, setReplyQosSettings, setSubscriptionDurable, setSubscriptionName, setSubscriptionShared, setupMessageListener, validateConfiguration
从类继承的方法 org.springframework.jms.listener.AbstractJmsListeningContainer
afterPropertiesSet, createSharedConnection, destroy, doInitialize, doRescheduleTask, doShutdown, doStart, doStop, establishSharedConnection, getBeanName, getClientId, getPausedTaskCount, getPhase, getSharedConnection, isActive, isAutoStartup, isRunning, logRejectedTask, prepareSharedConnection, refreshSharedConnection, rescheduleTaskIfNecessary, resumePausedTasks, runningAllowed, setAutoStartup, setBeanName, setClientId, setPhase, sharedConnectionEnabled, shutdown, start, startSharedConnection, stop, stopSharedConnection
从类继承的方法 org.springframework.jms.support.destination.JmsDestinationAccessor
getDestinationResolver, isPubSubDomain, receiveFromConsumer, resolveDestinationName, setDestinationResolver, setPubSubDomain
从类继承的方法 org.springframework.jms.support.JmsAccessor
convertJmsAccessException, createConnection, createSession, getConnectionFactory, getSessionAcknowledgeMode, isClientAcknowledge, isSessionTransacted, obtainConnectionFactory, setConnectionFactory, setSessionAcknowledgeMode, setSessionAcknowledgeModeName
从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
从接口继承的方法 org.springframework.jms.listener.MessageListenerContainer
getDestinationResolver, isPubSubDomain
从接口继承的方法 org.springframework.context.SmartLifecycle
getPhase, isAutoStartup, stop
字段详细资料
DEFAULT_RECEIVE_TIMEOUT
public static final long DEFAULT_RECEIVE_TIMEOUT
The default receive timeout: 1000 ms = 1 second.- 另请参阅:
- 常量字段值
构造器详细资料
AbstractPollingMessageListenerContainer
public AbstractPollingMessageListenerContainer()
方法详细资料
setSessionTransacted
public void setSessionTransacted(boolean sessionTransacted)
从类复制的说明:JmsAccessor
Set the transaction mode that is used when creating a JMSSession
. Default is "false".Note that within a JTA transaction, the parameters passed to
create(Queue/Topic)Session(boolean transacted, int acknowledgeMode)
method are not taken into account. Depending on the Java EE transaction context, the container makes its own decisions on these values. Analogously, these parameters are not taken into account within a locally managed transaction either, since the accessor operates on an existing JMS Session in this case.Setting this flag to "true" will use a short local JMS transaction when running outside of a managed transaction, and a synchronized local JMS transaction in case of a managed transaction (other than an XA transaction) being present. This has the effect of a local JMS transaction being managed alongside the main transaction (which might be a native JDBC transaction), with the JMS transaction committing right after the main transaction.
setTransactionManager
public void setTransactionManager(@Nullable PlatformTransactionManager transactionManager)
Specify the SpringPlatformTransactionManager
to use for transactional wrapping of message reception plus listener execution.Default is none, not performing any transactional wrapping. If specified, this will usually be a Spring
JtaTransactionManager
or one of its subclasses, in combination with a JTA-aware ConnectionFactory that this message listener container obtains its Connections from.Note: Consider the use of local JMS transactions instead. Simply switch the
"sessionTransacted"
flag to "true" in order to use a locally transacted JMS Session for the entire receive processing, including any Session operations performed by aSessionAwareMessageListener
(e.g. sending a response message). This allows for fully synchronized Spring transactions based on local JMS transactions, similar to whatJmsTransactionManager
provides. CheckAbstractMessageListenerContainer
's javadoc for a discussion of transaction choices and message redelivery scenarios.
getTransactionManager
@Nullable protected final PlatformTransactionManager getTransactionManager()
Return the Spring PlatformTransactionManager to use for transactional wrapping of message reception plus listener execution.
setTransactionName
public void setTransactionName(String transactionName)
Specify the transaction name to use for transactional wrapping. Default is the bean name of this listener container, if any.
setTransactionTimeout
public void setTransactionTimeout(int transactionTimeout)
Specify the transaction timeout to use for transactional wrapping, in seconds. Default is none, using the transaction manager's default timeout.
setReceiveTimeout
public void setReceiveTimeout(long receiveTimeout)
Set the timeout to use for receive calls, in milliseconds. The default is 1000 ms, that is, 1 second.NOTE: This value needs to be smaller than the transaction timeout used by the transaction manager (in the appropriate unit, of course). 0 indicates no timeout at all; however, this is only feasible if not running within a transaction manager and generally discouraged since such a listener container cannot cleanly shut down. A negative value such as -1 indicates a no-wait receive operation.
getReceiveTimeout
protected long getReceiveTimeout()
Return the receive timeout (ms) configured for this listener container.- 从以下版本开始:
- 4.2
initialize
public void initialize()
从类复制的说明:AbstractJmsListeningContainer
Initialize this container.Creates a JMS Connection, starts the
Connection
(if"autoStartup"
hasn't been turned off), and callsAbstractJmsListeningContainer.doInitialize()
.
createListenerConsumer
protected MessageConsumer createListenerConsumer(Session session) throws JMSException
Create a MessageConsumer for the given JMS Session, registering a MessageListener for the specified listener.- 参数:
session
- the JMS Session to work on- 返回:
- the MessageConsumer
- 抛出:
JMSException
- if thrown by JMS methods- 另请参阅:
receiveAndExecute(java.lang.Object, javax.jms.Session, javax.jms.MessageConsumer)
receiveAndExecute
protected boolean receiveAndExecute(Object invoker, @Nullable Session session, @Nullable MessageConsumer consumer) throws JMSException
Execute the listener for a message received from the given consumer, wrapping the entire operation in an external transaction if demanded.- 参数:
session
- the JMS Session to work onconsumer
- the MessageConsumer to work on- 返回:
- whether a message has been received
- 抛出:
JMSException
- if thrown by JMS methods- 另请参阅:
doReceiveAndExecute(java.lang.Object, javax.jms.Session, javax.jms.MessageConsumer, org.springframework.transaction.TransactionStatus)
doReceiveAndExecute
protected boolean doReceiveAndExecute(Object invoker, @Nullable Session session, @Nullable MessageConsumer consumer, @Nullable TransactionStatus status) throws JMSException
Actually execute the listener for a message received from the given consumer, fetching all requires resources and invoking the listener.- 参数:
session
- the JMS Session to work onconsumer
- the MessageConsumer to work onstatus
- the TransactionStatus (may benull
)- 返回:
- whether a message has been received
- 抛出:
JMSException
- if thrown by JMS methods- 另请参阅:
AbstractMessageListenerContainer.doExecuteListener(javax.jms.Session, javax.jms.Message)
isSessionLocallyTransacted
protected boolean isSessionLocallyTransacted(Session session)
This implementation checks whether the Session is externally synchronized. In this case, the Session is not locally transacted, despite the listener container's "sessionTransacted" flag being set to "true".- 覆盖:
isSessionLocallyTransacted
在类中AbstractMessageListenerContainer
- 参数:
session
- the Session to check- 返回:
- whether the given Session is locally transacted
- 另请参阅:
JmsResourceHolder
shouldCommitAfterNoMessageReceived
protected boolean shouldCommitAfterNoMessageReceived(Session session)
Determine whether to trigger a commit after no message has been received. This is a good idea on any modern-day JMS provider.- 参数:
session
- the current JMS Session which received no message- 返回:
- whether to call
AbstractMessageListenerContainer.commitIfNecessary(javax.jms.Session, javax.jms.Message)
on the given Session
receiveMessage
@Nullable protected Message receiveMessage(MessageConsumer consumer) throws JMSException
Receive a message from the given consumer.- 参数:
consumer
- the MessageConsumer to use- 返回:
- the Message, or
null
if none - 抛出:
JMSException
- if thrown by JMS methods
messageReceived
protected void messageReceived(Object invoker, Session session)
Template method that gets called right when a new message has been received, before attempting to process it. Allows subclasses to react to the event of an actual incoming message, for example adapting their consumer count.- 参数:
invoker
- the invoker object (passed through)session
- the receiving JMS Session
noMessageReceived
protected void noMessageReceived(Object invoker, Session session)
Template method that gets called when no message has been received, before returning to the receive loop again. Allows subclasses to react to the event of no incoming message, for example marking the invoker as idle.- 参数:
invoker
- the invoker object (passed through)session
- the receiving JMS Session
getConnection
@Nullable protected Connection getConnection(JmsResourceHolder holder)
Fetch an appropriate Connection from the given JmsResourceHolder.This implementation accepts any JMS 1.1 Connection.
- 参数:
holder
- the JmsResourceHolder- 返回:
- an appropriate Connection fetched from the holder, or
null
if none found
getSession
@Nullable protected Session getSession(JmsResourceHolder holder)
Fetch an appropriate Session from the given JmsResourceHolder.This implementation accepts any JMS 1.1 Session.
- 参数:
holder
- the JmsResourceHolder- 返回:
- an appropriate Session fetched from the holder, or
null
if none found