Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Apache Camel Without DSL
package org.jboss.fuse.camel;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.LoggingLevel;
import org.apache.camel.Processor;
import org.apache.camel.builder.DefaultErrorHandlerBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.camel.model.BeanDefinition;
import org.apache.camel.model.FromDefinition;
import org.apache.camel.model.LogDefinition;
import org.apache.camel.model.ProcessorDefinition;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.model.SetBodyDefinition;
import org.apache.camel.model.ToDefinition;
import org.apache.camel.model.language.SimpleExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MainApp1 {
private static Logger logger = LoggerFactory.getLogger(MainApp1.class);
public static void main(String[] args) throws Exception {
// CamelContext = container where we will register the routes
DefaultCamelContext camelContext = new DefaultCamelContext();
// Registry to keep Beans
SimpleRegistry myRegistry = new SimpleRegistry();
myRegistry.put("myBean", new MyBean());
// 1. Using RouteDefinition
RouteDefinition rd = new RouteDefinition();
rd.setExchangePattern(ExchangePattern.InOnly);
rd.setAutoStartup("true");
rd.setErrorHandlerBuilder(new DefaultErrorHandlerBuilder());
rd.setTrace("true");
// A From
FromDefinition from = new FromDefinition("timer://exercise-rd?delay=1000");
List<FromDefinition> fromDefinitionList = new ArrayList<FromDefinition>();
fromDefinitionList.add(from);
// A Log EIP
List<ProcessorDefinition<?>> processorDefinitions = new ArrayList<ProcessorDefinition<?>>();
LogDefinition logDefinition = new LogDefinition(">> This is the first Camel exercise - using Route Definition");
logDefinition.setLoggingLevel(LoggingLevel.INFO );
// A Simple processor
ToDefinition to = new ToDefinition();
to.setUri("log:org.jboss.training.fuse?level=DEBUG");
// Define a Body
SetBodyDefinition myBody = new SetBodyDefinition(new SimpleExpression("Training user"));
// A Bean
BeanDefinition myBean = new BeanDefinition("myBean");
processorDefinitions.add(logDefinition);
processorDefinitions.add(to);
processorDefinitions.add(myBody);
processorDefinitions.add(myBean);
// Complete Route definition
rd.setInputs(fromDefinitionList);
rd.setOutputs(processorDefinitions);
// Register RouteDefinition
camelContext.addRouteDefinition(rd);
camelContext.setRegistry(myRegistry);
// Start the container
camelContext.start();
// give it time to realize it has work to do
Thread.sleep(20000);
}
public static class MyBean {
public void sayHello(String to) {
System.out.println("Hello 1 : " + to);
}
public void sayHelloList(List<String> students) {
System.out.println("Hello : " + students.get(0));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment