How to use Spring Integration Http Outbound Gateway for Polling a http end point with GET method

Spring Integration provides Http Outbound Components for polling a Http Endpoints with: Http Outbound Gateway & Http Outbound Adapter.
Http Outbound adapter: just send requests to an http endpoint.
– But Http Outbound gateway: send requests to an http endpoint then return responses.
In the tutorial, JavaSampleApproach will guide you how to poll a http end point with Get request by Http Outbound Gateway.

Related Posts:
1. How to use Spring Integration Http Inbound with Spring Boot
2. How to start Spring Integration with Spring Boot

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Boot: 1.5.1.RELEASE
– Spring Tool Suite – Version 3.8.1.RELEASE

II. Overview

The tutorial creates 2 projects: Http OutBound Gateway & simple RestfulApi Server with a GET request mapping.

1. Project Structure

Spring Integration Http Outboud Gateway - structure project

2. Step to do

– Create Spring Boot projects
– Create a simple data model
– Configure Http Outbound Gateway
– Create a Test case for HTTP Outbound Gateway
– Create a simple RestController for Server
– Run & Check Result

III. Practice
1. Create Spring Boot projects

– Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input project info. Press Next then Finish, a Spring Boot project will be created successfully.

For Http Outbound Gateway, add dependencies:
spring-boot-starter-integration
spring-boot-starter-web
spring-integration-http


          org.springframework.boot
          spring-boot-starter-integration
      
		
	org.springframework.boot
	spring-boot-starter-web


    org.springframework.integration
    spring-integration-http

For RestfulApi Server, add dependencies:
spring-boot-starter-web


	org.springframework.boot
	spring-boot-starter-web

2. Create a simple data model
package com.javasampleapproach.springsecurity.model;

public class ServerMsg {
	
	private String welcomeMsg = "Welcome To Spring Integration Http Outbound Example!";
	private String currentTime;
	
    public ServerMsg(){
    }
     
    public ServerMsg(String currentTime){
        this.setCurrentTime(currentTime);
    }

	public String getWelcomeMsg() {
		return welcomeMsg;
	}

	public void setWelcomeMsg(String welcomeMsg) {
		this.welcomeMsg = welcomeMsg;
	}

	public String getCurrentTime() {
		return currentTime;
	}

	public void setCurrentTime(String currentTime) {
		this.currentTime = currentTime;
	}
     
    public String toString(){
        String info = String.format("ServerMsg with welcomeMsg = %s, currentTime = %s", welcomeMsg, currentTime);
        return info;
    }
    
}
3. Configure Http Outbound Gateway

Use HTTP Namespace Support, we define 2 channel: reply.channel & get.request.channel
expected-response-type is used to define type of message’s Payload.




	
		
	
	
	
	


4. Create a Test case for HTTP Outbound Gateway

@Autowired @Qualifier("reply.channel") PollableChannel receivedChannel for received channel.
@Autowired @Qualifier("get.request.channel") MessageChannel getRequestChannel for request channel.
Use MessageBuilder to build a message with empty payload.

package com.javasampleapproach.springsecurity;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.PollableChannel;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.javasampleapproach.springsecurity.model.ServerMsg;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:http-outbound.xml")
@SpringBootTest
public class SpringIntegrationOutboundComponentApplicationTests {

	@Autowired @Qualifier("reply.channel") PollableChannel receivedChannel;
	@Autowired @Qualifier("get.request.channel") MessageChannel getRequestChannel;

	@Test
	public void testCase() {
		Message message = MessageBuilder.withPayload("").build();
		getRequestChannel.send(message);
		Message receivedMsg = receivedChannel.receive();
		ServerMsg serverMsg = (ServerMsg) receivedMsg.getPayload();
		System.out.println("############## ServerMsg ##############");
		System.out.println(serverMsg.toString());
		System.out.println("############## Done! ##############");
	}
}
5. Create a simple RestController for Server

Create WebController with a GET requestmapping.

package com.javasampleapproach.restfulapi.controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.javasampleapproach.restfulapi.model.ServerMsg;

@RestController
public class WebController {
	
	@RequestMapping("/getServerTime")
	public ServerMsg getServerTime(){
		
		System.out.println("# GET Request!");
		
		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
		LocalDateTime now = LocalDateTime.now();
		
		ServerMsg serverTime = new ServerMsg(dtf.format(now));
		return serverTime;
	}
}
6. Run & Check Result

Build the projects with Spring Boot App mode.
– Run RestfulApi Server
– Then run testcase for HTTP Outbound Gateway project.
Logs:

############## ServerMsg ##############
ServerMsg with welcomeMsg = Welcome To Spring Integration Http Outbound Example!, currentTime = 2017/02/11 17:51:43
############## Done! ##############
IV. Sourcecode

SpringRestfulApi
SpringIntegrationOutboundComponent

0 0 votes
Article Rating
Subscribe
Notify of
guest
949 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments