Posts

Showing posts with the label Future

Callout Limits For Future Methods And Queueable Apex

Image
Answer : I quickly wrote a class with future method to test this behaviour. public class FutureClassLimitsTest { @future(callout=true) public static void docallouts(){ for(Integer i=0;i<200;i++){ Http http=new Http(); HttpRequest hr=new HttpRequest(); hr.setMethod('POST'); hr.setEndpoint('https://google.com'); http.send(hr); } } } This is the error I get. First error: Too many callouts: 101. So I believe this is uniform for the whole platform irrespective of the transaction(Sync or Async). Is there a workaround? Well unless there is a strong business requirement it is kinda bad doing so many callous in a transaction. We can call a Queauble Method from Queueable method,.. and you can chain them infinitely. Thus using proper implementation you can have 100+ callouts. Batch is another way to tackle this. You have to write /modify data model add a some queue cus...

Convert A Java Future To A Scala Future

Answer : How about just wrapping it (I'm assuming there's an implicit ExecutionContext here): val scalaFuture = Future { javaFuture.get } EDIT: A simple polling strategy could look like this (java.util.Future => F): def pollForResult[T](f: F[T]): Future[T] = Future { Thread.sleep(500) f }.flatMap(f => if (f.isDone) Future { f.get } else pollForResult(f)) This will check if the Java future is done every 500ms. Obviously the total blocking time is the same as above (rounded up to the nearest 500ms) but this solution will allow other tasks to be interleaved in the same thread of the ExecutionContext . Starting Scala 2.13 , the standard library includes scala.jdk.FutureConverters which provides Java to Scala CompletableFuture/Future implicit conversions: import scala.jdk.FutureConverters._ // val javaFuture = java.util.concurrent.CompletableFuture.completedFuture(12) val scalaFuture = javaFuture.asScala // scalaFuture: scala.concurrent.Future[Int] = Future(...