alex
alex

Reputation: 2213

Simple (preferably OO) method to make parallel/concurrent API calls using Scala?

My application now requires me to make several thousand API calls every minute to check for updates. I've never done anything like this before and am very lost / struggling to get started. I've looked up all sorts of frameworks/libraries that might be able to help. I've looked at one of the most recommended HTTP4S but am utterly confused since I've never done any functional programming and I don't have time right now to learn this. I don't understand any of the examples and I'm very hesitant to use what I can't understand. I've also looked at an Akka library, STTP, and play ws but again pretty confused by the examples.

I guess I'm just asking for some help, maybe an example using a simple library to make parallel API calls.

EDIT Adding some semi-Pseudo Code Below of what I'm trying to do

import scala.collection.mutable.ListBuffer 

object TestObject {

  def main (args: Array[String]): Unit = {

    val customers: ListBuffer[Customer] = DBSingleton.getCustomers

    for(customer <- customers) {
      val response = doApiCall(customer)
      if(response == "Yes"){
        doTheOtherThingForThisCustomer(customer)
      }
    }
  }
}

But instead of waiting for each response from the API on doApiCall, I'd like to call it for each customer at the same time.

Upvotes: 1

Views: 473

Answers (1)

jwvh
jwvh

Reputation: 51271

It looks like doTheOtherThing() doesn't return anything meaningful, so you could just do this:

import concurrent.Future

...
for (customer <- customers) Future {
  val response = doApiCall(customer)
  if(response == "Yes"){
    doTheOtherThingForThisCustomer(customer)
  }
}

Upvotes: 1

Related Questions