日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

源碼解讀Spring-Integration執行過程

瀏覽:215日期:2023-07-10 17:37:36
一,前言

Spring-Integration基于Spring,在應用程序中啟用了輕量級消息傳遞,并支持通過聲明式適配器與外部系統集成。這一段官網的介紹,概況了整個Integration的用途。個人感覺消息傳遞是真正的重點。

源碼解讀Spring-Integration執行過程

如上圖所示,典型的生產者-消費者模式,中間通過一個特定的通道進行數據傳輸,說到這,是不是隱隱感覺到queue的存在。確實事實上這個所謂的通道默認就是用的 blockingqueue。

Spring-Integration網上的資料是真少,再加上源碼分析的是更少。關于Spring-Integration的基本介紹直接去官網上看更加的直觀,這邊就不累述了。

今天主要是看個簡單的hello word進來分析下整個執行過程。

先看下代碼:

<?xml version='1.0' encoding='UTF-8'?><beans:beans xmlns='http://www.springframework.org/schema/integration'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xmlns:beans='http://www.springframework.org/schema/beans'xsi:schemaLocation='http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/integrationhttps://www.springframework.org/schema/integration/spring-integration.xsd'><annotation-config/><channel ><queue/></channel><beans:bean /></beans:beans>

@Configurationpublic class Beans { @ServiceActivator(inputChannel = 'ic', outputChannel = 'oc') public String sayHello(String name) {return 'Hello ' + name; }}

public class HelloWorldDemo { @Test public void testDemo() throws Exception {ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext('/demo.xml', HelloWorldDemo.class);DirectChannel inputChannel = context.getBean('ic', DirectChannel.class);PollableChannel outputChannel = context.getBean('oc', PollableChannel.class);inputChannel.send(new GenericMessage<String>('World'));System.out.println('==> HelloWorldDemo: ' + outputChannel.receive(0).getPayload());context.close(); }}out:==> HelloWorldDemo: Hello World二,ServiceActivator

上面的代碼演示了調用方法的入站通道適配器和標準的出站通道適配器, 它們之間是一個帶注解的ServiceActivator。關于這個ServiceActivator就是一個消息端點。

消息端點的主要作用是以非侵入性方式將應用程序代碼連接到消息傳遞框架。換句話說,理想情況下,應用程序代碼應該不知道消息對象或消息管道。這類似于 MVC 范式中controller 的作用。正如controller 處理 HTTP 請求一樣,消息端點處理消息。以及controller 映射到 URL 模式一樣,消息端點映射到消息通道。這兩種情況的目標是相同的。

ServiceActivator是用于將服務實例連接到消息傳遞系統的通用端點。必須配置輸入消息通道,如果要調用的服務方法能夠返回值,還可以提供輸出消息通道。

具體流程如下圖:

源碼解讀Spring-Integration執行過程

上面的代碼比較簡單,但是或許會發現我們只定義了輸出通道oc,輸入通道ic竟然沒有定義也能正常應用,是不是很奇怪?帶著疑問我們先看下ServiceActivator的源碼:

源碼解讀Spring-Integration執行過程

注釋上寫的很清楚,如果輸入通道不存在,將在應用程序上下文中注冊具有此名稱的DirectChannel 。具體在哪定義,我們后面會看到,現在不急,先一步步來看他的執行過程。

我們全局查找ServiceActivator,看他是哪邊進行處理的,最后發現了MessagingAnnotationPostProcessor類,用來處理方法級消息注解的BeanPostProcessor實現。

源碼解讀Spring-Integration執行過程

