=doc #3689 Make activator templates for camel samples

* @rkuhn found the problems in the scala camel samples
  - HttpSample returns scrambled data
  - CustomRouteSample times out
This commit is contained in:
Patrik Nordwall 2013-11-15 16:28:14 +01:00
parent 362074177a
commit 23dd957ba2
47 changed files with 693 additions and 559 deletions

View file

@ -1,23 +0,0 @@
package docs.camel.sample.http;
import akka.actor.ActorRef;
import akka.camel.javaapi.UntypedConsumerActor;
//#HttpExample
public class HttpConsumer extends UntypedConsumerActor{
private ActorRef producer;
public HttpConsumer(ActorRef producer){
this.producer = producer;
}
public String getEndpointUri() {
return "jetty:http://0.0.0.0:8875/";
}
public void onReceive(Object message) {
producer.forward(message, getContext());
}
}
//#HttpExample

View file

@ -1,38 +0,0 @@
package docs.camel.sample.http;
import akka.actor.ActorRef;
import akka.camel.CamelMessage;
import akka.camel.javaapi.UntypedProducerActor;
import org.apache.camel.Exchange;
import java.util.HashSet;
import java.util.Set;
//#HttpExample
public class HttpProducer extends UntypedProducerActor{
private ActorRef transformer;
public HttpProducer(ActorRef transformer) {
this.transformer = transformer;
}
public String getEndpointUri() {
return "jetty://http://akka.io/?bridgeEndpoint=true";
}
@Override
public Object onTransformOutgoingMessage(Object message) {
if (message instanceof CamelMessage) {
CamelMessage camelMessage = (CamelMessage) message;
Set<String> httpPath = new HashSet<String>();
httpPath.add(Exchange.HTTP_PATH);
return camelMessage.withHeaders(camelMessage.getHeaders(httpPath));
} else return super.onTransformOutgoingMessage(message);
}
@Override
public void onRouteResponse(Object message) {
transformer.forward(message, getContext());
}
}
//#HttpExample

View file

@ -1,23 +0,0 @@
package docs.camel.sample.http;
import akka.actor.*;
public class HttpSample {
public static void main(String[] args) {
//#HttpExample
// Create the actors. this can be done in a Boot class so you can
// run the example in the MicroKernel. Just add the three lines below
// to your boot class.
ActorSystem system = ActorSystem.create("some-system");
final ActorRef httpTransformer = system.actorOf(
Props.create(HttpTransformer.class));
final ActorRef httpProducer = system.actorOf(
Props.create(HttpProducer.class, httpTransformer));
final ActorRef httpConsumer = system.actorOf(
Props.create(HttpConsumer.class, httpProducer));
//#HttpExample
}
}

View file

@ -1,29 +0,0 @@
package docs.camel.sample.http;
import akka.actor.Status;
import akka.actor.UntypedActor;
import akka.camel.CamelMessage;
import akka.dispatch.Mapper;
import akka.japi.Function;
//#HttpExample
public class HttpTransformer extends UntypedActor{
public void onReceive(Object message) {
if (message instanceof CamelMessage) {
CamelMessage camelMessage = (CamelMessage) message;
CamelMessage replacedMessage =
camelMessage.mapBody(new Mapper<Object, String>(){
@Override
public String apply(Object body) {
String text = new String((byte[])body);
return text.replaceAll("Akka ", "AKKA ");
}
});
getSender().tell(replacedMessage, getSelf());
} else if (message instanceof Status.Failure) {
getSender().tell(message, getSelf());
} else
unhandled(message);
}
}
//#HttpExample

View file

@ -1,20 +0,0 @@
package docs.camel.sample.quartz;
//#QuartzExample
import akka.camel.CamelMessage;
import akka.camel.javaapi.UntypedConsumerActor;
public class MyQuartzActor extends UntypedConsumerActor{
public String getEndpointUri() {
return "quartz://example?cron=0/2+*+*+*+*+?";
}
public void onReceive(Object message) {
if (message instanceof CamelMessage) {
CamelMessage camelMessage = (CamelMessage) message;
String body = camelMessage.getBodyAs(String.class, getCamelContext());
System.out.println(String.format("==============> received %s ", body));
} else
unhandled(message);
}
}
//#QuartzExample

