Skip to content

Instantly share code, notes, and snippets.

View krasserm's full-sized avatar

Martin Krasser krasserm

View GitHub Profile
@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
@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 / CamelContextManager.scala
Created February 28, 2011 06:29
Akka producer actors best practices - Part 1
CamelContextManager.init
CamelContextManager.start
@krasserm
krasserm / CorrelationIdentifier.scala
Created February 28, 2011 06:36
Akka producer actors best practices - Part 2
httpProducer ! Message("test", Map(Message.MessageExchangeId -> 123))
@krasserm
krasserm / scalaz-camel-0.3.patch
Created April 14, 2011 15:54
Downgrade to Akka 1.0 and Scala 2.8.1
Index: project/build/Project.scala
===================================================================
--- project/build/Project.scala (date 1302329914000)
+++ project/build/Project.scala (revision )
@@ -3,9 +3,9 @@
class Project(info: ProjectInfo) extends DefaultProject(info) with IdeaProject with AkkaProject {
val ScalazVersion = "5.0"
val CamelVersion = "2.7.0"
- val AkkaVersion = "1.1-M1"
+ val AkkaVersion = "1.0"
@krasserm
krasserm / GCAuthExample.scala
Created July 5, 2011 17:53
GAE client login example
import org.apache.http.client.methods.HttpPost
import org.apache.http.client.params.ClientPNames._
import org.apache.http.client.HttpClient
import org.apache.http.entity.StringEntity
// other imports omitted ...
object GCAuthExample {
def main(args:Array[String]) {
@krasserm
krasserm / MonadTransformerExamples.scala
Created July 14, 2011 10:32
Scalaz 7 monad transformer examples
import scalaz._
import Scalaz._
object MonadTransformerExamples {
def main(args: Array[String]) = run
def run {
// ------------------------------------------------------
// Combined Option/Option
// ------------------------------------------------------
@krasserm
krasserm / domain-01.scala
Created November 29, 2011 11:55
Building an event-sourced web application with Akka - Part 1
case class InvoiceAddress(street: String, city: String, country: String)
case class InvoiceItem(description: String, count: Int, amount: BigDecimal)
case class Invoice(
id: String,
items: List[InvoiceItem] = Nil,
discount: Option[BigDecimal] = None,
sentTo: Option[InvoiceAddress] = None) extends EventSourced[InvoiceEvent, Invoice] {
def addItem(item: InvoiceItem): Update[InvoiceEvent, Invoice] = ...
@krasserm
krasserm / service-01.scala
Created November 29, 2011 12:03
Building an event-sourced web application with Akka - Part 1
class InvoiceService {
val invoicesRef = Ref(Map.empty[String, Invoice]) // naive approach
// ...
}
@krasserm
krasserm / service-01.scala
Created January 20, 2012 16:52
Building an event-sourced web application with Akka - Part 2
import akka.actor._
import akka.dispatch._
import akka.stm._
import scalaz._
trait Projection[S, A] {
def initialState: S
def currentState: S
def project: PartialFunction[(S, A), S]