Reputation: 1362
I am a newbie in the spring integration framework. Below is my code, I am actually trying to make some HTTP calls using HTTP outbound gateway using SI DSL configuration. When I ran the code the IntegrationFlow
methods are called but the HTTP hit is not making. I am not sure why.
Main class
@EnableIntegration
@Configuration
@Import({ AptHttp.class })
public class DemosiApplication {
public static void main(String[] args) {
SpringApplication.run(DemosiApplication.class, args);
}
}
config 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 ). I have another application where I can have some API through the spring integration code I am trying to hit that API method. To understand the flow of HTTP outbound gateway I am trying this way.
Could anyone please help/ suggest me on this.
Upvotes: 0
Views: 1441
Reputation: 1362
I am sharing my answer those who are looking for an example for HTTP outbound gateway using java DSL .
Main 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.
Upvotes: 0
Reputation: 121177
You don't show (or don't have) the code which sends messages into the inputChannel
.
The Http.outboundGateway()
is not an active components and its work has to be triggered by the request message.
Also there are two main phases in Spring application context: bean creation and runtime.
So, you see that System.out.println()
during bean creation phase. It has nothing to do with runtime when really a send over HTTP happens.
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. Only after that your HTTP Outbound Gateway is going to be triggered.
See more info in samples: https://github.com/spring-projects/spring-integration-samples
Upvotes: 2