Created
February 28, 2011 06:29
-
-
Save krasserm/847006 to your computer and use it in GitHub Desktop.
Akka producer actors best practices - Part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CamelContextManager.init | |
CamelContextManager.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor._ | |
import akka.camel._ | |
class HttpProducer extends Actor with Producer { | |
def endpointUri = "jetty:http://localhost:8080/test" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implicit val sender: Option[ActorRef] = ... | |
httpProducer ! "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val httpProducer = Actor.actorOf[HttpProducer].start | |
httpProducer !! "test" match { | |
case Some(Message(body, headers)) => println("body = %s, headers = %s" format (body, headers)) | |
case _ => ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
httpProducer !! "test" match { | |
case Some(Message(body, headers)) => println("body = %s, headers = %s" format (body, headers)) | |
case Some(Failure(cause, headers)) => println("failure cause = %s, headers = %s" format (cause, headers)) | |
case None => println("timeout") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JmsProducer extends Actor with Producer { | |
def endpointUri = "jms:queue:test" | |
override def oneway = true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JmsReplyingProducer extends Actor with Producer { | |
def endpointUri = "jms:queue:test" | |
override def oneway = true | |
override def receiveAfterProduce = { | |
case _ => self.reply("done") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JmsReplyingProducer extends Actor with Producer { | |
def endpointUri = "jms:queue:test" | |
override def oneway = true | |
override def receiveAfterProduce = { | |
case message: Message => self.reply("done") | |
case failure: Failure => self.reply("message could not be added to queue: %s" format failure.cause.getMessage) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Producer { | |
// ... | |
protected def receiveAfterProduce: Receive = { | |
case msg => if (!oneway) self.reply(msg) | |
} | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeProducer extends Actor with Producer { | |
// ... | |
override def receiveAfterProduce = { | |
case Failure(cause, _) => throw cause | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment