dubboのServiceBeanExportedEventについて

3747 ワード

シーケンス
本文は主にdubboのServiceBeanExportedEventを研究する
ServiceBeanExportedEvent
dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/ServiceBeanExportedEvent.java
public class ServiceBeanExportedEvent extends ApplicationEvent {

    /**
     * Create a new ApplicationEvent.
     *
     * @param serviceBean {@link ServiceBean} bean
     */
    public ServiceBeanExportedEvent(ServiceBean serviceBean) {
        super(serviceBean);
    }

    /**
     * Get {@link ServiceBean} instance
     *
     * @return non-null
     */
    public ServiceBean getServiceBean() {
        return (ServiceBean) super.getSource();
    }
}
  • ServiceBeanExportedEventはApplicationEventを継承し、そのsourceはServiceBean
  • です.
    ServiceBean
    dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ServiceBean.java
    public class ServiceBean extends ServiceConfig implements InitializingBean, DisposableBean,
            ApplicationContextAware, ApplicationListener, BeanNameAware,
            ApplicationEventPublisherAware {
    
            //......
    
        /**
         * @since 2.6.5
         */
        @Override
        public void export() {
            super.export();
            // Publish ServiceBeanExportedEvent
            publishExportEvent();
        }
    
        /**
         * @since 2.6.5
         */
        private void publishExportEvent() {
            ServiceBeanExportedEvent exportEvent = new ServiceBeanExportedEvent(this);
            applicationEventPublisher.publishEvent(exportEvent);
        }
    
            //......
    }
  • ServiceBeanのexportメソッドはpublishExportEvent
  • ReferenceAnnotationBeanPostProcessor
    dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
    public class ReferenceAnnotationBeanPostProcessor extends AnnotationInjectedBeanPostProcessor implements
            ApplicationContextAware, ApplicationListener {
    
            //......
    
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            if (event instanceof ServiceBeanExportedEvent) {
                onServiceBeanExportEvent((ServiceBeanExportedEvent) event);
            } else if (event instanceof ContextRefreshedEvent) {
                onContextRefreshedEvent((ContextRefreshedEvent) event);
            }
        }
    
        private void onServiceBeanExportEvent(ServiceBeanExportedEvent event) {
            ServiceBean serviceBean = event.getServiceBean();
            initReferenceBeanInvocationHandler(serviceBean);
        }
    
        private void initReferenceBeanInvocationHandler(ServiceBean serviceBean) {
            String serviceBeanName = serviceBean.getBeanName();
            // Remove ServiceBean when it's exported
            ReferenceBeanInvocationHandler handler = localReferenceBeanInvocationHandlerCache.remove(serviceBeanName);
            // Initialize
            if (handler != null) {
                handler.init();
            }
        }
    
            //......
    }
  • ReferenceAnnotationBeanPostProcessorは、ApplicationListenerのonApplicationEventメソッドを実装し、ServiceBeanExportedEventイベントを受信するとonServiceBeanExportEventを実行し、ここではlocalReferenceBeanInvocationHandlerCacheから削除し、その後ReferenceBeanInvocationHandlerのinitメソッド
  • を実行する.
    小結
    ServiceBeanExportedEventはApplicationEventを継承し、そのsourceはServiceBeanです.ServiceBeanのexportメソッドはpublishExportEvent;Reference AnnotationBeanPostProcessorは、ApplicationListenerのonApplicationEventメソッドを実装し、ServiceBeanExportedEventイベントを受信したときにonServiceBeanExportEventを実行します.ここでlocalReferenceBeanInvocationHandlerCacheから削除し、ReferenceBeanInvocationHandlerのinitメソッドを実行します.
    doc
  • ServiceBeanExportedEvent