Scala: match on value

IF you want to match on a value in Scala you can do so simply. Here, I’m using it to handle commands from RabbitMQ in this format: index, update,,12345 Here is the code: val items = message.split(",") val command = items(0);   command match { case "index" => { val domain = items(1) … } […]

Using RabbitMQ with Scala Futures

RabbitMQ and Futures provide related, but different functionality: RabbitMQ allows persistent storage for a queue of events, and Futures allow you to delegate lambda functions to a thread pool. To make this concrete, lets say we make a function that reads a task from RabbitMQ: def doSomething { val factory = new ConnectionFactory() factory.setHost("") factory.setUsername("guest") […]

Scala: infinite duration / timeout for Futures

For whatever reason, a lot of blog posts I’ve seen put timeouts on Futures, and it took me some time to find the “infinite” one. If you want to do this, here is what you need: import scala.concurrent.duration.Duration   val tasks = for (thread <- 1 to 10) yield Future { doSomething }   val […]

Using RabbitMQ in Scala

RabbitMQ is a a great place to stash the state of long running scripts. It has APIs for basically every language, so you could conceivably write different steps in a batch job in different languages, to take advantages of strong languages in each. I also like that this allows you to kill a script and […]

Creating a thread pool in Scala

Part of the magic of Scala is that you can easily parallelize operations with features like Futures and parallel collections. When you do this, scala creates a thread pool, so that it can allocate appropriate resources. If you want to control this, you can make your own thread pool by copying the following code. This […]

Scala: Cannot find an implicit ExecutionContext, either require one yourself or import

If you use a scala future, you can hit this error: Cannot find an implicit ExecutionContext, either require one yourself or import If you get this error, you can fix it by pasting this in right before the code using futures: implicit val ec = new ExecutionContext { val threadPool = Executors.newFixedThreadPool(5)   def […]

SBT: pass arguments to main

If you write a main class in Scala, sbt will generally find it automatically: def main(args: Array[String]): Unit = { println(args.mkString(", ")) } If you want to pass arguments to it, you can do that with run-main like so: sbt "run-main SSLChecker arg1 arg2" And you’ll get this: arg1, arg2

Scala Futures example

Scala futures allow you to easily parallelize operations, by creating a series of tasks and running them in a thread pool. Lets take a simple example. We’ve defined two functions that require some I/O: a function that retrieves information about the SSL certificate chain for a website, and a function that publishes that to a […]