diff --git a/spring-boot-packaging/README.md b/spring-boot-packaging/README.md new file mode 100644 index 0000000..cf4997b --- /dev/null +++ b/spring-boot-packaging/README.md @@ -0,0 +1,100 @@ +# Spring Boot packaging for Camel + +## Goals + +Spring Boot enables you to package your Camel routes along with any required +dependency. A "fat jar" is produced and can be run with just a `java -jar /path/to/app.jar`. + +In this example, we will package the Camel routes as a Spring Boot application. + +## How this project has been created + +Generate a new Maven project using the `camel-archetype-spring-boot` archetype: +``` +mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-spring-boot -DarchetypeVersion=2.21.1 -DinteractiveMode=false -DgroupId=fr.itix.camel-examples -DartifactId=spring-boot-packaging -Dversion=1.0-SNAPSHOT -Dpackage=fr.itix.camel.spring_boot_packaging +``` + +By default, the `camel-archetype-spring-boot` archetype creates a sample route +using the Java DSL. In this example, we will use the Spring DSL, so you can remove +the two following classes: +``` +rm src/main/java/fr/itix/camel/spring_boot_packaging/MySpringBean.java +rm src/main/java/fr/itix/camel/spring_boot_packaging/MySpringBootRouter.java +``` + +Create a `camel` directory in `src/main/resources`: +``` +mkdir src/main/resources/camel/ +``` + +Create an `hello-world.xml` file in the newly created `camel` directory with +this content: +``` + + + + + + +``` + +## Try it ! + +Each time you changed something in your `camel-context.xml`, you will have +to re-run Camel with: +``` +mvn package spring-boot:run +``` + +If you followed the instructions, you should have something like this: +``` +[INFO] --- spring-boot-maven-plugin:1.5.12.RELEASE:run (default-cli) @ spring-boot-packaging --- + + . ____ _ __ _ _ + /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ +( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ + \\/ ___)| |_)| | | | | || (_| | ) ) ) ) + ' |____| .__|_| |_|_| |_\__, | / / / / + =========|_|==============|___/=/_/_/_/ + :: Spring Boot :: (v1.5.12.RELEASE) + +2018-06-05 12:40:51.475 INFO 24580 --- [ main] f.i.c.s.MySpringBootApplication : Starting MySpringBootApplication on nmasse-OSX.local with PID 24580 (/Users/nmasse/git/perso/Camel-Examples/spring-boot-packaging/target/classes started by nmasse in /Users/nmasse/git/perso/Camel-Examples/spring-boot-packaging) +2018-06-05 12:40:51.477 INFO 24580 --- [ main] f.i.c.s.MySpringBootApplication : No active profile set, falling back to default profiles: default + +[...] + +2018-06-05 12:40:54.527 INFO 24580 --- [ main] o.a.camel.spring.boot.RoutesCollector : Loading additional Camel XML routes from: classpath:camel/*.xml +2018-06-05 12:40:55.006 INFO 24580 --- [ main] o.a.camel.spring.boot.RoutesCollector : Loading additional Camel XML rests from: classpath:camel-rest/*.xml +2018-06-05 12:40:55.020 INFO 24580 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.21.1 (CamelContext: MyCamel) is starting +2018-06-05 12:40:55.021 INFO 24580 --- [ main] o.a.c.m.ManagedManagementStrategy : JMX is enabled +2018-06-05 12:40:55.190 INFO 24580 --- [ main] o.a.camel.spring.SpringCamelContext : StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html +2018-06-05 12:40:55.205 INFO 24580 --- [ main] o.a.camel.spring.SpringCamelContext : Route: hello-world started and consuming from: timer://hello?period=1000 +2018-06-05 12:40:55.207 INFO 24580 --- [ main] o.a.camel.spring.SpringCamelContext : Total 1 routes, of which 1 are started +2018-06-05 12:40:55.207 INFO 24580 --- [ main] o.a.camel.spring.SpringCamelContext : Apache Camel 2.21.1 (CamelContext: MyCamel) started in 0.187 seconds +2018-06-05 12:40:55.263 INFO 24580 --- [ main] b.c.e.u.UndertowEmbeddedServletContainer : Undertow started on port(s) 8080 (http) +2018-06-05 12:40:55.271 INFO 24580 --- [ main] f.i.c.s.MySpringBootApplication : Started MySpringBootApplication in 4.173 seconds (JVM running for 7.038) +2018-06-05 12:40:56.222 INFO 24580 --- [- timer://hello] hello-world : Hello, world ! +2018-06-05 12:40:57.216 INFO 24580 --- [- timer://hello] hello-world : Hello, world ! +2018-06-05 12:40:58.216 INFO 24580 --- [- timer://hello] hello-world : Hello, world ! +2018-06-05 12:40:59.217 INFO 24580 --- [- timer://hello] hello-world : Hello, world ! +``` + +Note: you will have to press Ctrl-C to exit this Hello World example! + +## Packaging + +As you might have noticed, by default this archetype generates a fat jar: +``` +$ ls -lh target/*.jar +-rw-r--r-- 1 nmasse staff 21M Jun 5 12:40 target/spring-boot-packaging-1.0-SNAPSHOT.jar +``` + +When ready, you can copy the fat jar on another server and run it: +``` +java -jar target/spring-boot-packaging-1.0-SNAPSHOT.jar +``` + +## References + +- [Camel Archetypes](http://camel.apache.org/camel-maven-archetypes.html) +- [Spring Boot support for Camel](http://camel.apache.org/spring-boot.html) diff --git a/spring-boot-packaging/pom.xml b/spring-boot-packaging/pom.xml new file mode 100644 index 0000000..ba402f9 --- /dev/null +++ b/spring-boot-packaging/pom.xml @@ -0,0 +1,103 @@ + + + + 4.0.0 + + fr.itix.camel-examples + spring-boot-packaging + jar + 1.0-SNAPSHOT + + A Camel Spring Boot Route + + + UTF-8 + UTF-8 + 1.5.12.RELEASE + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring.boot-version} + pom + import + + + + org.apache.camel + camel-spring-boot-dependencies + 2.21.1 + pom + import + + + + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + org.springframework.boot + spring-boot-starter-undertow + + + org.springframework.boot + spring-boot-starter-actuator + + + + + org.apache.camel + camel-spring-boot-starter + + + org.apache.camel + camel-stream-starter + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.apache.camel + camel-test-spring + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring.boot-version} + + + + repackage + + + + + + + + diff --git a/spring-boot-packaging/src/main/java/fr/itix/camel/spring_boot_packaging/MySpringBootApplication.java b/spring-boot-packaging/src/main/java/fr/itix/camel/spring_boot_packaging/MySpringBootApplication.java new file mode 100644 index 0000000..26b6e00 --- /dev/null +++ b/spring-boot-packaging/src/main/java/fr/itix/camel/spring_boot_packaging/MySpringBootApplication.java @@ -0,0 +1,16 @@ +package fr.itix.camel.spring_boot_packaging; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class MySpringBootApplication { + + /** + * A main method to start this application. + */ + public static void main(String[] args) { + SpringApplication.run(MySpringBootApplication.class, args); + } + +} diff --git a/spring-boot-packaging/src/main/resources/application.properties b/spring-boot-packaging/src/main/resources/application.properties new file mode 100644 index 0000000..4411c89 --- /dev/null +++ b/spring-boot-packaging/src/main/resources/application.properties @@ -0,0 +1,21 @@ +# to automatic shutdown the JVM after a period of time +#camel.springboot.duration-max-seconds=60 +#camel.springboot.duration-max-messages=100 + +# add for example: &repeatCount=5 to the timer endpoint to make Camel idle +#camel.springboot.duration-max-idle-seconds=15 + +# all access to actuator endpoints without security +management.security.enabled = false +# turn on actuator health check +endpoints.health.enabled = true + +# allow to obtain basic information about camel routes (read only mode) +endpoints.camelroutes.enabled = true +endpoints.camelroutes.read-only = true + +# to configure logging levels +#logging.level.org.springframework = INFO +#logging.level.org.apache.camel.spring.boot = INFO +#logging.level.org.apache.camel.impl = DEBUG +#logging.level.sample.camel = DEBUG diff --git a/spring-boot-packaging/src/main/resources/camel/hello-world.xml b/spring-boot-packaging/src/main/resources/camel/hello-world.xml new file mode 100644 index 0000000..3e4213b --- /dev/null +++ b/spring-boot-packaging/src/main/resources/camel/hello-world.xml @@ -0,0 +1,6 @@ + + + + + +