Docker Image To Simulate Long Delay API Calls

Recently I worked on Spring RestTemplate and the Asynchronous feature of the Spring framework. Thus, I need to create some sample APIs for the application.

I tried a few searches but couldn’t find one so I created one and packaged/pushed to docker hub.

Now, you have a Docker image that provides some API endpoints which you can specify how long it takes to return the result.

Let’s find out how.

Run a docker container instance

First thing first, you need to run a docker container for the service to be online:

docker run -p 8282:8181 codingpuss/long-api:0.0.1

As you can see, I mapped port 8282 to the container’s port 8181. You can change 8282 to whatever you want (as long as the port is valid).

It’s time to try the API out.

Call APIs with a delay

The syntax to call the endpoints with delay is this:

http://localhost:8282/users/{delay}/random

This API endpoint returns one random user.

The following returns a random list of users with a specified size:

http://localhost:8080/users/{delay}/{count}/random-list

Let’s try them out

Calling api with a 2 seconds delay
Calling api with a 2 seconds delay

As you can see, in this request, I specified a 2 seconds delay (2000 milliseconds).

This call instead returns a list of 5 random users, also after 2 seconds:

Return random list of users after 2 seconds
Return random list of users after 2 seconds

Sure enough, you can change the delay and the count of users to different numbers if you prefer.

Conclusion

Hopefully, this docker image can help you quickly set up a long-waiting API and you can focus on working with your application instead.

Leave a Comment