Skip to content

Instantly share code, notes, and snippets.

View krasserm's full-sized avatar

Martin Krasser krasserm

View GitHub Profile
@krasserm
krasserm / PromiseEither.scala
Created February 22, 2011 16:59
Composition of concurrent functions that may fail
import scalaz._
import Scalaz._
import scalaz.concurrent._
/**
* Result type of concurrent functions that may fail.
*/
case class PromiseEither[A, B](value: Promise[Either[A, B]]) extends NewType[Promise[Either[A, B]]]
/**
@krasserm
krasserm / BoundedMailboxDirectConsumer.scala
Created February 19, 2011 14:14
Akka consumer actors best practices
class BoundedMailboxDirectConsumer extends Actor with Consumer {
import akka.dispatch._
import akka.util.duration._
// Create a bounded mailbox for this consumer actor with a capacity of 10
self.dispatcher = Dispatchers.newThreadBasedDispatcher(self, 5, 100.milliseconds)
// instruct how the route should be customized during route creation (definition)
onRouteDefinition { rd: RouteDefinition =>
// on exception attempt max 3 redeliveries with a delay of 1000 ms
// ###########################################################
//
// Demonstrates how to supervise an Akka consumer actor.
//
// The consumer consumes messages from a file endpoint:
// - successful message processing by the consumer will
// positively acknowledge the message receipt, causing
// the file endpoint to delete the file.
// - an exception during message processing will cause a
// supervisor to restart the consumer. Before restart,
import scalaz._
object Example extends Application {
import Scalaz._
import Scalaz._
//
// monadic use of Either
import Responder._
import scalaz._
object ResponderDemo extends Application {
import Scalaz._
// --------------------------------------------------------------------------
// Uses Responder (a continuation monad) to compose asynchronous functions.
// --------------------------------------------------------------------------
import org.scalatest.matchers.MustMatchers
import scalaz._
import scalaz.camel._
object RouterConfig {
import org.apache.camel.impl.DefaultCamelContext
val context = new DefaultCamelContext
val template = context.createProducerTemplate
import scalaz._
object ValidationPromise extends Application {
import scalaz.Scalaz._
import scalaz.concurrent.Promise
import scalaz.concurrent.Strategy
// ------------------------------------------------
// Applicative composition of concurrent functions
// ------------------------------------------------
import scalaz._
object ValidationApplicative extends Application {
import Scalaz._
// -----------------------------------------------------------------------------------
// Applicative use of scalaz.Validation to combine results of independent computations
// e.g. results from multiple recipients, see also multicast EIP in scalaz-camel: http://goo.gl/FXWRi
// -----------------------------------------------------------------------------------
import scalaz._
object ValidationKleisli extends Application {
import Scalaz._
// ------------------------------------------------------------------------------------------------
// Monadic use of scalaz.Validation to sequence dependent computations
// e.g. processors in a route, see also Kleisli composition of processors in scalaz-camel: http://goo.gl/CUIlQ
// ------------------------------------------------------------------------------------------------
import scalaz._
object ValidationApplicative extends Application {
import Scalaz._
case class Message(b: String)
// special-case sufficient for scalaz-camel
implicit def ExceptionSemigroup: Semigroup[Exception] = semigroup((e1, e2) => e1)