public abstract class AbstractJmsListeningContainer extends JmsDestinationAccessor implements BeanNameAware, DisposableBean, SmartLifecycle
JmsAccessor
base class.
This class provides basic lifecycle management, in particular management
of a shared JMS Connection. Subclasses are supposed to plug into this
lifecycle, implementing the sharedConnectionEnabled()
as well
as the doInitialize()
and doShutdown()
template methods.
This base class does not assume any specific listener programming model or listener invoker mechanism. It just provides the general runtime lifecycle management needed for any kind of JMS-based listening mechanism that operates on a JMS Connection/Session.
For a concrete listener programming model, check out the
AbstractMessageListenerContainer
subclass. For a concrete listener
invoker mechanism, check out the DefaultMessageListenerContainer
class.
sharedConnectionEnabled()
,
doInitialize()
,
doShutdown()
Modifier and Type | Class and Description |
---|---|
static class |
AbstractJmsListeningContainer.SharedConnectionNotInitializedException
Exception that indicates that the initial setup of this container's
shared JMS Connection failed.
|
Modifier and Type | Field and Description |
---|---|
protected Object |
lifecycleMonitor |
protected Object |
sharedConnectionMonitor |
logger
Constructor and Description |
---|
AbstractJmsListeningContainer() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Delegates to
validateConfiguration() and initialize() . |
protected Connection |
createSharedConnection()
Create a shared Connection for this container.
|
void |
destroy()
Calls
shutdown() when the BeanFactory destroys the container instance. |
protected abstract void |
doInitialize()
Register any invokers within this container.
|
protected void |
doRescheduleTask(Object task)
Reschedule the given task object immediately.
|
protected abstract void |
doShutdown()
Close the registered invokers.
|
protected void |
doStart()
Start the shared Connection, if any, and notify all invoker tasks.
|
protected void |
doStop()
Notify all invoker tasks and stop the shared Connection, if any.
|
protected void |
establishSharedConnection()
Establish a shared Connection for this container.
|
protected String |
getBeanName()
Return the bean name that this listener container has been assigned
in its containing bean factory, if any.
|
String |
getClientId()
Return the JMS client ID for the shared Connection created and used
by this container, if any.
|
int |
getPausedTaskCount()
Determine the number of currently paused tasks, if any.
|
int |
getPhase()
Return the phase in which this container will be started and stopped.
|
protected Connection |
getSharedConnection()
Return the shared JMS Connection maintained by this container.
|
void |
initialize()
Initialize this container.
|
boolean |
isActive()
Return whether this container is currently active,
that is, whether it has been set up but not shut down yet.
|
boolean |
isAutoStartup()
Returns
true if this Lifecycle component should get
started automatically by the container at the time that the containing
ApplicationContext gets refreshed. |
boolean |
isRunning()
Determine whether this container is currently running,
that is, whether it has been started and not stopped yet.
|
protected void |
logRejectedTask(Object task,
RuntimeException ex)
Log a task that has been rejected by
doRescheduleTask(java.lang.Object) . |
protected void |
prepareSharedConnection(Connection connection)
Prepare the given Connection, which is about to be registered
as shared Connection for this container.
|
protected void |
refreshSharedConnection()
Refresh the shared Connection that this container holds.
|
protected boolean |
rescheduleTaskIfNecessary(Object task)
Take the given task object and reschedule it, either immediately if
this container is currently running, or later once this container
has been restarted.
|
protected void |
resumePausedTasks()
Try to resume all paused tasks.
|
protected boolean |
runningAllowed()
Check whether this container's listeners are generally allowed to run.
|
void |
setAutoStartup(boolean autoStartup)
Set whether to automatically start the container after initialization.
|
void |
setBeanName(String beanName)
Set the name of the bean in the bean factory that created this bean.
|
void |
setClientId(String clientId)
Specify the JMS client ID for a shared Connection created and used
by this container.
|
void |
setPhase(int phase)
Specify the phase in which this container should be started and
stopped.
|
protected abstract boolean |
sharedConnectionEnabled()
Return whether a shared JMS Connection should be maintained
by this container base class.
|
void |
shutdown()
Stop the shared Connection, call
doShutdown() ,
and close this container. |
void |
start()
Start this container.
|
protected void |
startSharedConnection()
Start the shared Connection.
|
void |
stop()
Stop this container.
|
void |
stop(Runnable callback)
Indicates that a Lifecycle component must stop if it is currently running.
|
protected void |
stopSharedConnection()
Stop the shared Connection.
|
protected void |
validateConfiguration()
Validate the configuration of this container.
|
getDestinationResolver, isPubSubDomain, resolveDestinationName, setDestinationResolver, setPubSubDomain
convertJmsAccessException, createConnection, createSession, getConnectionFactory, getSessionAcknowledgeMode, isClientAcknowledge, isSessionTransacted, setConnectionFactory, setSessionAcknowledgeMode, setSessionAcknowledgeModeName, setSessionTransacted
protected final Object sharedConnectionMonitor
protected final Object lifecycleMonitor
public void setClientId(String clientId)
Note that client IDs need to be unique among all active Connections of the underlying JMS provider. Furthermore, a client ID can only be assigned if the original ConnectionFactory hasn't already assigned one.
public String getClientId()
public void setAutoStartup(boolean autoStartup)
Default is "true"; set this to "false" to allow for manual startup
through the start()
method.
public boolean isAutoStartup()
SmartLifecycle
true
if this Lifecycle
component should get
started automatically by the container at the time that the containing
ApplicationContext
gets refreshed.
A value of false
indicates that the component is intended to
be started through an explicit Lifecycle.start()
call instead, analogous
to a plain Lifecycle
implementation.
isAutoStartup
in interface SmartLifecycle
Lifecycle.start()
,
Phased.getPhase()
,
LifecycleProcessor.onRefresh()
,
ConfigurableApplicationContext.refresh()
public void setPhase(int phase)
public int getPhase()
public void setBeanName(String beanName)
BeanNameAware
Invoked after population of normal bean properties but before an
init callback such as InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanName
in interface BeanNameAware
beanName
- the name of the bean in the factory.
Note that this name is the actual bean name used in the factory, which may
differ from the originally specified name: in particular for inner bean
names, the actual bean name might have been made unique through appending
"#..." suffixes. Use the BeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.protected final String getBeanName()
public void afterPropertiesSet()
validateConfiguration()
and initialize()
.afterPropertiesSet
in interface InitializingBean
afterPropertiesSet
in class JmsAccessor
protected void validateConfiguration()
The default implementation is empty. To be overridden in subclasses.
public void destroy()
shutdown()
when the BeanFactory destroys the container instance.destroy
in interface DisposableBean
shutdown()
public void initialize() throws JmsException
Creates a JMS Connection, starts the Connection
(if "autoStartup"
hasn't been turned off),
and calls doInitialize()
.
JmsException
- if startup failedpublic void shutdown() throws JmsException
doShutdown()
,
and close this container.JmsException
- if shutdown failedpublic final boolean isActive()
public void start() throws JmsException
start
in interface Lifecycle
JmsException
- if starting faileddoStart()
protected void doStart() throws JMSException
JMSException
- if thrown by JMS API methodsstartSharedConnection()
public void stop() throws JmsException
stop
in interface Lifecycle
JmsException
- if stopping faileddoStop()
public void stop(Runnable callback)
SmartLifecycle
The provided callback is used by the LifecycleProcessor
to support
an ordered, and potentially concurrent, shutdown of all components having a
common shutdown order value. The callback must be executed after
the SmartLifecycle
component does indeed stop.
The LifecycleProcessor
will call only this variant of the
stop
method; i.e. Lifecycle.stop()
will not be called for
SmartLifecycle
implementations unless explicitly delegated to within
the implementation of this method.
stop
in interface SmartLifecycle
Lifecycle.stop()
,
Phased.getPhase()
protected void doStop() throws JMSException
JMSException
- if thrown by JMS API methodsstopSharedConnection()
public final boolean isRunning()
isRunning
in interface Lifecycle
start()
,
stop()
,
runningAllowed()
protected boolean runningAllowed()
This implementation always returns true
; the default 'running'
state is purely determined by start()
/ stop()
.
Subclasses may override this method to check against temporary
conditions that prevent listeners from actually running. In other words,
they may apply further restrictions to the 'running' state, returning
false
if such a restriction prevents listeners from running.
protected void establishSharedConnection() throws JMSException
The default implementation delegates to createSharedConnection()
,
which does one immediate attempt and throws an exception if it fails.
Can be overridden to have a recovery process in place, retrying
until a Connection can be successfully established.
JMSException
- if thrown by JMS API methodsprotected final void refreshSharedConnection() throws JMSException
Called on startup and also after an infrastructure exception that occurred during invoker setup and/or execution.
JMSException
- if thrown by JMS API methodsprotected Connection createSharedConnection() throws JMSException
The default implementation creates a standard Connection
and prepares it through prepareSharedConnection(javax.jms.Connection)
.
JMSException
- if the creation failedprotected void prepareSharedConnection(Connection connection) throws JMSException
The default implementation sets the specified client id, if any. Subclasses can override this to apply further settings.
connection
- the Connection to prepareJMSException
- if the preparation efforts failedgetClientId()
protected void startSharedConnection() throws JMSException
JMSException
- if thrown by JMS API methodsConnection.start()
protected void stopSharedConnection() throws JMSException
JMSException
- if thrown by JMS API methodsConnection.start()
protected final Connection getSharedConnection()
null
)IllegalStateException
- if this container does not maintain a
shared Connection, or if the Connection hasn't been initialized yetsharedConnectionEnabled()
protected final boolean rescheduleTaskIfNecessary(Object task)
If this container has already been shut down, the task will not get rescheduled at all.
task
- the task object to rescheduledoRescheduleTask(java.lang.Object)
protected void resumePausedTasks()
public int getPausedTaskCount()
protected void doRescheduleTask(Object task)
To be implemented by subclasses if they ever call
rescheduleTaskIfNecessary
.
This implementation throws an UnsupportedOperationException.
task
- the task object to reschedulerescheduleTaskIfNecessary(java.lang.Object)
protected void logRejectedTask(Object task, RuntimeException ex)
doRescheduleTask(java.lang.Object)
.
The default implementation simply logs a corresponding message at debug level.
task
- the rejected task objectex
- the exception thrown from doRescheduleTask(java.lang.Object)
protected abstract boolean sharedConnectionEnabled()
getSharedConnection()
protected abstract void doInitialize() throws JMSException
Subclasses need to implement this method for their specific invoker management process.
A shared JMS Connection, if any, will already have been started at this point.
JMSException
- if registration failedgetSharedConnection()
protected abstract void doShutdown() throws JMSException
Subclasses need to implement this method for their specific invoker management process.
A shared JMS Connection, if any, will automatically be closed afterwards.
JMSException
- if shutdown failedshutdown()