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

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

This example Akka source code file (PublishSubscribe.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, camelmessage, consumer, producer, props, publishsubscribe, string, subscriber

The PublishSubscribe.scala Akka example source code

package docs.camel

object PublishSubscribe {
  //#PubSub
  import akka.actor.{ Actor, ActorRef, ActorSystem, Props }
  import akka.camel.{ Producer, CamelMessage, Consumer }

  class Subscriber(name: String, uri: String) extends Actor with Consumer {
    def endpointUri = uri

    def receive = {
      case msg: CamelMessage => println("%s received: %s" format (name, msg.body))
    }
  }

  class Publisher(name: String, uri: String) extends Actor with Producer {

    def endpointUri = uri

    // one-way communication with JMS
    override def oneway = true
  }

  class PublisherBridge(uri: String, publisher: ActorRef) extends Actor with Consumer {
    def endpointUri = uri

    def receive = {
      case msg: CamelMessage => {
        publisher ! msg.bodyAs[String]
        sender() ! ("message published")
      }
    }
  }

  // Add below to a Boot class
  // Setup publish/subscribe example
  val system = ActorSystem("some-system")
  val jmsUri = "jms:topic:test"
  val jmsSubscriber1 = system.actorOf(Props(classOf[Subscriber], "jms-subscriber-1", jmsUri))
  val jmsSubscriber2 = system.actorOf(Props(classOf[Subscriber], "jms-subscriber-2", jmsUri))
  val jmsPublisher = system.actorOf(Props(classOf[Publisher], "jms-publisher", jmsUri))
  val jmsPublisherBridge = system.actorOf(Props(classOf[PublisherBridge], "jetty:http://0.0.0.0:8877/camel/pub/jms", jmsPublisher))
  //#PubSub
}

Other Akka source code examples

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