This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch bind in repository https://gitbox.apache.org/repos/asf/camel.git
commit e23466692d005a456fc12f81a62e8a3e9d4fa371 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Mar 5 05:51:21 2019 +0100 CAMEL-13283: Add example --- examples/camel-example-main/pom.xml | 85 ++++++++++++++++++++++ examples/camel-example-main/readme.adoc | 13 ++++ .../org/apache/camel/example/MyApplication.java | 80 ++++++++++++++++++++ .../src/main/resources/log4j2.properties | 23 ++++++ examples/pom.xml | 1 + 5 files changed, 202 insertions(+) diff --git a/examples/camel-example-main/pom.xml b/examples/camel-example-main/pom.xml new file mode 100644 index 0000000..f2c9489 --- /dev/null +++ b/examples/camel-example-main/pom.xml @@ -0,0 +1,85 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel.example</groupId> + <artifactId>examples</artifactId> + <version>3.0.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-main</artifactId> + <packaging>jar</packaging> + <name>Camel :: Example :: Main</name> + <description>An example for showing standalone Camel</description> + + <properties> + <category>Beginner</category> + </properties> + + <dependencies> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + </dependency> + + <!-- logging --> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-jul</artifactId> + <scope>runtime</scope> + </dependency> + + </dependencies> + + <build> + <plugins> + <!-- Allows the example to be run via 'mvn camel:run' --> + <plugin> + <groupId>org.apache.camel</groupId> + <artifactId>camel-maven-plugin</artifactId> + <version>${project.version}</version> + <configuration> + <mainClass>org.apache.camel.example.MyApplication</mainClass> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/examples/camel-example-main/readme.adoc b/examples/camel-example-main/readme.adoc new file mode 100644 index 0000000..18aef79 --- /dev/null +++ b/examples/camel-example-main/readme.adoc @@ -0,0 +1,13 @@ +# Camel Example Main + +This example shows how to run Camel standalone via the built-in Main class. + +## How to run + +You can run this example using + + mvn camel:run + +## More information + +You can find more information about Apache Camel at the website: http://camel.apache.org/ diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java new file mode 100644 index 0000000..e60df81 --- /dev/null +++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyApplication.java @@ -0,0 +1,80 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.example; + +import java.util.Date; +import java.util.Objects; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.main.Main; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class MyApplication { + private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class); + + private MyApplication() { + } + + public static void main(String[] args) throws Exception { + Main main = new Main(); + main.addRouteBuilder(new MyRouteBuilder()); + main.run(args); + } + + private static class MyRouteBuilder extends RouteBuilder { + @Override + public void configure() throws Exception { + from("timer:simple?period=503") + .id("simple-route") + .transform() + .exchange(this::dateToTime) + .process() + .message(this::log) + .process() + .body(this::log) + .choice() + .when() + .body(Integer.class, b -> (b & 1) == 0) + .log("Received even number") + .when() + .body(Integer.class, (b, h) -> h.containsKey("skip") ? false : (b & 1) == 0) + .log("Received odd number") + .when() + .body(Objects::isNull) + .log("Received null body") + .when() + .body(Integer.class, b -> (b & 1) != 0) + .log("Received odd number") + .endChoice(); + } + + private Long dateToTime(Exchange e) { + return e.getProperty(Exchange.TIMER_FIRED_TIME, Date.class).getTime(); + } + + private void log(Object b) { + LOGGER.info("body is: {}", b); + } + + private void log(Message m) { + LOGGER.info("message is: {}", m); + } + } +} diff --git a/examples/camel-example-main/src/main/resources/log4j2.properties b/examples/camel-example-main/src/main/resources/log4j2.properties new file mode 100644 index 0000000..d406a9f --- /dev/null +++ b/examples/camel-example-main/src/main/resources/log4j2.properties @@ -0,0 +1,23 @@ +## --------------------------------------------------------------------------- +## Licensed to the Apache Software Foundation (ASF) under one or more +## contributor license agreements. See the NOTICE file distributed with +## this work for additional information regarding copyright ownership. +## The ASF licenses this file to You under the Apache License, Version 2.0 +## (the "License"); you may not use this file except in compliance with +## the License. You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## --------------------------------------------------------------------------- + +appender.out.type = Console +appender.out.name = out +appender.out.layout.type = PatternLayout +appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n +rootLogger.level = INFO +rootLogger.appenderRef.out.ref = out diff --git a/examples/pom.xml b/examples/pom.xml index ab39b58..a5c5d8b 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -77,6 +77,7 @@ <module>camel-example-loadbalancing</module> <module>camel-example-loan-broker-cxf</module> <module>camel-example-loan-broker-jms</module> + <module>camel-example-main</module> <module>camel-example-management</module> <module>camel-example-micrometer</module> <module>camel-example-mybatis</module>