pekko/akka-samples/akka-sample-camel/src/main/scala/Boot.scala

109 lines
4.4 KiB
Scala
Raw Normal View History

package sample.camel
import org.apache.camel.{Exchange, Processor}
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.spring.spi.ApplicationContextRegistry
import org.springframework.context.support.ClassPathXmlApplicationContext
2010-05-03 09:05:27 +02:00
import se.scalablesolutions.akka.actor.Actor._
import se.scalablesolutions.akka.actor.{TypedActor, Supervisor}
import se.scalablesolutions.akka.camel.CamelContextManager
import se.scalablesolutions.akka.config.Supervision._
/**
* @author Martin Krasser
*/
class Boot {
2010-06-10 17:09:23 +02:00
// -----------------------------------------------------------------------
// Basic example
2010-06-10 17:09:23 +02:00
// -----------------------------------------------------------------------
actorOf[Consumer1].start
actorOf[Consumer2].start
// Alternatively, use a supervisor for these actors
//val supervisor = Supervisor(
// SupervisorConfig(
// RestartStrategy(OneForOne, 3, 100, List(classOf[Exception])),
// Supervise(actorOf[Consumer1], Permanent) ::
// Supervise(actorOf[Consumer2], Permanent) :: Nil))
2010-06-10 17:09:23 +02:00
// -----------------------------------------------------------------------
2010-07-20 18:48:34 +02:00
// Custom Camel route example
2010-06-10 17:09:23 +02:00
// -----------------------------------------------------------------------
2010-07-20 18:48:34 +02:00
// Create CamelContext and a Spring-based registry
val context = new ClassPathXmlApplicationContext("/context-jms.xml", getClass)
2010-07-20 18:48:34 +02:00
val registry = new ApplicationContextRegistry(context)
// Use a custom Camel context and a custom touter builder
CamelContextManager.init(new DefaultCamelContext(registry))
CamelContextManager.mandatoryContext.addRoutes(new CustomRouteBuilder)
2010-07-20 18:48:34 +02:00
2010-05-08 19:01:12 +02:00
val producer = actorOf[Producer1]
2010-05-08 15:59:11 +02:00
val mediator = actorOf(new Transformer(producer))
val consumer = actorOf(new Consumer3(mediator))
producer.start
mediator.start
consumer.start
2010-07-20 18:48:34 +02:00
// -----------------------------------------------------------------------
// Asynchronous consumer-producer example (Akka homepage transformation)
2010-07-20 18:48:34 +02:00
// -----------------------------------------------------------------------
val httpTransformer = actorOf(new HttpTransformer).start
val httpProducer = actorOf(new HttpProducer(httpTransformer)).start
val httpConsumer = actorOf(new HttpConsumer(httpProducer)).start
2010-06-10 17:09:23 +02:00
// -----------------------------------------------------------------------
// Publish subscribe examples
// -----------------------------------------------------------------------
2010-03-19 08:14:00 +01:00
//
2010-07-20 18:48:34 +02:00
// Cometd example commented out because camel-cometd is broken since Camel 2.3
2010-03-19 08:14:00 +01:00
//
2010-05-30 08:22:23 +02:00
//val cometdUri = "cometd://localhost:8111/test/abc?baseResource=file:target"
//val cometdSubscriber = actorOf(new Subscriber("cometd-subscriber", cometdUri)).start
//val cometdPublisher = actorOf(new Publisher("cometd-publisher", cometdUri)).start
val jmsUri = "jms:topic:test"
2010-05-08 15:59:11 +02:00
val jmsSubscriber1 = actorOf(new Subscriber("jms-subscriber-1", jmsUri)).start
val jmsSubscriber2 = actorOf(new Subscriber("jms-subscriber-2", jmsUri)).start
val jmsPublisher = actorOf(new Publisher("jms-publisher", jmsUri)).start
2010-05-30 08:22:23 +02:00
//val cometdPublisherBridge = actorOf(new PublisherBridge("jetty:http://0.0.0.0:8877/camel/pub/cometd", cometdPublisher)).start
val jmsPublisherBridge = actorOf(new PublisherBridge("jetty:http://0.0.0.0:8877/camel/pub/jms", jmsPublisher)).start
2010-06-10 17:09:23 +02:00
// -----------------------------------------------------------------------
// Actor un-publishing and re-publishing example
// -----------------------------------------------------------------------
actorOf[Consumer4].start // POSTing "stop" to http://0.0.0.0:8877/camel/stop stops and unpublishes this actor
actorOf[Consumer5].start // POSTing any msg to http://0.0.0.0:8877/camel/start starts and published Consumer4 again.
2010-06-10 17:09:23 +02:00
// -----------------------------------------------------------------------
// Active object example
2010-06-10 17:09:23 +02:00
// -----------------------------------------------------------------------
TypedActor.newInstance(classOf[TypedConsumer1], classOf[TypedConsumer1Impl])
}
2010-06-10 17:09:23 +02:00
/**
* @author Martin Krasser
*/
class CustomRouteBuilder extends RouteBuilder {
def configure {
val actorUri = "actor:%s" format classOf[Consumer2].getName
2010-06-10 17:09:23 +02:00
from("jetty:http://0.0.0.0:8877/camel/custom").to(actorUri)
from("direct:welcome").process(new Processor() {
def process(exchange: Exchange) {
exchange.getOut.setBody("Welcome %s" format exchange.getIn.getBody)
}
})
}
2010-06-01 18:41:39 +02:00
}