@Overridepublic void afterPropertiesSet() {Assert.notNull(this.beanFactory, 'BeanFactory must not be null');((BeanDefinitionRegistry) this.beanFactory).registerBeanDefinition(IntegrationContextUtils.DISPOSABLES_BEAN_NAME,BeanDefinitionBuilder.genericBeanDefinition(Disposables.class, Disposables::new).getRawBeanDefinition());this.postProcessors.put(Filter.class, new FilterAnnotationPostProcessor(this.beanFactory));this.postProcessors.put(Router.class, new RouterAnnotationPostProcessor(this.beanFactory));this.postProcessors.put(Transformer.class, new TransformerAnnotationPostProcessor(this.beanFactory));this.postProcessors.put(ServiceActivator.class, new ServiceActivatorAnnotationPostProcessor(this.beanFactory));this.postProcessors.put(Splitter.class, new SplitterAnnotationPostProcessor(this.beanFactory));this.postProcessors.put(Aggregator.class, new AggregatorAnnotationPostProcessor(this.beanFactory));this.postProcessors.put(InboundChannelAdapter.class,new InboundChannelAdapterAnnotationPostProcessor(this.beanFactory));this.postProcessors.put(BridgeFrom.class, new BridgeFromAnnotationPostProcessor(this.beanFactory));this.postProcessors.put(BridgeTo.class, new BridgeToAnnotationPostProcessor(this.beanFactory));Map<Class<? extends Annotation>, MethodAnnotationPostProcessor<?>> customPostProcessors =setupCustomPostProcessors();if (!CollectionUtils.isEmpty(customPostProcessors)) {this.postProcessors.putAll(customPostProcessors);}}

在afterPropertiesSet方法中,我們看到定義了一個后處理器postProcessors,里面注冊了相關的注解處理類。包含各種消息端點處理,除了上面寫的ServiceActivator,還有過濾器,路由,轉換器等各種不同的端點方法。

接著往向下看,既然實現了BeanPostProcessor,那必然要用到postProcessAfterInitialization方法實現,這里的流程大概就是遍歷出包含有@ServiceActivator的bean方法,用來做后續處理。我們直接看重點的代碼。

Object result = postProcessor.postProcess(bean, beanName, targetMethod, annotations);三,postProcess

在AbstractMethodAnnotationPostProcessor中有個共通方法postProcess用來生成對應的端點信息。具體代碼:

@Overridepublic Object postProcess(Object bean, String beanName, Method method, List<Annotation> annotations) {Object sourceHandler = null;if (beanAnnotationAware() && AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {if (!this.beanFactory.containsBeanDefinition(resolveTargetBeanName(method))) {this.logger.debug('Skipping endpoint creation; perhaps due to some ’@Conditional’ annotation.');return null;}else {sourceHandler = resolveTargetBeanFromMethodWithBeanAnnotation(method);}}//生成對應的MessageHandler,用來執行對應的注解的方法MessageHandler handler = createHandler(bean, method, annotations);if (!(handler instanceof ReactiveMessageHandlerAdapter)) {orderable(method, handler);producerOrRouter(annotations, handler);if (!handler.equals(sourceHandler)) {handler = registerHandlerBean(beanName, method, handler);}handler = annotated(method, handler);handler = adviceChain(beanName, annotations, handler);}//將MessageHandler實現連接到消息端點,生成對應的endpoint。AbstractEndpoint endpoint = createEndpoint(handler, method, annotations);if (endpoint != null) {return endpoint;}else {return handler;}}

這里面主要是兩件事:

根據模板模式中不同的createHandler抽象方法實現,生成對應的MessageHandler。譬如說我們這邊的ServiceActivatorAnnotationPostProcessor 將MessageHandler實現連接到消息端點,生成對應的endpoint。

1.createHandler

@Overrideprotected MessageHandler createHandler(Object bean, Method method, List<Annotation> annotations) {AbstractReplyProducingMessageHandler serviceActivator;if (AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {...else {serviceActivator = new ServiceActivatingHandler(bean, method);}String requiresReply = MessagingAnnotationUtils.resolveAttribute(annotations, 'requiresReply', String.class);if (StringUtils.hasText(requiresReply)) {serviceActivator.setRequiresReply(resolveAttributeToBoolean(requiresReply));}String isAsync = MessagingAnnotationUtils.resolveAttribute(annotations, 'async', String.class);if (StringUtils.hasText(isAsync)) {serviceActivator.setAsync(resolveAttributeToBoolean(isAsync));}//是否設置了輸出通道setOutputChannelIfPresent(annotations, serviceActivator);return serviceActivator;}

createHandler的代碼比較簡單,就是根據注解中的幾個屬性還有對應的方法參數,生成ServiceActivatingHandler。追溯下去ServiceActivatingHandler中最后會生成一個委托對象MessagingMethodInvokerHelper用來以反射的方式來執行目標方法。

2.createEndpoint

createEndpoint字面上都能知道是生成消息端點,事實上也是,把生成的handler和對應的管道進行關聯。具體看下代碼體會:

protected AbstractEndpoint createEndpoint(MessageHandler handler, @SuppressWarnings('unused') Method method,List<Annotation> annotations) {AbstractEndpoint endpoint = null;//取得注解中inputChannelName String inputChannelName = MessagingAnnotationUtils.resolveAttribute(annotations, getInputChannelAttribute(),String.class);if (StringUtils.hasText(inputChannelName)) {MessageChannel inputChannel;try {//從beanFactory中取得對應的通道beaninputChannel = this.channelResolver.resolveDestination(inputChannelName);}catch (DestinationResolutionException e) { //取不到,則自動注冊一個類型為DirectChannel的inputChannel if (e.getCause() instanceof NoSuchBeanDefinitionException) {inputChannel = new DirectChannel();this.beanFactory.registerSingleton(inputChannelName, inputChannel);inputChannel = (MessageChannel) this.beanFactory.initializeBean(inputChannel, inputChannelName);if (this.disposables != null) {this.disposables.add((DisposableBean) inputChannel);}}else {throw e;}}Assert.notNull(inputChannel, () -> 'failed to resolve inputChannel ’' + inputChannelName + '’');//生成endpoint endpoint = doCreateEndpoint(handler, inputChannel, annotations);}return endpoint;}

上面的代碼中,我們就能清楚的看到為什么我們在demo中沒有注冊輸入通道也能正常應用的原因了,從而回答之前的疑問。

protected AbstractEndpoint doCreateEndpoint(MessageHandler handler, MessageChannel inputChannel,List<Annotation> annotations) {....else if (inputChannel instanceof SubscribableChannel) {//生成SubscribableChannel類型對應的執行端點return new EventDrivenConsumer((SubscribableChannel) inputChannel, handler);}else if (inputChannel instanceof PollableChannel) {return pollingConsumer(inputChannel, handler, pollers);}else {throw new IllegalArgumentException('Unsupported ’inputChannel’ type: ’'+ inputChannel.getClass().getName() + '’. ' +'Must be one of ’SubscribableChannel’, ’PollableChannel’ or ’ReactiveStreamsSubscribableChannel’');}}

通道類型一共有兩種,一種是發布訂閱,一種是可輪詢的,我們是默認是走的第一種,因為DirectChannel默認就是個SubscribableChannel。所以最終我們生成了對應的信息端點類EventDrivenConsumer。

我們先看下EventDrivenConsumer整體結構:

源碼解讀Spring-Integration執行過程

EventDrivenConsumer上面有一個抽象類AbstractEndpoint,最上面實現了Lifecycle接口,所以生命周期跟著容器走,我們直接跳到star方法看:

@Overrideprotected void doStart() {this.logComponentSubscriptionEvent(true);//把handler和inputChannel進行綁定this.inputChannel.subscribe(this.handler);if (this.handler instanceof Lifecycle) {((Lifecycle) this.handler).start();}}

@Overridepublic synchronized boolean addHandler(MessageHandler handler) {Assert.notNull(handler, 'handler must not be null');Assert.isTrue(this.handlers.size() < this.maxSubscribers, 'Maximum subscribers exceeded');boolean added = this.handlers.add(handler);if (this.handlers.size() == 1) {this.theOneHandler = handler;}else {this.theOneHandler = null;}return added;}

上面的代碼主要就是把handler注冊到inputChannel中,這樣只要inputChannel通道一收到信息,就會通知他注冊的handlers進行處理。代碼中比較清楚的記錄了一切的操作,就不多解釋了。

四,發送信息

執行完上面一系列的注冊,已經把這一些的通道打通了,剩下的就是真正的發送操作了。下面分析下inputChannel.send(new GenericMessage<String>('World'));看看send操作:

/** * 在此頻道上發送消息。 如果通道已滿,則此方法將阻塞,直到發生超時或發送線程中斷。 如果指定的超時時間為 0,則該方法將立即返回。 如果小于零,它將無限期阻塞(請參閱send(Message) )。 * 參數: * messageArg ? 要發送的消息 * timeout - 以毫秒為單位的超時時間 * 返回: * true如果消息發送成功, false如果消息無法在規定時間內發送或發送線程被中斷 */@Override public boolean send(Message<?> messageArg, long timeout) {...try {//message是否需要轉換message = convertPayloadIfNecessary(message);//發送前攔截器if (interceptorList.getSize() > 0) {interceptorStack = new ArrayDeque<>();message = interceptorList.preSend(message, this, interceptorStack);if (message == null) {return false;}}if (this.metricsCaptor != null) {sample = this.metricsCaptor.start();}//發送操作sent = doSend(message, timeout);if (sample != null) {sample.stop(sendTimer(sent));}metricsProcessed = true;if (debugEnabled) {logger.debug('postSend (sent=' + sent + ') on channel ’' + this + '’, message: ' + message);}//發送后攔截器if (interceptorStack != null) {interceptorList.postSend(message, this, sent);interceptorList.afterSendCompletion(message, this, sent, null, interceptorStack);}return sent;}catch (Exception ex) {...}}

真正的send操作跟下去,會發現層次極深,礙于篇幅,我們直接跟到重點代碼:

@Overrideprotected final void handleMessageInternal(Message<?> message) {Object result;if (this.advisedRequestHandler == null) {//反射執行對應的端點方法result = handleRequestMessage(message);}else {result = doInvokeAdvisedRequestHandler(message);}if (result != null) {//往outputChannel發送執行結果 sendOutputs(result, message);}...}

handleRequestMessage的操作就是用之前我們handler中的委托類MessagingMethodInvokerHelper去反射運行對應的端點方法,然后把執行結果發送outputChannel。最后我們直接定位到具體的發送操作:

@Overrideprotected boolean doSend(Message<?> message, long timeout) {Assert.notNull(message, '’message’ must not be null');try {if (this.queue instanceof BlockingQueue) {BlockingQueue<Message<?>> blockingQueue = (BlockingQueue<Message<?>>) this.queue;if (timeout > 0) {return blockingQueue.offer(message, timeout, TimeUnit.MILLISECONDS);}if (timeout == 0) {return blockingQueue.offer(message);}blockingQueue.put(message);return true;}else {try {return this.queue.offer(message);}finally {this.queueSemaphore.release();}}}catch (InterruptedException e) {Thread.currentThread().interrupt();return false;}}

看到這,我們就明白了數據的去向,存儲在隊列里了,生產者產生的數據就已經生成了,所以發送的操作基本上就告一段落了。

五,接收信息

數據已經生成,后面就是看如何消費操作了,下面分析下 outputChannel.receive(0).getPayload()操作:

/** * 從該通道接收第一條可用消息。 如果通道不包含任何消息,則此方法將阻塞,直到分配的超時時間過去。 如果指定的超時時間為 0,則該方法將立即返回。 如果小于零,它將無限期阻塞(參見receive() )。 * 參數: * timeout - 以毫秒為單位的超時時間 * 返回: * 如果在分配的時間內沒有可用的消息或接收線程被中斷,則為第一個可用消息或null 。 */@Override // NOSONAR complexity@Nullablepublic Message<?> receive(long timeout) {...try {//接受前攔截器操作if (interceptorList.getSize() > 0) {interceptorStack = new ArrayDeque<>();//一旦調用接收并在實際檢索消息之前調用if (!interceptorList.preReceive(this, interceptorStack)) {return null;}}//接收操作Message<?> message = doReceive(timeout);...//在檢索到 Message 之后但在將其返回給調用者之前立即調用。 必要時可以修改消息if (interceptorStack != null && message != null) {message = interceptorList.postReceive(message, this);}//在接收完成后調用,而不管已引發的任何異常,從而允許適當的資源清理interceptorList.afterReceiveCompletion(message, this, null, interceptorStack);return message;}catch (RuntimeException ex) {...}}

最后的doReceive操作,其實大家都心知肚明了,就是從上面的隊列中直接讀取數據,代碼比較簡單,就不注釋了:

@Override@Nullableprotected Message<?> doReceive(long timeout) {try {if (timeout > 0) {if (this.queue instanceof BlockingQueue) {return ((BlockingQueue<Message<?>>) this.queue).poll(timeout, TimeUnit.MILLISECONDS);}else {return pollNonBlockingQueue(timeout);}}if (timeout == 0) {return this.queue.poll();}if (this.queue instanceof BlockingQueue) {return ((BlockingQueue<Message<?>>) this.queue).take();}else {Message<?> message = this.queue.poll();while (message == null) {this.queueSemaphore.tryAcquire(50, TimeUnit.MILLISECONDS); // NOSONAR ok to ignore resultmessage = this.queue.poll();}return message;}}catch (InterruptedException e) {Thread.currentThread().interrupt();return null;}}六,結語

能堅持看到這的,基本上都是勇士了。這一系列的執行過程其實還是比較繞的,我估計有些人看得也是云里霧里。其實我已經盡量精簡了許多,Spring-Integration其實涉及到的應用分支更多,我這也只是十分基礎的東西,我只能把我自己知道的先記錄下來。如果讓你對Spring-Integration產生了興趣,那本文的目的就達到了。這需要你自己去實地操作研究下,總是有收獲的。

以上就是源碼簡析Spring-Integration執行流程的詳細內容,更多關于Spring Integration執行的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
国产精品任我爽爆在线播放| 伊人久久高清| 国产一在线精品一区在线观看| 日本在线一区二区三区| 妖精视频成人观看www| 午夜精品久久久久久久久久蜜桃| 国产精品s色| 中文字幕一区二区三区在线视频| 日韩免费小视频| 久久中文欧美| 国产精品久久777777毛茸茸| 91欧美极品| 久久精品动漫| 国产一区二区三区天码| 国产精品资源| 日韩成人av影视| 蜜臀精品一区二区三区在线观看| 今天的高清视频免费播放成人| 国产精品伦理久久久久久| 国产精品18| 亚欧成人精品| 麻豆久久精品| 伊人精品一区| 激情综合自拍| 久久国产精品成人免费观看的软件| а√天堂8资源在线| 久久精品资源| 久久久精品国产**网站| 国产中文欧美日韩在线| 日韩高清中文字幕一区| 久久亚洲欧洲| 亚洲午夜黄色| 久久激情中文| 国产一区二区中文| 亚洲精品2区| 蜜桃av一区| 亚洲九九精品| 亚洲tv在线| 青草综合视频| 国产精品白浆| 六月丁香综合在线视频| 久久精品国产成人一区二区三区| 精品国产精品国产偷麻豆| 成人国产精品一区二区免费麻豆| 日韩欧美综合| 亚洲精品97| 国产亚洲精品美女久久| 国内揄拍国内精品久久| 九九精品调教| 亚洲一区二区三区无吗| 欧美日韩99| 精品国产亚洲一区二区三区在线 | 国内不卡的一区二区三区中文字幕| 精品国产欧美日韩一区二区三区| 国产一区二区三区精品在线观看| 欧美午夜精彩| 中文字幕亚洲精品乱码| 国产精品videossex久久发布| 日韩精品一区二区三区免费观影 | 久久精品国产成人一区二区三区| 在线日韩av| 在线精品一区| 久久影院午夜精品| 激情五月综合| 日韩欧美四区| 国产伊人久久| 自由日本语亚洲人高潮| 日韩精彩视频在线观看| 欧美精品三级在线| 国产精品久久久久蜜臀| 黄色成人在线网址| 欧美一区免费| 日韩久久视频| 宅男噜噜噜66国产日韩在线观看| 91成人在线精品视频| 久久一区欧美| 欧美sss在线视频| 蜜臀91精品一区二区三区| 麻豆精品一区二区综合av| 亚洲手机视频| 国产精品一区二区av交换| 一区二区三区视频免费观看| 日韩**一区毛片| 日韩欧美午夜| 亚洲精品麻豆| 激情久久99| 蜜臀精品一区二区三区在线观看| 精品久久国产一区| 国产高清久久| 国产成人免费| 欧美日韩精品一区二区三区在线观看| 久久久久网站| 久久精品国产999大香线蕉| 日韩欧美在线精品| 亚洲激情偷拍| 精品成人免费一区二区在线播放| 欧美精品成人| 日本欧美韩国一区三区| 视频一区二区欧美| 91精品综合| 日韩欧美三级| 成人亚洲欧美| 黄色在线观看www| 久草精品视频| 久久99精品久久久久久园产越南| 日韩欧美另类中文字幕| 亚洲激情不卡| 欧美a级片一区| 99久久婷婷这里只有精品| 亚洲成人不卡| 另类中文字幕国产精品| 天堂√8在线中文| 在线看片国产福利你懂的| 日韩av在线中文字幕| 国产第一亚洲| 高清av一区| 久久久久久自在自线| 精品久久免费| 精品国产aⅴ| 精品国产精品久久一区免费式 | 日韩精品一区二区三区av | 欧美午夜网站| 国产色噜噜噜91在线精品| 国产美女撒尿一区二区| 国产精品高清一区二区| 免费一级欧美片在线观看网站| 国产精品欧美三级在线观看| 国产日韩欧美三级| 国产伦理一区| 麻豆久久久久久久| 国产夫妻在线| 91精品久久久久久久久久不卡| 99久久久久久中文字幕一区| 国产精品黑丝在线播放| 91精品xxx在线观看| 久久久夜精品| 好吊日精品视频| 蜜桃视频一区二区三区在线观看| 视频精品一区| 国产精品chinese| 日本久久精品| 久久国产电影| 免费在线观看精品| 日韩av电影一区| 麻豆精品99| 黄色aa久久| 香蕉国产精品| 婷婷综合成人| 国产欧美高清视频在线| 精品一区二区三区视频在线播放| 国产一区不卡| 999久久久免费精品国产| aⅴ色国产欧美| 日韩av资源网| 成人台湾亚洲精品一区二区 | 亚洲激情中文在线| 亚洲精品韩国| 欧美aaaaaa午夜精品| 日韩免费高清| 国产免费成人| 欧美日韩亚洲一区在线观看| 国产精品视频一区二区三区综合| 色爱综合网欧美| 99视频精品| 国产日韩欧美| 亚洲91久久| 日韩一区二区三区高清在线观看| 里番精品3d一二三区| 欧美日韩中文字幕一区二区三区 | 欧美在线日韩| 欧美日韩免费观看视频| 日韩中文字幕一区二区三区| 国产精品成人自拍| | 欧美福利一区| 日本综合精品一区| 97精品国产福利一区二区三区| 五月天久久久| 国产精品高清一区二区| 在线日韩av| 久久精品 人人爱| 99精品国产一区二区三区| 日本不卡视频在线观看| 国产盗摄——sm在线视频| 视频在线观看91| 精品精品99| 亚洲在线久久| 色黄视频在线观看| 蜜臀av亚洲一区中文字幕| 欧美国产极品| 99国产一区| 久久影视三级福利片| 99国产精品99久久久久久粉嫩| 国产精品欧美三级在线观看| 亚洲成人免费| 欧美国产先锋| 亚洲制服少妇| 成人亚洲一区| 日韩动漫一区| 免费精品国产的网站免费观看|