简体   繁体   English

Http 出站网关正在工作但未拨打电话 - 使用 spring 集成 DSL java

[英]Http outbound gateway is working but not making the call - using spring integration DSL java

I am a newbie in the spring integration framework.我是 spring 集成框架的新手。 Below is my code, I am actually trying to make some HTTP calls using HTTP outbound gateway using SI DSL configuration.下面是我的代码,我实际上是在尝试使用 HTTP 出站网关使用 SI DSL 配置进行一些 HTTP 调用。 When I ran the code the IntegrationFlow methods are called but the HTTP hit is not making.当我运行代码时, IntegrationFlow方法被调用,但 HTTP 没有命中。 I am not sure why.我不确定为什么。

Main class主class

@EnableIntegration
@Configuration
@Import({ AptHttp.class })
public class DemosiApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemosiApplication.class, args);
    }

}

config class配置 class

@Configuration
@IntegrationComponentScan
public class AptHttp {

    @EnableIntegration
    public static class ContextConfiguration {

        @Bean("inputChannel")
        public MessageChannel inputChannel() {
            return MessageChannels.direct().get();
        }

        @Bean
        public MessageChannel outputChannel() {
            return MessageChannels.direct().get();
        }

        @Bean
        public IntegrationFlow outBoundFlow() {
            System.out.println("Inside t outBoundFlow flow ");
            final String uri = "http://localhost:9090/api/test";
            return f -> f.channel(inputChannel())
                    .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.GET).expectedResponseType(String.class))
                    .channel(outputChannel());
        }

    }

}

Above two classes only.仅限以上两个班。 I don't get any error too when I ran the SI application (sysout are printing but the call is not made I don't know why ).当我运行 SI 应用程序时,我也没有收到任何错误(sysout 正在打印,但没有拨打电话,我不知道为什么)。 I have another application where I can have some API through the spring integration code I am trying to hit that API method.我有另一个应用程序,我可以通过 spring 集成代码获得一些 API 我正在尝试使用 API 方法。 To understand the flow of HTTP outbound gateway I am trying this way.为了了解 HTTP 出站网关的流程,我正在尝试这种方式。

Could anyone please help/ suggest me on this.谁能帮助/建议我。

You don't show (or don't have) the code which sends messages into the inputChannel .您没有显示(或没有)将消息发送到inputChannel的代码。

The Http.outboundGateway() is not an active components and its work has to be triggered by the request message. Http.outboundGateway()不是活动组件,它的工作必须由请求消息触发。

Also there are two main phases in Spring application context: bean creation and runtime. Spring 应用程序上下文中还有两个主要阶段:bean 创建和运行时。 So, you see that System.out.println() during bean creation phase.因此,您会在 bean 创建阶段看到System.out.println() It has nothing to do with runtime when really a send over HTTP happens.当真正通过 HTTP 发生发送时,它与运行时无关。

So, after crating and starting an application context ( SpringApplication.run(DemosiApplication.class, args); ) you need to take an inputChannel bean and send a Message<?> into it.因此,在创建并启动应用程序上下文( SpringApplication.run(DemosiApplication.class, args); )之后,您需要获取一个inputChannel bean 并向其中发送Message<?> Only after that your HTTP Outbound Gateway is going to be triggered.只有在那之后,您的 HTTP 出站网关才会被触发。

See more info in samples: https://github.com/spring-projects/spring-integration-samples在示例中查看更多信息: https://github.com/spring-projects/spring-integration-samples

I am sharing my answer those who are looking for an example for HTTP outbound gateway using java DSL.我正在分享我的答案,那些正在寻找使用 java DSL 的 HTTP 出站网关示例的人。

Main class主class

@SpringBootApplication(scanBasePackages="com.testsi")
@EnableIntegration
@IntegrationComponentScan
public class DemosiApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemosiApplication.class, args);
    }



        @Bean
        @Qualifier("get_send_channel")
        MessageChannel getSendChannel() {
            return MessageChannels.direct().get();
        }

        @Bean
        @Qualifier("get_receive_channel")
        PollableChannel getReceiveChannel() {
         return new PriorityChannel() ;
        }

        @Bean
        public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
            System.out.println("inside the command liner ");
            return args -> {
                Message<?> message = MessageBuilder.withPayload("").build();
                System.out.println("Message [ayload =>"+ message.getPayload());
                getSendChannel().send(message);
                System.out.println("we are getting an output from test application"+getReceiveChannel().receive().getPayload());
            };
        }

        @Bean
        public IntegrationFlow outBoundFlow() {
            System.out.println("Inside t outBoundFlow flow ");
            final String uri = "http://localhost:9090/api/test/1";
            return IntegrationFlows.from(getSendChannel())
                    .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.POST).expectedResponseType(String.class))
                    .channel(getReceiveChannel()).get();
        }

        @Bean
        public IntegrationFlow outBoundFlow() {
            System.out.println("Inside t outBoundFlow flow ");
            final String uri = "http://localhost:9090/api/test";
            return IntegrationFlows.from(getSendChannel())
                    .handle(Http.outboundGateway(uri).httpMethod(HttpMethod.GET).expectedResponseType(String.class))
                    .channel(getReceiveChannel()).get();
        }
        }

Thanks @Artem Bilan.谢谢@Artem Bilan。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Integration Java DSL - Http Outbound Gateway uri 变量表达式 - Spring Integration Java DSL - Http Outbound Gateway uri variable Expression Spring集成dsl:http出站网关 - Spring integration dsl: http outbound gateway Spring Integration Java DSL - 如何调用int-http:outbound-gateway? - Spring Integration Java DSL - How to invoke int-http:outbound-gateway? Spring Integration DSL - 可以访问标头的出站网关 - Spring Integration DSL - Outbound Gateway with access to Headers Spring Integration-网址变量在http:outbound-gateway中不起作用 - Spring Integration - url-variable not working in http:outbound-gateway 在 Spring Integration 中使用 Transformer 轮询 HTTP 服务(出站网关)和流程 - Polling HTTP Service(Outbound Gateway) & process using Transformer in Spring Integration 将文件/文件从远程目录移动到另一个 Spring 集成 SFTP 出站网关 Java 配置/Java DSL - Moving file/files from remote directory to another Spring Integration SFTP Outbound Gateway Java Config/Java DSL Spring Integration Http Outbound Gateway Header Mapper - Spring Integration Http Outbound Gateway Header Mapper 在 Spring Integration 中使用 Http Outbound Gateway 进行错误处理 - Error handling with Http Outbound Gateway in Spring Integration Spring Integration Java DSL中的JPA出站通道适配器配置 - JPA outbound channel adapter config in Spring Integration Java DSL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM