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

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

This example Akka source code file (Introduction.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, actorsystem, akka, camel, camelextension, camelmessage, consumer, introduction, myendpoint, oneway, producer, props, unit, util

The Introduction.scala Akka example source code

package docs.camel

import akka.actor.{ Props, ActorSystem }
import akka.camel.CamelExtension

import language.postfixOps
import akka.util.Timeout

object Introduction {
  def foo(): Unit = {
    //#Consumer-mina
    import akka.camel.{ CamelMessage, Consumer }

    class MyEndpoint extends Consumer {
      def endpointUri = "mina2:tcp://localhost:6200?textline=true"

      def receive = {
        case msg: CamelMessage => { /* ... */ }
        case _                 => { /* ... */ }
      }
    }

    // start and expose actor via tcp
    import akka.actor.{ ActorSystem, Props }

    val system = ActorSystem("some-system")
    val mina = system.actorOf(Props[MyEndpoint])
    //#Consumer-mina
  }
  def bar(): Unit = {
    //#Consumer
    import akka.camel.{ CamelMessage, Consumer }

    class MyEndpoint extends Consumer {
      def endpointUri = "jetty:http://localhost:8877/example"

      def receive = {
        case msg: CamelMessage => { /* ... */ }
        case _                 => { /* ... */ }
      }
    }
    //#Consumer
  }
  def baz(): Unit = {
    //#Producer
    import akka.actor.Actor
    import akka.camel.{ Producer, Oneway }
    import akka.actor.{ ActorSystem, Props }

    class Orders extends Actor with Producer with Oneway {
      def endpointUri = "jms:queue:Orders"
    }

    val sys = ActorSystem("some-system")
    val orders = sys.actorOf(Props[Orders])

    orders ! <order amount="100" currency="PLN" itemId="12345"/>
    //#Producer
  }
  {
    //#CamelExtension
    val system = ActorSystem("some-system")
    val camel = CamelExtension(system)
    val camelContext = camel.context
    val producerTemplate = camel.template

    //#CamelExtension
  }
  {
    //#CamelExtensionAddComponent
    // import org.apache.activemq.camel.component.ActiveMQComponent
    val system = ActorSystem("some-system")
    val camel = CamelExtension(system)
    val camelContext = camel.context
    // camelContext.addComponent("activemq", ActiveMQComponent.activeMQComponent(
    //   "vm://localhost?broker.persistent=false"))
    //#CamelExtensionAddComponent
  }
  {
    //#CamelActivation
    import akka.camel.{ CamelMessage, Consumer }
    import scala.concurrent.duration._

    class MyEndpoint extends Consumer {
      def endpointUri = "mina2:tcp://localhost:6200?textline=true"

      def receive = {
        case msg: CamelMessage => { /* ... */ }
        case _                 => { /* ... */ }
      }
    }
    val system = ActorSystem("some-system")
    val camel = CamelExtension(system)
    val actorRef = system.actorOf(Props[MyEndpoint])
    // get a future reference to the activation of the endpoint of the Consumer Actor
    val activationFuture = camel.activationFutureFor(actorRef)(timeout = 10 seconds,
      executor = system.dispatcher)
    //#CamelActivation
    //#CamelDeactivation
    system.stop(actorRef)
    // get a future reference to the deactivation of the endpoint of the Consumer Actor
    val deactivationFuture = camel.deactivationFutureFor(actorRef)(timeout = 10 seconds,
      executor = system.dispatcher)
    //#CamelDeactivation
  }

}

Other Akka source code examples

Here is a short list of links related to this Akka Introduction.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.