paulwong

          My Links

          Blog Stats

          常用鏈接

          留言簿(67)

          隨筆分類(1390)

          隨筆檔案(1148)

          文章分類(7)

          文章檔案(10)

          相冊

          收藏夾(2)

          AI

          Develop

          E-BOOK

          Other

          養生

          微服務

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          60天內閱讀排行

          SPRING INTEGRATION ERROR HANDLING

          https://github.com/zakyalvan/spring-integration-java-dsl-learn

          package com.jwebs.learn.errorhandling;

          import java.util.Random;

          import javax.jms.ConnectionFactory;

          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.beans.factory.annotation.Qualifier;
          import org.springframework.boot.autoconfigure.SpringBootApplication;
          import org.springframework.boot.builder.SpringApplicationBuilder;
          import org.springframework.context.ConfigurableApplicationContext;
          import org.springframework.context.annotation.Bean;
          import org.springframework.integration.annotation.IntegrationComponentScan;
          import org.springframework.integration.channel.PublishSubscribeChannel;
          import org.springframework.integration.core.MessageSource;
          import org.springframework.integration.dsl.IntegrationFlow;
          import org.springframework.integration.dsl.IntegrationFlows;
          import org.springframework.integration.dsl.core.Pollers;
          import org.springframework.integration.dsl.jms.Jms;
          import org.springframework.integration.support.MessageBuilder;
          import org.springframework.messaging.MessagingException;

          /**
           * Show how to handle error in spring integration flow.
           * Please note, errorChannel in spring integration only applicable to
           * error thrown in asynch component.
           * 
           * 
          @author zakyalvan
           
          */
          @SpringBootApplication
          @IntegrationComponentScan
          public class ErrorHandlingApplication {
              public static void main(String[] args) throws Exception {
                  ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(ErrorHandlingApplication.class)
                          .web(false)
                          .run(args);
                  
                  Runtime.getRuntime().addShutdownHook(new Thread(() -> applicationContext.close()));
                  
                  System.out.println("Pres enter key to exit");
                  System.in.read();
                  System.exit(0);
              }

              @Autowired
              private ConnectionFactory connectionFactory;
              
              @Bean
              public MessageSource<Integer> randomIntegerMessageSource() {
                  return () -> MessageBuilder.withPayload(new Random().nextInt()).build();
              }
              
              @Bean
              public IntegrationFlow withErrorFlow() {
                  return IntegrationFlows.from(randomIntegerMessageSource(), spec -> spec.poller(Pollers.fixedDelay(1000)))
                              .handle(Jms.outboundGateway(connectionFactory)
                              .requestDestination("processor.input")
                              .replyContainer(spec -> spec.sessionTransacted(true)))
                              .get();
              }
              
              @Autowired
              @Qualifier("errorChannel")
              private PublishSubscribeChannel errorChannel;
              
              @Bean
              public IntegrationFlow errorHandlingFlow() {
                  return IntegrationFlows.from(errorChannel)
                          .handle(message -> System.out.println("@@@@@@@@@@@@@@@@@@@@@" + ((MessagingException) message.getPayload()).getFailedMessage().getPayload()))
                          .get();
              }
          }

          posted on 2020-01-10 15:32 paulwong 閱讀(355) 評論(0)  編輯  收藏 所屬分類: SPRING INTERGRATION

          主站蜘蛛池模板: 宁波市| 黔江区| 南汇区| 兴业县| 贡山| 石林| 龙泉市| 通江县| 清流县| 南澳县| 广河县| 二手房| 扎鲁特旗| 定南县| 庆阳市| 隆化县| 孙吴县| 娄烦县| 闻喜县| 岫岩| 庆阳市| 鹤山市| 阿尔山市| 苏尼特左旗| 金湖县| 正安县| 合肥市| 万山特区| 石首市| 阿拉尔市| 苍梧县| 多伦县| 西充县| 乌拉特前旗| 得荣县| 绥江县| 武强县| 高邮市| 永丰县| 宁海县| 庄河市|