1

I have an Enterprise Service Bus (ESB) that posts Data to Microservices (MCS) via Rest. I use Spring to do this. The main Problem is that i have 6 Microservices, that run one after one. So it looks like this: MCS1 -> ESB -> MCS2 -> ESB -> ... -> MCS6

So my Problem looks like this: (ESB)

@RequestMapping(value = "/rawdataservice/container", method =  RequestMethod.POST)
@Produces(MediaType.APPLICATION_JSON)
public void rawContainer(@RequestBody Container c)
{
    // Here i want to do something to directly send a response and afterwards execute the 
    // heavy code
    // In the heavy code is a postForObject to the next Microservice
}

And the Service does something like this:

@RequestMapping(value = "/container", method = RequestMethod.POST)
public void addDomain(@RequestBody Container container)
{
    heavyCode();
    RestTemplate rt = new RestTemplate();
    rt.postForObject("http://134.61.64.201:8080/rest/rawdataservice/container",container, Container.class);
}

But i dont know how to do this. I looked up the post for Location method, but i dont think it would solve the Problem.

EDIT: I have a chain of Microservices. The first Microservice waits for a Response of the ESB. In the response the ESB posts to another Microservice and waits for a response and the next one does the same as the first one. So the Problem is that the first Microservice is blocked as long as the complete Microservice Route is completed.

ESB Route Maybe a picture could help. 1.rawdataService 2.metadataservice 3.syntaxservice 4.semantik

NemesisFLX
  • 23
  • 1
  • 7
  • I am not sure I understand the question. Is it that you are unable to determine where to post from microservice – Sangram Jadhav Jun 09 '16 at 10:13
  • It looks to me that you wish to run your requests async so that your requests are not blocked. Assuming that you have two Microservices A + B you can initiate requests in parallel / async to both Microservices A + B. However, that is not always possible i.e. in cases where response from A is needed as input for B. – softius Jun 09 '16 at 10:42
  • Async would help to run the request without blocking but the Main Problem of the chain is not solved. I dont want the chain and it is not necessary for the System. Microservice A dont need the information that microservice B completed the Process. Microservice A only needs the information that the Data is sucessfully send to the ESB not more – NemesisFLX Jun 09 '16 at 11:00

1 Answers1

0
// Here i want to do something to directly send a response and afterwards execute the 
// heavy code

The usual spelling of that is to use the data from the http request to create a Runnable that knows how to do the work, and dispatch that runnable to an executor service for later processing. Much the same, you copy the data you need into a queue, which is polled by other threads ready to complete the work.

The http request handler then returns as soon as the executor service/queue has accepted the pending work. The most common implementation is to return a "202 Accepted" response, including in the Location header the url for a resource that will allow the client to monitor the work in progress, if desired.

In Spring, it might be ResponseEntity that manages the codes for you. For instance

ResponseEntity.accepted()....

See also:

From the caller's point of view, it would invoke RestTemplate.postForLocation, receive a URI, and throw away that URI because the microservice only needs to know that the work as been accepted

Side note: in the long term, you are probably going to want to be able to correlate the activities of the different micro services, especially when you are troubleshooting. So make sure you understand what Gregor Hohpe has to say about correlation identifiers.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • That could solve the Problem. My last remaining question is how i return a "202 Accepted" with spring. Normally Spring does the http response for me. – NemesisFLX Jun 09 '16 at 13:07