Class MatsSpringAnnotationRegistration
- java.lang.Object
-
- io.mats3.spring.MatsSpringAnnotationRegistration
-
- All Implemented Interfaces:
org.springframework.beans.factory.Aware,org.springframework.beans.factory.config.BeanPostProcessor,org.springframework.context.ApplicationContextAware
@Role(2) @Component public class MatsSpringAnnotationRegistration extends java.lang.Object implements org.springframework.beans.factory.config.BeanPostProcessor, org.springframework.context.ApplicationContextAwareTheBeanPostProcessor-class specified by the@EnableMatsannotation. It checks all Spring beans registered to the SpringApplicationContextfor whether their classes are annotated with@MatsClassMapping, or if they have methods annotated with@MatsMappingor@MatsEndpointSetup, and if so configures Mats endpoints for them on the (possibly specified)MatsFactory. It will also control any registeredMatsFactorybeans, invokingMatsFactory.holdEndpointsUntilFactoryIsStarted()early in the startup procedure before adding the endpoints, and thenMatsFactory.start()as late as possible in the startup procedure. Upon Spring context shutdown, it invokesMatsFactory.stop(..)as early as possible in the shutdown procedure.Startup procedure:
- The
MatsSpringAnnotationRegistration(which is aBeanPostProcessor) will have each bean in the Spring ApplicationContext presented:- Each
MatsFactorybean will have theirMatsFactory.holdEndpointsUntilFactoryIsStarted()method invoked. - Each bean, and each method of each bean, will be searched for the relevant annotations. Such annotated methods will be put in a list.
- Each
- Upon
ContextRefreshedEvent:- All definitions that was put in the list will be processed, and Mats Endpoints will be registered into the
MatsFactory - using the specified MatsFactory if this was qualified in the definition (Read more at
@MatsMapping). - Then, all MatsFactories present in the ApplicationContext will have their
MatsFactory.start()method invoked, which will "release" the configured Mats endpoints, so that they will start (which in turn fires up their "stage processor" threads, which will each go and get a connection to the underlying MQ (which probably is JMS-based) and start to listen for messages).
- All definitions that was put in the list will be processed, and Mats Endpoints will be registered into the
MatsFactory - using the specified MatsFactory if this was qualified in the definition (Read more at
Notice:
ContextRefreshedEventis invoked late in the Spring ApplicationContext startup procedure. This means that all beans have been fully injected, and any@PostConstructand lifecycle events have been run. Thus, if the Endpoint depends on any bean/service, it is now running. This is important as the Endpoint's Stage Processors will start picking messages of the queue pretty much immediately. And this is important again if you have any cache that takes time to be populated - it might be of interest to instead use programmatic registration and implement delayed start for Endpoints which depends on caches that take a substantial time to load.Notice: All MatsFactories found in the Spring ApplicationContext are started, regardless of whether they had any Mats endpoints registered using the Mats SpringConfig. This implies that if you register any Mats endpoints programmatically using e.g.
@PostConstructor similar functionality, these will also be started - unless they have not had theirMatsEndpoint.finishSetup()method invoked, ref. "delayed start" mentioned above.Shutdown procedure:
- Upon
ContextClosedEvent, all MatsFactories in the ApplicationContext will have theirstop()method invoked. - This causes all registered Mats Endpoints to be
stopped, which releases the connection to the underlying MQ and stops the stage processor threads.
Notice:
ContextClosedEventis fired rather early in the Spring ApplicationContext shutdown procedure. When running in integration testing mode, which typically will involve starting an in-vm ActiveMQ in the same JVM as the endpoints, this early stopping and releasing of connections is beneficial so that when the in-vm ActiveMQ is stopped somewhat later, one won't get a load of connection failures from the Mats endpoints which otherwise would have their connections shut down under their feet.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMatsSpringAnnotationRegistration.MatsSpringConfigExceptionThrown if the setup of a Mats Spring endpoint fails.static classMatsSpringAnnotationRegistration.MatsSpringInvocationTargetExceptionThrown if the invocation of a@MatsMappingor@MatsEndpointSetupannotated method raisesInvocationTargetExceptionand the underlying exception is not aRuntimeException.
-
Constructor Summary
Constructors Constructor Description MatsSpringAnnotationRegistration()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidonContextClosedEvent(org.springframework.context.event.ContextClosedEvent e)ContextClosedEventruns pretty much as the first step in the Spring life cycle stopping process: Stop the MatsFactory, which will stop all MATS Endpoints, which will have them release their JMS resources - and then the MatsFactory will clean out the JmsMatsJmsSessionHandler.voidonContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent e)ContextRefreshedEventruns pretty much as the latest step in the Spring life cycle starting process: Processes allMatsMappingandMatsEndpointSetupannotations, then starts the MatsFactory, which will start any "hanging" MATS Endpoints, which will then start consuming messages.java.lang.ObjectpostProcessAfterInitialization(java.lang.Object bean, java.lang.String beanName)java.lang.ObjectpostProcessBeforeInitialization(java.lang.Object bean, java.lang.String beanName)voidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext)
-
-
-
Method Detail
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException- Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware- Throws:
org.springframework.beans.BeansException
-
postProcessBeforeInitialization
public java.lang.Object postProcessBeforeInitialization(java.lang.Object bean, java.lang.String beanName) throws org.springframework.beans.BeansException- Specified by:
postProcessBeforeInitializationin interfaceorg.springframework.beans.factory.config.BeanPostProcessor- Throws:
org.springframework.beans.BeansException
-
postProcessAfterInitialization
public java.lang.Object postProcessAfterInitialization(java.lang.Object bean, java.lang.String beanName) throws org.springframework.beans.BeansException- Specified by:
postProcessAfterInitializationin interfaceorg.springframework.beans.factory.config.BeanPostProcessor- Throws:
org.springframework.beans.BeansException
-
onContextRefreshedEvent
@EventListener public void onContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent e)
ContextRefreshedEventruns pretty much as the latest step in the Spring life cycle starting process: Processes allMatsMappingandMatsEndpointSetupannotations, then starts the MatsFactory, which will start any "hanging" MATS Endpoints, which will then start consuming messages.
-
onContextClosedEvent
@EventListener public void onContextClosedEvent(org.springframework.context.event.ContextClosedEvent e)
ContextClosedEventruns pretty much as the first step in the Spring life cycle stopping process: Stop the MatsFactory, which will stop all MATS Endpoints, which will have them release their JMS resources - and then the MatsFactory will clean out the JmsMatsJmsSessionHandler.
-
-