Skip to content

Multithreaded & Asynchronous Spring Boot and Java 8 based REST implementation for counting the images in the given Urls

Notifications You must be signed in to change notification settings

RathaKM/url-imagecount-service

Repository files navigation

Url Image Count Service

This repo contains working code for a Url Image Count service (SpringBoot with Java8).

There are two different implementations provided in this application.

Implementation I (for Live Urls)

  • In this implementation the application provides a REST API (/v1/imagecount) that will accept a list of live URLs as JSON input. This call would return a Job Id immediately after invoked.
  • The application’s back-end would then get the html contents for each URL and then count the total number of images found in that content by parsing the <img src="" > tag using regular expression. This would be done asynchronously.
  • The application also provides another REST API (v1/imagecount/jobId/{jobId}) with a Job ID as path parameter.
    • This API will return a JSON response with the number of images found in the contents of each URL supplied for that job id.
    • The total image count for each url content will be displayed if that particular url request is completed
    • Otherwise the status 'Pending' will be displayed
  • The urls in the request payload could be any live urls.
  • This implementation uses Executor interface for multithreading and CompletableFuture for asynchronous programming for processing the urls, parsing and counting the images.

Implementation II (for Demo Urls)

  • This implementation provides the REST endpoint (/v1/imagecount/demourl) that will accept a list of demo urls as JSON input. This call would also return a Job Id immediately after invoked.
  • For the purpose of demonstration this application also contains REST API for providing the demo Url contents. The contents available thro' the uri's '/v1/url1/image, /v1/url2/image and /v1/url3/image'. The response of these urls are not html contents. They are JSON response with tags.
  • The same REST endpoint (v1/imagecount/jobId/{jobId}) with a Job ID as path parameter can be used to retrieve the job details.
    • This will also return a JSON response with the number of images found in the contents of each URL supplied for that job id.
    • The total image count for each url content will be displayed if that particular url request is completed
    • Otherwise the status 'Pending' will be displayed
  • The urls in the request payload could be any of the three demo (url1, url2, url3) urls.
  • This one uses @Async of Spring for url processing and the image count.

Project Features

  • Multithreaded & Asynchronous Spring Boot and Java 8 based REST implementation
  • Layered approach
  • Dependency Injection using Spring
  • Input data and Service validation
  • Testing
    • Unit Testing
    • Curl
    • Postman

Technology Stack

Description Tool/Framework
Java version JDK 1.8
JAX-RS Implementation Spring Boot
IDE IntelliJ IDEA 14
Build tool Gradle
Platform iOS

Application Design

alt txt

UrlImageCount Service Resources

Resource Type Resource URI HTTP Method
Create a Job (for Live Urls) /v1/imagecount POST
Create a Job (for Demo Urls) /v1/imagecount/demourl POST
Get a Job Detail /v1/imagecount/jobId/{jobId} GET
Get Url1 content /v1/url1/image GET
Get Url2 content /v1/url2/image GET
Get Url3 content /v1/url3/image GET

Project Setup

Service Setup

  • Clone/fork this Repo and open/import this Project into IntelliJ or Eclipse
  • Open a Terminal window and go to the project directory, 'url-imagecount-service'
  • Build/Package the project using 'gradle build' command (use gradle build -x test to skip the test)
  • Start SpringBoot application using 'java -jar build/libs/imagecount-service-0.1.0.jar' command
  • Test the server by http://localhost:8080/v1/imagecount/jobId/1. This may display {"errorMessage":"JobId 1 is not found"}, as you have not created any Job already.
  • For a quick deployment you can use the shared imagecount-service-0.1.0.jar file, in case you didn't have time or ran into any issues.
    • Simply go to the root folder and run the command 'java -jar build/libs/imagecount-service-0.1.0.jar'

How to Consume/Test the Resources

There are 2 ways you can consume these resources. They are by using Postman, and Curl command.

Using Postman

The resources can be consumed by using below Postman collection

For Live Urls

Create A Job for Live Url Image Count processing

alt txt

Retrive A Job Detail which is Pending for some Live Url

alt txt

Retrive A Job Detail which is fully completed for Live Url

alt txt

For Demo Urls

Create A Job for Demo Url Image Count processing

alt txt

Retrive A Job Detail which is Pending for some Demo Url

alt txt

Retrive A Job Detail which is fully completed for Demo Url

alt txt

Demo Service Urls

Retrieve Url1 content

alt txt

Retrieve Url2 content

alt txt

Retrieve Url3 content

alt txt

Validation

Retrieve Job Detail - Invalid Job Id

If the JobId is not available then an error message will be displayed

{
    "errorMessage": "JobId 11 is not found"
}

Using Curl command

We can test the resources using below Curl command. The data files are available in the root folder [liveurl_requestX.json or demourl_requestX.json]

Please make sure that the Application is running before running this command.

Create a Job (HTTP POST call)

  • Command: You can try with different data file (demourl_request2.json, demourl_request3.json)
    curl -H "Content-Type:application/json" -X POST -d @demourl_request1.json http://localhost:8080/v1/imagecount/demourl
    
  • response
    {
            "jobId":"2",
            "imageCountUrls":[
                  {
                    "url":"http://localhost:8080/v1/url1/image",
                    "imageCount":"Pending"
                  },
                  {
                    "url":"http://localhost:8080/v1/url2/image",
                    "imageCount":"Pending"
                  }
              ],
              "_links":{
                  "self":{
                      "href":"http://localhost:8080/v1/imagecount"
                   },
                   "get":{
                      "href":"http://localhost:8080/v1/imagecount/jobId/2"
                    },
                   "update":{
                      "href":"http://localhost:8080/v1/imagecount/jobId/2"
                    },
                   "delete":{
                      "href":"http://localhost:8080/v1/imagecount/jobId/2"
                    }
                }
            }
    

Retrieve a Job (HTTP GET call)

  • Command: curl -H "Content-Type:application/json" -X GET http://localhost:8080/v1/imagecount/jobId/1
  • Response
    {
    "jobId": "1",
    "imageCountUrls": [
      {
        "url": "http://localhost:8080/v1/url1/image",
        "imageCount": "60"
      },
      {
        "url": "http://localhost:8080/v1/url2/image",
        "imageCount": "130"
      }
    ],
    "_links": {
      "self": {
        "href": "http://localhost:8080/v1/imagecount/jobId/1"
      },
      "update": {
        "href": "http://localhost:8080/v1/imagecount/jobId/1"
      },
      "delete": {
        "href": "http://localhost:8080/v1/imagecount/jobId/1"
      }
    }
    

Unit Tests

The unit tests are added mainly for Controller, and Service layers. These tests will be run as part of the 'gradle build' command

About

Multithreaded & Asynchronous Spring Boot and Java 8 based REST implementation for counting the images in the given Urls

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages