【问题标题】:How to start an asychronous gateway from a Spring RestController then pass a message to the next channel?如何从 Spring RestController 启动异步网关,然后将消息传递到下一个通道?
【发布时间】:2025-12-17 06:50:01
【问题描述】:

您好,我正在使用 Spring 集成并将网关注入到 RestController

<int:gateway id="reportsService"
    service-interface="me.service.MyReportingGateway"
    default-request-channel="reportsRequestChannel" />

<int:channel id="reportsRequestChannel" >
    <int:dispatcher task-executor="reportServiceExecutor" /></int:channel>

<task:executor id="reportServiceExecutor" pool-size="5" />

<int:service-activator input-channel="reportsRequestChannel" output-channel="reportRouterChannel">
    <bean id="reportServiceExecutor" class="me.service.SpecReporting"/>
</int:service-activator>

<int:channel id="reportRouterChannel" />

<int:router input-channel="reportRouterChannel" method="routeReport" ref="reportRouter"></int:router>

<bean class="me.service.ReportRequestRouter" id="reportRouter">
</bean>

  <int:channel id="report01Channel">
    <int:queue capacity="10"></int:queue>
  </int:channel>

  <int:channel id="report02Channel">
    <int:queue capacity="10"></int:queue>
  </int:channel>

  <!--  for the channels -->
  <int:poller default="true" fixed-delay="1000" id="poller"></int:poller>

  <int:service-activator input-channel="report01Channel"
    output-channel="CallbackChannel" ref="filewriter" method="writeFile">
  </int:service-activator>
  <int:service-activator input-channel="report02Channel"
    output-channel="CallbackChannel" ref="filewriter" method="writeFile">
  </int:service-activator>

  <bean class="me.service.CsvFileWriter" id="filewriter"></bean>

  <int:channel id="CallbackChannel" />

  <int-http:outbound-gateway id="httpOutbound"
    request-channel="CallbackChannel"
    http-method="GET"
    url="http://localhost:8080/services/ping" />

我的 RestController 注入网关:

@Autowired
MyReporting reportsService;

并调用控制器方法内的方法

HttpStatus httpStatus = HttpStatus.OK;
ReportingResponse arr = new ReportingResponse();
AppError ae = new AppError();
ae.setCode("ANP-SM-1000");
ae.setDescription("Everything Criss!!");
arr.setErrors(ae);

logger.debug("Invoking: reportsService.createReport...");
// Async - this will start the report generation process
reportsService.createReport(reportId,rInput);

return new ResponseEntity<ReportingResponse>(arr, httpStatus);

由于我配置了一个带有 void return 的 executor-executor 通道,我如何在这个线程中继续到下一个通道,即 reportRouterChannel?

【问题讨论】:

    标签: spring asynchronous spring-integration integration gateway


    【解决方案1】:

    Spring Boot 提供了一个很好的处理方式。添加@Async注解会异步执行你的方法。

    只需将您的方法包装在 async

    HttpStatus httpStatus = HttpStatus.OK;
    ReportingResponse arr = new ReportingResponse();
    AppError ae = new AppError();
    ae.setCode("ANP-SM-1000");
    ae.setDescription("Everything Criss!!");
    arr.setErrors(ae);
    
    logger.debug("Invoking: reportsService.createReport...");
    asyncMethod(reportId,rInput);
    
    return new ResponseEntity<ReportingResponse>(arr, httpStatus);
    
    @Async
    public void asyncMethod(String reportId, File rInput){
    reportsService.createReport(reportId,rInput);
    
    }
    

    不要忘记将@EnableAsync 添加到@Configuration 类之一。 春天会为你做所有的魔法。

    【讨论】:

      最近更新 更多