View file

@ -1,12 +0,0 @@
package docs.camel.sample.quartz;
//#QuartzExample
import akka.actor.ActorSystem;
import akka.actor.Props;
public class QuartzSample {
public static void main(String[] args) {
ActorSystem system = ActorSystem.create("my-quartz-system");
system.actorOf(Props.create(MyQuartzActor.class));
}
}
//#QuartzExample

View file

@ -1,29 +0,0 @@
package docs.camel.sample.route;
//#CustomRouteExample
import akka.actor.ActorRef;
import akka.camel.CamelMessage;
import akka.camel.javaapi.UntypedConsumerActor;
public class Consumer3 extends UntypedConsumerActor{
private ActorRef transformer;
public Consumer3(ActorRef transformer){
this.transformer = transformer;
}
public String getEndpointUri() {
return "jetty:http://0.0.0.0:8877/camel/welcome";
}
public void onReceive(Object message) {
if (message instanceof CamelMessage) {
CamelMessage camelMessage = (CamelMessage) message;
// Forward a string representation of the message body to transformer
String body = camelMessage.getBodyAs(String.class, getCamelContext());
transformer.forward(camelMessage.withBody(body), getContext());
} else
unhandled(message);
}
}
//#CustomRouteExample

View file

@ -1,18 +0,0 @@
package docs.camel.sample.route;
//#CustomRouteExample
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
public class CustomRouteBuilder extends RouteBuilder{
public void configure() throws Exception {
from("direct:welcome").process(new Processor(){
public void process(Exchange exchange) throws Exception {
exchange.getOut().setBody(String.format("Welcome %s",
exchange.getIn().getBody()));
}
});
}
}
//#CustomRouteExample

View file

@ -1,23 +0,0 @@
package docs.camel.sample.route;
import akka.actor.*;
import akka.camel.CamelExtension;
public class CustomRouteSample {
@SuppressWarnings("unused")
public static void main(String[] args) {
try {
//#CustomRouteExample
// the below lines can be added to a Boot class, so that you can run the
// example from a MicroKernel
ActorSystem system = ActorSystem.create("some-system");
final ActorRef producer = system.actorOf(Props.create(Producer1.class));
final ActorRef mediator = system.actorOf(Props.create(Transformer.class, producer));
final ActorRef consumer = system.actorOf(Props.create(Consumer3.class, mediator));
CamelExtension.get(system).context().addRoutes(new CustomRouteBuilder());
//#CustomRouteExample
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -1,10 +0,0 @@
package docs.camel.sample.route;
//#CustomRouteExample
import akka.camel.javaapi.UntypedProducerActor;
public class Producer1 extends UntypedProducerActor{
public String getEndpointUri() {
return "direct:welcome";
}
}
//#CustomRouteExample

View file

@ -1,33 +0,0 @@
package docs.camel.sample.route;
//#CustomRouteExample
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import akka.camel.CamelMessage;
import akka.dispatch.Mapper;
import akka.japi.Function;
public class Transformer extends UntypedActor {
private ActorRef producer;
public Transformer(ActorRef producer) {
this.producer = producer;
}
public void onReceive(Object message) {
if (message instanceof CamelMessage) {
// example: transform message body "foo" to "- foo -" and forward result
// to producer
CamelMessage camelMessage = (CamelMessage) message;
CamelMessage transformedMessage =
camelMessage.mapBody(new Mapper<String, String>(){
@Override
public String apply(String body) {
return String.format("- %s -",body);
}
});
producer.forward(transformedMessage, getContext());
} else
unhandled(message);
}
}
//#CustomRouteExample