2010-02-25 17:19:50 +01:00
|
|
|
package sample.camel
|
|
|
|
|
|
2010-03-05 19:38:23 +01:00
|
|
|
import org.apache.camel.{Exchange, Processor}
|
2010-03-14 11:17:34 +01:00
|
|
|
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-02-25 17:19:50 +01:00
|
|
|
|
2010-05-03 09:05:27 +02:00
|
|
|
import se.scalablesolutions.akka.actor.Actor._
|
2010-07-26 18:47:25 +02:00
|
|
|
import se.scalablesolutions.akka.actor.{TypedActor, Supervisor}
|
2010-03-05 19:38:23 +01:00
|
|
|
import se.scalablesolutions.akka.camel.CamelContextManager
|
2010-10-18 19:30:43 +02:00
|
|
|
import se.scalablesolutions.akka.config.Supervision._
|
2010-02-25 17:19:50 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author Martin Krasser
|
|
|
|
|
*/
|
|
|
|
|
class Boot {
|
|
|
|
|
|
2010-06-10 17:09:23 +02:00
|
|
|
// -----------------------------------------------------------------------
|
2010-06-20 17:16:06 +02:00
|
|
|
// Basic example
|
2010-06-10 17:09:23 +02:00
|
|
|
// -----------------------------------------------------------------------
|
2010-03-14 11:17:34 +01:00
|
|
|
|
2010-06-20 17:16:06 +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])),
|
2010-10-04 11:18:10 +02:00
|
|
|
// Supervise(actorOf[Consumer1], Permanent) ::
|
|
|
|
|
// Supervise(actorOf[Consumer2], Permanent) :: Nil))
|
2010-02-25 17:19:50 +01:00
|
|
|
|
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-03-14 11:17:34 +01:00
|
|
|
|
2010-07-20 18:48:34 +02:00
|
|
|
// Create CamelContext and a Spring-based registry
|
2010-07-21 15:37:37 +02:00
|
|
|
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))
|
2010-10-05 15:49:11 +02:00
|
|
|
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))
|
2010-03-05 19:38:23 +01:00
|
|
|
|
|
|
|
|
producer.start
|
|
|
|
|
mediator.start
|
|
|
|
|
consumer.start
|
|
|
|
|
|
2010-07-20 18:48:34 +02:00
|
|
|
// -----------------------------------------------------------------------
|
2010-07-21 08:33:20 +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-14 11:17:34 +01:00
|
|
|
|
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
|
2010-03-14 11:17:34 +01:00
|
|
|
|
|
|
|
|
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-03-14 11:17:34 +01:00
|
|
|
|
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
|
2010-05-16 20:15:08 +02:00
|
|
|
val jmsPublisherBridge = actorOf(new PublisherBridge("jetty:http://0.0.0.0:8877/camel/pub/jms", jmsPublisher)).start
|
2010-05-09 15:35:43 +02:00
|
|
|
|
2010-06-10 17:09:23 +02:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
// Actor un-publishing and re-publishing example
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
2010-05-16 20:15:08 +02:00
|
|
|
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-01 17:11:56 +02:00
|
|
|
|
2010-06-10 17:09:23 +02:00
|
|
|
// -----------------------------------------------------------------------
|
2010-06-03 13:06:27 +02:00
|
|
|
// Active object example
|
2010-06-10 17:09:23 +02:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
|
|
2010-07-30 13:17:44 +02:00
|
|
|
TypedActor.newInstance(classOf[TypedConsumer1], classOf[TypedConsumer1Impl])
|
2010-02-25 17:19:50 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-10 17:09:23 +02:00
|
|
|
/**
|
|
|
|
|
* @author Martin Krasser
|
|
|
|
|
*/
|
2010-02-25 17:19:50 +01:00
|
|
|
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)
|
2010-03-05 19:38:23 +01:00
|
|
|
from("direct:welcome").process(new Processor() {
|
|
|
|
|
def process(exchange: Exchange) {
|
|
|
|
|
exchange.getOut.setBody("Welcome %s" format exchange.getIn.getBody)
|
|
|
|
|
}
|
|
|
|
|
})
|
2010-02-25 17:19:50 +01:00
|
|
|
}
|
2010-06-01 18:41:39 +02:00
|
|
|
}
|