Created
May 27, 2015 07:39
-
-
Save cmoulliard/b8ecd0b24526117e5186 to your computer and use it in GitHub Desktop.
Using Apache Camel Without DSL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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