In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the principle and usage of RocketMQTransactionAnnotationProcessor". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Order
This paper mainly studies RocketMQTransactionAnnotationProcessor.
RocketMQTransactionAnnotationProcessor
RocketmqkoSpringMubootMutual 2.0.3 Methodissources.jarAcheOnRocketmqqqSpringCompact RocketMQTransactionAnnotationProcessor.java
Public class RocketMQTransactionAnnotationProcessor implements BeanPostProcessor, Ordered, ApplicationContextAware {private final static Logger log = LoggerFactory.getLogger (RocketMQTransactionAnnotationProcessor.class); private ApplicationContext applicationContext; private final Set targetClass = AopUtils.getTargetClass (bean); RocketMQTransactionListener listener = AnnotationUtils.findAnnotation (targetClass, RocketMQTransactionListener.class); this.nonProcessedClasses.add (bean.getClass ()) If (listener = = null) {/ / for quick search log.trace ("No @ RocketMQTransactionListener annotations found on bean type: {}", bean.getClass ());} else {try {processTransactionListenerAnnotation (listener, bean);} catch (MQClientException e) {log.error ("Failed to process annotation" + listener, e) Throw new BeanCreationException ("Failed to process annotation" + listener, e);} return bean;} private void processTransactionListenerAnnotation (RocketMQTransactionListener listener, Object bean) throws MQClientException {if (transactionHandlerRegistry = = null) {throw new MQClientException ("Bad usage of @ RocketMQTransactionListener," + "the class must work with RocketMQTemplate", null) } if (! RocketMQLocalTransactionListener.class.isAssignableFrom (bean.getClass () {throw new MQClientException ("Bad usage of @ RocketMQTransactionListener,"+" the class must implement interface RocketMQLocalTransactionListener ", null);} TransactionHandler transactionHandler = new TransactionHandler (); transactionHandler.setBeanFactory (this.applicationContext.getAutowireCapableBeanFactory ()); transactionHandler.setName (listener.txProducerGroup ()) TransactionHandler.setBeanName (bean.getClass (). GetName ()); transactionHandler.setListener ((RocketMQLocalTransactionListener) bean); transactionHandler.setCheckExecutor (listener.corePoolSize (), listener.maximumPoolSize (), listener.keepAliveTime (), listener.blockingQueueSize ()); RPCHook rpcHook = RocketMQUtil.getRPCHookByAkSk (applicationContext.getEnvironment (), listener.accessKey (), listener.secretKey ()) If (Objects.nonNull (rpcHook)) {transactionHandler.setRpcHook (rpcHook);} else {log.debug ("Access-key or secret-key not configure in" + listener + ".");} transactionHandlerRegistry.registerTransactionHandler (transactionHandler);} @ Override public int getOrder () {return LOWEST_PRECEDENCE;}}
RocketMQTransactionAnnotationProcessor implements BeanPostProcessor, Ordered and ApplicationContextAware interfaces.
The postProcessAfterInitialization method looks for the bean marked with RocketMQTransactionListener comments, and then executes the processTransactionListenerAnnotation method
The processTransactionListenerAnnotation method creates a transactionHandler and then executes transactionHandlerRegistry.registerTransactionHandler to register
TransactionHandler
RocketmqFIQFRISSINGFUTINFUOTHUTHUTHUTHANLER.java
Class TransactionHandler {private String name; private String beanName; private RocketMQLocalTransactionListener bean; private BeanFactory beanFactory; private ThreadPoolExecutor checkExecutor; private RPCHook rpcHook; public String getBeanName () {return beanName;} public void setBeanName (String beanName) {this.beanName = beanName;} public String getName () {return name;} public void setName (String name) {this.name = name } public RPCHook getRpcHook () {return rpcHook;} public void setRpcHook (RPCHook rpcHook) {this.rpcHook = rpcHook;} public BeanFactory getBeanFactory () {return beanFactory;} public void setBeanFactory (BeanFactory beanFactory) {this.beanFactory = beanFactory;} public void setListener (RocketMQLocalTransactionListener listener) {this.bean = listener;} public RocketMQLocalTransactionListener getListener () {return this.bean } public void setCheckExecutor (int corePoolSize, int maxPoolSize, long keepAliveTime, int blockingQueueSize) {this.checkExecutor = new ThreadPoolExecutor (corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS, new LinkedBlockingDeque (blockingQueueSize));} public ThreadPoolExecutor getCheckExecutor () {return checkExecutor;}}
TransactionHandler contains name, beanName, bean, beanFactory, checkExecutor, rpcHook attributes
TransactionHandlerRegistry
RocketmqFIRESINGRIBOTHUTHandlerRegistry.java is an orgUniverse apachemax rocketmqqqqqqspringlerRegistry.java
Public class TransactionHandlerRegistry implements DisposableBean {private RocketMQTemplate rocketMQTemplate; private final Set listenerContainers = new ConcurrentSet (); public TransactionHandlerRegistry (RocketMQTemplate template) {this.rocketMQTemplate = template;} @ Override public void destroy () throws Exception {listenerContainers.clear () } public void registerTransactionHandler (TransactionHandler handler) throws MQClientException {if (listenerContainers.contains (handler.getName () {throw new MQClientException (- 1, String .format ("The transaction name [% s] has been defined in TransactionListener [% s]", handler.getName (), handler.getBeanName () } listenerContainers.add (handler.getName ()); rocketMQTemplate.createAndStartTransactionMQProducer (handler.getName (), handler.getListener (), handler.getCheckExecutor (), handler.getRpcHook ());}
TransactionHandlerRegistry implements the DisposableBean interface, and its clear method directly empties the listenerContainers;registerTransactionHandler method to add the name of the handler to the listenerContainers, and then executes rocketMQTemplate.createAndStartTransactionMQProducer to create and start the TransactionMQProducer
Summary
RocketMQTransactionAnnotationProcessor implements BeanPostProcessor, Ordered and ApplicationContextAware interfaces.
The postProcessAfterInitialization method looks for the bean marked with RocketMQTransactionListener comments, and then executes the processTransactionListenerAnnotation method
The processTransactionListenerAnnotation method creates a transactionHandler and then executes transactionHandlerRegistry.registerTransactionHandler to register
This is the end of the introduction to the principle and usage of RocketMQTransactionAnnotationProcessor. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.