alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Akka/Scala example source code file (HttpExample.scala)

This example Akka source code file (HttpExample.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Akka and Scala source code examples by using tags.

All credit for the original source code belongs to akka.io; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Akka tags/keywords

actor, actorref, actorsystem, akka, any, array, camel, camelmessage, failure, httpconsumer, producer

The HttpExample.scala Akka example source code

package sample.camel

import org.apache.camel.Exchange
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.Status.Failure
import akka.actor.actorRef2Scala
import akka.camel.CamelMessage
import akka.camel.Consumer
import akka.camel.Producer

object HttpExample {

  def main(args: Array[String]): Unit = {
    val system = ActorSystem("some-system")
    val httpTransformer = system.actorOf(Props[HttpTransformer])
    val httpProducer = system.actorOf(Props(classOf[HttpProducer], httpTransformer))
    val httpConsumer = system.actorOf(Props(classOf[HttpConsumer], httpProducer))
  }

  class HttpConsumer(producer: ActorRef) extends Consumer {
    def endpointUri = "jetty:http://0.0.0.0:8875/"

    def receive = {
      case msg => producer forward msg
    }
  }

  class HttpProducer(transformer: ActorRef) extends Actor with Producer {
    // bridgeEndpoint=true makes the producer ignore the Exchange.HTTP_URI header, 
    // and use the endpoint's URI for request
    def endpointUri = "jetty://http://akka.io/?bridgeEndpoint=true"

    // before producing messages to endpoints, producer actors can pre-process
    // them by overriding the transformOutgoingMessage method  
    override def transformOutgoingMessage(msg: Any) = msg match {
      case camelMsg: CamelMessage => camelMsg.copy(headers =
        camelMsg.headers(Set(Exchange.HTTP_PATH)))
    }

    // instead of replying to the initial sender(), producer actors can implement custom
    // response processing by overriding the routeResponse method
    override def routeResponse(msg: Any) { transformer forward msg }
  }

  class HttpTransformer extends Actor {
    def receive = {
      case msg: CamelMessage =>
        sender() ! (msg.mapBody { body: Array[Byte] =>
          new String(body).replaceAll("Akka ", "AKKA ")
        })
      case msg: Failure => sender() ! msg
    }
  }

}

Other Akka source code examples

Here is a short list of links related to this Akka HttpExample.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.