├── .gitignore ├── README.adoc ├── configuration └── settings.xml ├── mysql-deployment.yml ├── pom.xml └── src ├── main ├── java │ └── io │ │ └── fabric8 │ │ └── quickstarts │ │ └── camel │ │ ├── Application.java │ │ ├── Book.java │ │ ├── Order.java │ │ └── OrderService.java ├── jkube │ ├── deployment.yml │ └── route.yml └── resources │ ├── application-dev.yml │ ├── application.yml │ ├── schema-mysql.sql │ └── spring │ └── camel-context.xml └── test ├── java └── io │ └── fabric8 │ └── quickstarts │ └── camel │ └── ApplicationTest.java └── resources ├── application.yml └── log4j.properties /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .idea 3 | *.iml 4 | *.im 5 | *.ipr 6 | *.iws 7 | overlays 8 | .DS_Store 9 | .settings 10 | *.swp 11 | *.log 12 | .project 13 | .classpath 14 | *.fmd 15 | .cache 16 | dependency-reduced-pom.xml 17 | kube-cluster/kubernetes 18 | apps/modifiedFabric8.json 19 | git-clones 20 | .vscode 21 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = Spring-Boot Camel Quickstart using REST / SQL QuickStart 2 | 3 | This example demonstrates how to use SQL via JDBC along with Camel's REST DSL to expose a RESTful API. 4 | 5 | This example relies on the https://www.eclipse.org/jkube/docs/openshift-maven-plugin[Openshift Maven plugin] for its build configuration 6 | and uses the https://github.com/fabric8io/base-images#java-base-images[fabric8 Java base image]. 7 | 8 | The application utilizes the Spring http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/ImportResource.html[`@ImportResource`] annotation to load a Camel Context definition via a _src/main/resources/spring/camel-context.xml_ file on the classpath. 9 | 10 | IMPORTANT: This quickstart can run in 2 modes: standalone on your machine and on Kubernetes / OpenShift Cluster. Quickstart requires Java 8 or Java 11 (`fuse-java-openshift-jdk11-rhel8` image is used to build in Java 11). 11 | 12 | == Deployment options 13 | 14 | You can run this quickstart in the following modes: 15 | 16 | * Kubernetes / Single-node OpenShift cluster 17 | * Standalone on your machine 18 | 19 | The most effective way to run this quickstart is to deploy and run the project on OpenShift. 20 | 21 | For more details about running this quickstart on a single-node OpenShift cluster, CI/CD deployments, as well as the rest of the runtime, see the link:http://appdev.openshift.io/docs/spring-boot-runtime.html[Spring Boot Runtime Guide]. 22 | 23 | == Running the Quickstart on a single-node Kubernetes/OpenShift cluster 24 | 25 | IMPORTANT: You need to run this example on Container Development Kit 3.3 or OpenShift 3.7. 26 | Both of these products have suitable Fuse images pre-installed. 27 | If you run it in an environment where those images are not preinstalled follow the steps described in <>. 28 | 29 | A single-node Kubernetes/OpenShift cluster provides you with access to a cloud environment that is similar to a production environment. 30 | 31 | If you have a single-node Kubernetes/OpenShift cluster, such as Minishift or the Red Hat Container Development Kit, link:http://appdev.openshift.io/docs/minishift-installation.html[installed and running], you can deploy your quickstart there. 32 | 33 | . Log in to your OpenShift cluster: 34 | + 35 | [source,bash,options="nowrap",subs="attributes+"] 36 | ---- 37 | $ oc login -u developer -p developer 38 | ---- 39 | 40 | . Create a new OpenShift project for the quickstart: 41 | + 42 | [source,bash,options="nowrap",subs="attributes+"] 43 | ---- 44 | $ oc new-project MY_PROJECT_NAME 45 | ---- 46 | 47 | . It is assumed that a MySQL service is already running on the platform. You can deploy it using the provided deployment by executing in single-node OpenShift cluster: 48 | + 49 | ---- 50 | $ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/mysql-ephemeral-template.json 51 | $ oc new-app --template=mysql-ephemeral 52 | ---- 53 | + 54 | More information can be found in https://docs.openshift.com/container-platform/3.3/using_images/db_images/mysql.html[using the MySQL database image]. You may need to pass `MYSQL_RANDOM_ROOT_PASSWORD=true` as environment variable to the deployment. 55 | 56 | . Change the directory to the folder that contains the extracted quickstart application (for example, `my_openshift/spring-boot-camel-rest-sql`) : 57 | + 58 | or 59 | + 60 | [source,bash,options="nowrap",subs="attributes+"] 61 | ---- 62 | $ cd my_openshift/spring-boot-camel-rest-sql 63 | ---- 64 | 65 | . Build and deploy the project to the OpenShift cluster: 66 | + 67 | [source,bash,options="nowrap",subs="attributes+"] 68 | ---- 69 | $ mvn clean -DskipTests oc:deploy -Dmysql-service-username= -Dmysql-service-password= -Popenshift 70 | ---- 71 | + 72 | The `username` and `password` system properties correspond to the credentials used when deploying the MySQL database service. 73 | 74 | . In your browser, navigate to the `MY_PROJECT_NAME` project in the OpenShift console. 75 | Wait until you can see that the pod for the `spring-boot-camel-rest-sql` has started up. 76 | 77 | . On the project's `Overview` page, navigate to the details page deployment of the `spring-boot-camel-rest-sql` application: `https://OPENSHIFT_IP_ADDR:8443/console/project/MY_PROJECT_NAME/browse/pods/spring-boot-camel-rest-sql-NUMBER_OF_DEPLOYMENT?tab=details`. 78 | 79 | . Switch to tab `Logs` and then see the messages sent by Camel. 80 | 81 | [#single-node-without-preinstalled-images] 82 | === Running the Quickstart on a single-node Kubernetes/OpenShift cluster without preinstalled images 83 | 84 | A single-node Kubernetes/OpenShift cluster provides you with access to a cloud environment that is similar to a production environment. 85 | 86 | If you have a single-node Kubernetes/OpenShift cluster, such as Minishift or the Red Hat Container Development Kit, link:http://appdev.openshift.io/docs/minishift-installation.html[installed and running], you can deploy your quickstart there. 87 | 88 | 89 | . Log in to your OpenShift cluster: 90 | + 91 | [source,bash,options="nowrap",subs="attributes+"] 92 | ---- 93 | $ oc login -u developer -p developer 94 | ---- 95 | 96 | . Create a new OpenShift project for the quickstart: 97 | + 98 | [source,bash,options="nowrap",subs="attributes+"] 99 | ---- 100 | $ oc new-project MY_PROJECT_NAME 101 | ---- 102 | 103 | . Configure Red Hat Container Registry authentication (if it is not configured). 104 | Follow https://access.redhat.com/documentation/en-us/red_hat_fuse/7.13/html-single/fuse_on_openshift_guide/index#configure-container-registry[documentation]. 105 | 106 | . Import base images in your newly created project (MY_PROJECT_NAME): 107 | + 108 | [source,bash,options="nowrap",subs="attributes+"] 109 | ---- 110 | $ oc import-image fuse-java-openshift:1.13 --from=registry.redhat.io/fuse7/fuse-java-openshift:1.13 --confirm 111 | ---- 112 | 113 | . It is assumed that a MySQL service is already running on the platform. You can deploy it using the provided deployment by executing in single-node OpenShift cluster: 114 | + 115 | ---- 116 | $ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/db-templates/mysql-ephemeral-template.json 117 | $ oc new-app --template=mysql-ephemeral -p MYSQL_USER= -p MYSQL_PASSWORD= 118 | ---- 119 | + 120 | More information can be found in https://docs.openshift.com/container-platform/3.3/using_images/db_images/mysql.html[using the MySQL database image]. You may need to pass `MYSQL_RANDOM_ROOT_PASSWORD=true` as environment variable to the deployment. 121 | 122 | . Change the directory to the folder that contains the extracted quickstart application (for example, `my_openshift/spring-boot-camel-rest-sql`) : 123 | + 124 | or 125 | + 126 | [source,bash,options="nowrap",subs="attributes+"] 127 | ---- 128 | $ cd my_openshift/spring-boot-camel-rest-sql 129 | ---- 130 | 131 | . Build and deploy the project to the OpenShift cluster: 132 | + 133 | [source,bash,options="nowrap",subs="attributes+"] 134 | ---- 135 | $ mvn clean -DskipTests oc:deploy -Dmysql-service-username= -Dmysql-service-password= -Popenshift -Djkube.generator.fromMode=istag -Djkube.generator.from=MY_PROJECT_NAME/fuse-java-openshift:1.13 136 | ---- 137 | + 138 | The `username` and `password` system properties correspond to the credentials used when deploying the MySQL database service. 139 | 140 | . In your browser, navigate to the `MY_PROJECT_NAME` project in the OpenShift console. 141 | Wait until you can see that the pod for the `spring-boot-camel-rest-sql` has started up. 142 | 143 | . On the project's `Overview` page, navigate to the details page deployment of the `spring-boot-camel-rest-sql` application: `https://OPENSHIFT_IP_ADDR:8443/console/project/MY_PROJECT_NAME/browse/pods/spring-boot-camel-rest-sql-NUMBER_OF_DEPLOYMENT?tab=details`. 144 | 145 | . Switch to tab `Logs` and then see the messages sent by Camel. 146 | 147 | == Accessing the REST service 148 | 149 | When the example is running, a REST service is available to list the books that can be ordered, and as well the order statuses. 150 | 151 | If you run the example on a single-node OpenShift cluster, then the REST service is exposed at 'http://spring-boot-camel-rest-sql-MY_PROJECT_NAME.OPENSHIFT_IP_ADDR.nip.io/camel-rest-sql/`. 152 | 153 | Notice: As it depends on your OpenShift setup, the hostname (route) might vary. Verify with `oc get routes` which hostname is valid for you. Add the `-Djkube.deploy.createExternalUrls=true` option to your Maven commands if you want it to deploy a Route configuration for the service. 154 | 155 | The actual endpoint is using the _context-path_ `camel-rest-sql/books` and the REST service provides two services: 156 | 157 | - `books`: to list all the available books that can be ordered, 158 | - `books/order/{id}`: to output order status for the given order `id`. 159 | 160 | The example automatically creates new orders with a running order `id` starting from 1. 161 | 162 | You can then access these services from your Web browser, e.g.: 163 | 164 | - 165 | - 166 | 167 | == Swagger API 168 | 169 | The example provides API documentation of the service using Swagger using the _context-path_ `camel-rest-sql/api-doc`. You can access the API documentation from your Web browser at . 170 | 171 | == Running the quickstart standalone on your machine 172 | 173 | To run this quickstart as a standalone project on your local machine: 174 | 175 | . Download the project and extract the archive on your local filesystem. 176 | . Build the project: 177 | + 178 | [source,bash,options="nowrap",subs="attributes+"] 179 | ---- 180 | $ cd PROJECT_DIR 181 | $ mvn clean package 182 | ---- 183 | . Run the service: 184 | 185 | + 186 | [source,bash,options="nowrap",subs="attributes+"] 187 | ---- 188 | $ mvn spring-boot:run 189 | ---- 190 | + 191 | Alternatively, you can run the application locally using the executable JAR produced: 192 | + 193 | ---- 194 | $ java -jar -Dspring.profiles.active=dev target/spring-boot-camel-rest-sql-1.0-SNAPSHOT.jar 195 | ---- 196 | + 197 | This uses an embedded in-memory HSQLDB database. You can use the default Spring Boot profile in case you have a MySQL server available for you to test. 198 | 199 | . You can then access the REST API directly from your Web browser, e.g.: 200 | 201 | - 202 | - 203 | -------------------------------------------------------------------------------- /configuration/settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | fusesource.repo 32 | 33 | 34 | maven.central 35 | Maven Central 36 | https://repo1.maven.org/maven2 37 | 38 | false 39 | 40 | 41 | true 42 | never 43 | 44 | 45 | 46 | redhat.ga 47 | Red Hat General Availability Repository 48 | https://maven.repository.redhat.com/ga 49 | 50 | false 51 | 52 | 53 | true 54 | never 55 | 56 | 57 | 58 | redhat.earlyaccess 59 | Red Hat General Early Access Repository 60 | https://maven.repository.redhat.com/earlyaccess 61 | 62 | false 63 | 64 | 65 | true 66 | never 67 | 68 | 69 | 70 | fusesource.m2 71 | FuseSource Community Release Repository 72 | https://repo.fusesource.com/nexus/content/groups/public 73 | 74 | false 75 | 76 | 77 | true 78 | never 79 | 80 | 81 | 82 | fusesource.ea 83 | FuseSource Community Early Access Release Repository 84 | https://repo.fusesource.com/nexus/content/groups/ea 85 | 86 | false 87 | 88 | 89 | true 90 | never 91 | 92 | 93 | 94 | 95 | 96 | maven.central 97 | Maven Central 98 | https://repo1.maven.org/maven2 99 | 100 | false 101 | 102 | 103 | true 104 | never 105 | 106 | 107 | 108 | redhat.ga 109 | Red Hat General Availability Repository 110 | https://maven.repository.redhat.com/ga 111 | 112 | false 113 | 114 | 115 | true 116 | never 117 | 118 | 119 | 120 | redhat.earlyaccess 121 | Red Hat General Early Access Repository 122 | https://maven.repository.redhat.com/earlyaccess 123 | 124 | false 125 | 126 | 127 | true 128 | never 129 | 130 | 131 | 132 | fusesource.m2 133 | FuseSource Community Release Repository 134 | https://repo.fusesource.com/nexus/content/groups/public 135 | 136 | false 137 | 138 | 139 | true 140 | never 141 | 142 | 143 | 144 | fusesource.ea 145 | FuseSource Community Early Access Release Repository 146 | https://repo.fusesource.com/nexus/content/groups/ea 147 | 148 | false 149 | 150 | 151 | true 152 | never 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | fusesource.repo 161 | 162 | 163 | 164 | -------------------------------------------------------------------------------- /mysql-deployment.yml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: mysql 5 | labels: 6 | template: mysql-ephemeral 7 | spec: 8 | ports: 9 | - name: mysql 10 | protocol: TCP 11 | port: 3306 12 | targetPort: 3306 13 | nodePort: 0 14 | selector: 15 | name: mysql 16 | type: ClusterIP 17 | sessionAffinity: None 18 | --- 19 | apiVersion: apps/v1 20 | kind: Deployment 21 | metadata: 22 | name: mysql 23 | spec: 24 | strategy: 25 | type: Recreate 26 | selector: 27 | matchLabels: 28 | name: mysql 29 | app: mysql 30 | template: 31 | metadata: 32 | name: mysql 33 | labels: 34 | name: mysql 35 | spec: 36 | containers: 37 | - image: mysql:5.6 38 | name: mysql 39 | ports: 40 | - containerPort: 3306 41 | protocol: TCP 42 | env: 43 | - name : MYSQL_USER 44 | value: ${MYSQL_USER} 45 | - name: MYSQL_PASSWORD 46 | value: ${MYSQL_PASSWORD} 47 | - name: MYSQL_DATABASE 48 | value: ${MYSQL_DATABASE} 49 | - name: MYSQL_RANDOM_ROOT_PASSWORD 50 | value: 'yes' 51 | volumeMounts: 52 | - name: mysql-data 53 | mountPath: /var/lib/mysql/data 54 | volumes: 55 | - name: mysql-data 56 | emptyDir: 57 | medium: -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 19 | 21 | 22 | 4.0.0 23 | 24 | io.fabric8.quickstarts 25 | spring-boot-camel-rest-sql 26 | 1.0-SNAPSHOT 27 | 28 | Fabric8 :: Quickstarts :: Spring-Boot :: Camel REST / SQL 29 | Spring Boot example running a Camel REST route connecting to a SQL database 30 | 31 | 32 | UTF-8 33 | UTF-8 34 | 35 | 36 | dev 37 | 38 | 39 | 7.12.0.fuse-7_12_0-00016-redhat-00001 40 | 1.13 41 | 42 | 43 | 3.7.0 44 | 2.22.2 45 | 46 | 47 | 48 | 49 | 50 | org.jboss.redhat-fuse 51 | fuse-springboot-bom 52 | ${fuse.bom.version} 53 | pom 54 | import 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | org.apache.camel 63 | camel-spring-boot-starter 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-starter-web 68 | 69 | 70 | org.springframework.boot 71 | spring-boot-starter-tomcat 72 | 73 | 74 | 75 | 76 | org.springframework.boot 77 | spring-boot-starter-undertow 78 | 79 | 80 | org.springframework.boot 81 | spring-boot-starter-actuator 82 | 83 | 84 | org.springframework.boot 85 | spring-boot-starter-jdbc 86 | 87 | 88 | org.apache.camel 89 | camel-servlet-starter 90 | 91 | 92 | org.apache.camel 93 | camel-jackson-starter 94 | 95 | 96 | org.apache.camel 97 | camel-swagger-java-starter 98 | 99 | 100 | org.apache.camel 101 | camel-sql-starter 102 | 103 | 104 | 105 | mysql 106 | mysql-connector-java 107 | runtime 108 | 109 | 110 | org.hsqldb 111 | hsqldb 112 | runtime 113 | 114 | 115 | 116 | 117 | org.springframework.boot 118 | spring-boot-starter-test 119 | test 120 | 121 | 122 | junit 123 | junit 124 | test 125 | 126 | 127 | org.jboss.arquillian.junit 128 | arquillian-junit-container 129 | test 130 | 131 | 132 | org.apache.httpcomponents 133 | httpclient 134 | test 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | maven-compiler-plugin 143 | org.apache.maven.plugins 144 | ${maven-compiler-plugin.version} 145 | 146 | 1.8 147 | 1.8 148 | 149 | 150 | 151 | 152 | maven-surefire-plugin 153 | org.apache.maven.plugins 154 | ${maven-surefire-plugin.version} 155 | 156 | 157 | **/*KT.java 158 | 159 | 160 | 161 | 162 | 163 | spring-boot-maven-plugin 164 | org.jboss.redhat-fuse 165 | ${fuse.bom.version} 166 | 167 | 168 | 169 | repackage 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | openshift 181 | 182 | registry.redhat.io/fuse7/fuse-java-openshift-rhel8:${docker.image.version} 183 | 184 | 185 | 186 | 187 | org.jboss.redhat-fuse 188 | openshift-maven-plugin 189 | ${fuse.bom.version} 190 | 191 | 192 | 193 | resource 194 | build 195 | apply 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | com.company 205 | Red_Hat 206 | 207 | 208 | rht.prod_name 209 | Red_Hat_Integration 210 | 211 | 212 | rht.prod_ver 213 | 7.13.0 214 | 215 | 216 | rht.comp 217 | spring-boot-camel-rest-sql 218 | 219 | 220 | rht.comp_ver 221 | ${fuse.bom.version} 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | java11 233 | 234 | registry.redhat.io/fuse7/fuse-java-openshift-jdk11-rhel8:${docker.image.version} 235 | 236 | 237 | [11,17) 238 | 239 | 240 | 241 | java17 242 | 243 | registry.redhat.io/fuse7/fuse-java-openshift-jdk17-rhel8:${docker.image.version} 244 | 245 | 246 | [17,) 247 | 248 | 249 | 250 | 251 | 252 | 253 | redhat-ga-repository 254 | https://maven.repository.redhat.com/ga 255 | 256 | true 257 | 258 | 259 | false 260 | 261 | 262 | 263 | redhat-ea-repository 264 | https://maven.repository.redhat.com/earlyaccess/all 265 | 266 | true 267 | 268 | 269 | false 270 | 271 | 272 | 273 | 274 | 275 | 276 | redhat-ga-repository 277 | https://maven.repository.redhat.com/ga 278 | 279 | true 280 | 281 | 282 | false 283 | 284 | 285 | 286 | redhat-ea-repository 287 | https://maven.repository.redhat.com/earlyaccess/all 288 | 289 | true 290 | 291 | 292 | false 293 | 294 | 295 | 296 | 297 | 298 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/quickstarts/camel/Application.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005-2016 Red Hat, Inc. 3 | * 4 | * Red Hat licenses this file to you under the Apache License, version 5 | * 2.0 (the "License"); you may not use this file except in compliance 6 | * with the License. You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 | * implied. See the License for the specific language governing 14 | * permissions and limitations under the License. 15 | */ 16 | package io.fabric8.quickstarts.camel; 17 | 18 | import org.apache.camel.builder.RouteBuilder; 19 | import org.apache.camel.component.servlet.CamelHttpTransportServlet; 20 | import org.apache.camel.model.rest.RestBindingMode; 21 | import org.springframework.boot.SpringApplication; 22 | import org.springframework.boot.autoconfigure.SpringBootApplication; 23 | import org.springframework.boot.web.servlet.ServletRegistrationBean; 24 | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; 25 | import org.springframework.context.annotation.Bean; 26 | import org.springframework.context.annotation.ImportResource; 27 | import org.springframework.stereotype.Component; 28 | 29 | @SpringBootApplication 30 | @ImportResource({"classpath:spring/camel-context.xml"}) 31 | public class Application extends SpringBootServletInitializer { 32 | 33 | public static void main(String[] args) { 34 | SpringApplication.run(Application.class, args); 35 | } 36 | 37 | @Component 38 | class RestApi extends RouteBuilder { 39 | 40 | @Override 41 | public void configure() { 42 | restConfiguration() 43 | .contextPath("/camel-rest-sql").apiContextPath("/api-doc") 44 | .apiProperty("api.title", "Camel REST API") 45 | .apiProperty("api.version", "1.0") 46 | .apiProperty("cors", "true") 47 | .apiProperty("api.specification.contentType.json", "application/vnd.oai.openapi+json;version=2.0") 48 | .apiProperty("api.specification.contentType.yaml", "application/vnd.oai.openapi;version=2.0") 49 | .apiContextRouteId("doc-api") 50 | .component("servlet") 51 | .bindingMode(RestBindingMode.json); 52 | 53 | rest("/books").description("Books REST service") 54 | .get("/").description("The list of all the books") 55 | .route().routeId("books-api") 56 | .to("sql:select distinct description from orders?" + 57 | "dataSource=dataSource&" + 58 | "outputClass=io.fabric8.quickstarts.camel.Book") 59 | .endRest() 60 | .get("order/{id}").description("Details of an order by id") 61 | .route().routeId("order-api") 62 | .to("sql:select * from orders where id = :#${header.id}?" + 63 | "dataSource=dataSource&outputType=SelectOne&" + 64 | "outputClass=io.fabric8.quickstarts.camel.Order"); 65 | } 66 | } 67 | 68 | @Component 69 | class Backend extends RouteBuilder { 70 | 71 | @Override 72 | public void configure() { 73 | // A first route generates some orders and queue them in DB 74 | from("timer:new-order?delay=1s&period={{quickstart.generateOrderPeriod:2s}}") 75 | .routeId("generate-order") 76 | .bean("orderService", "generateOrder") 77 | .to("sql:insert into orders (id, item, amount, description, processed) values " + 78 | "(:#${body.id} , :#${body.item}, :#${body.amount}, :#${body.description}, false)?" + 79 | "dataSource=dataSource") 80 | .log("Inserted new order ${body.id}"); 81 | 82 | // A second route polls the DB for new orders and processes them 83 | from("sql:select * from orders where processed = false?" + 84 | "consumer.onConsume=update orders set processed = true where id = :#id&" + 85 | "consumer.delay={{quickstart.processOrderPeriod:5s}}&" + 86 | "dataSource=dataSource") 87 | .routeId("process-order") 88 | .bean("orderService", "rowToOrder") 89 | .log("Processed order #id ${body.id} with ${body.amount} copies of the «${body.description}» book"); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/quickstarts/camel/Book.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005-2016 Red Hat, Inc. 3 | * 4 | * Red Hat licenses this file to you under the Apache License, version 5 | * 2.0 (the "License"); you may not use this file except in compliance 6 | * with the License. You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 | * implied. See the License for the specific language governing 14 | * permissions and limitations under the License. 15 | */ 16 | package io.fabric8.quickstarts.camel; 17 | 18 | public class Book { 19 | 20 | private String description; 21 | 22 | public String getDescription() { 23 | return description; 24 | } 25 | 26 | public void setDescription(String description) { 27 | this.description = description; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/quickstarts/camel/Order.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005-2016 Red Hat, Inc. 3 | * 4 | * Red Hat licenses this file to you under the Apache License, version 5 | * 2.0 (the "License"); you may not use this file except in compliance 6 | * with the License. You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 | * implied. See the License for the specific language governing 14 | * permissions and limitations under the License. 15 | */ 16 | package io.fabric8.quickstarts.camel; 17 | 18 | public class Order { 19 | 20 | private int id; 21 | private String item; 22 | private int amount; 23 | private String description; 24 | private boolean processed; 25 | 26 | public int getId() { 27 | return id; 28 | } 29 | 30 | public void setId(int id) { 31 | this.id = id; 32 | } 33 | 34 | public String getItem() { 35 | return item; 36 | } 37 | 38 | public void setItem(String item) { 39 | this.item = item; 40 | } 41 | 42 | public int getAmount() { 43 | return amount; 44 | } 45 | 46 | public void setAmount(int amount) { 47 | this.amount = amount; 48 | } 49 | 50 | public String getDescription() { 51 | return description; 52 | } 53 | 54 | public void setDescription(String description) { 55 | this.description = description; 56 | } 57 | 58 | public boolean isProcessed() { 59 | return processed; 60 | } 61 | 62 | public void setProcessed(boolean processed) { 63 | this.processed = processed; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/io/fabric8/quickstarts/camel/OrderService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005-2016 Red Hat, Inc. 3 | * 4 | * Red Hat licenses this file to you under the Apache License, version 5 | * 2.0 (the "License"); you may not use this file except in compliance 6 | * with the License. You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 | * implied. See the License for the specific language governing 14 | * permissions and limitations under the License. 15 | */ 16 | package io.fabric8.quickstarts.camel; 17 | 18 | import org.springframework.stereotype.Component; 19 | 20 | import java.util.Map; 21 | import java.util.Random; 22 | import java.util.concurrent.atomic.AtomicInteger; 23 | 24 | @Component 25 | public class OrderService { 26 | 27 | private final AtomicInteger counter = new AtomicInteger(); 28 | 29 | private final Random amount = new Random(); 30 | 31 | public Order generateOrder() { 32 | Order order = new Order(); 33 | order.setId(counter.incrementAndGet()); 34 | order.setItem(counter.get() % 2 == 0 ? "Camel" : "ActiveMQ"); 35 | order.setAmount(amount.nextInt(10) + 1); 36 | order.setDescription(counter.get() % 2 == 0 ? "Camel in Action" : "ActiveMQ in Action"); 37 | return order; 38 | } 39 | 40 | public Order rowToOrder(Map row) { 41 | Order order = new Order(); 42 | order.setId((Integer) row.get("id")); 43 | order.setItem((String) row.get("item")); 44 | order.setAmount((Integer) row.get("amount")); 45 | order.setDescription((String) row.get("description")); 46 | order.setProcessed((Boolean) row.get("processed")); 47 | return order; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/jkube/deployment.yml: -------------------------------------------------------------------------------- 1 | spec: 2 | replicas: 1 3 | template: 4 | spec: 5 | containers: 6 | - 7 | resources: 8 | requests: 9 | cpu: "0.2" 10 | memory: 256Mi 11 | limits: 12 | cpu: "1.0" 13 | memory: 256Mi 14 | env: 15 | - name: SPRING_APPLICATION_JSON 16 | value: '{"server":{"undertow":{"io-threads":1, "worker-threads":2 }}}' 17 | - name: MYSQL_SERVICE_NAME 18 | value: mysql 19 | - name: MYSQL_SERVICE_DATABASE 20 | value: sampledb 21 | - name: MYSQL_SERVICE_USERNAME 22 | value: ${mysql-service-username} 23 | - name: MYSQL_SERVICE_PASSWORD 24 | value: ${mysql-service-password} 25 | -------------------------------------------------------------------------------- /src/main/jkube/route.yml: -------------------------------------------------------------------------------- 1 | 2 | apiVersion: "route.openshift.io/v1" 3 | kind: Route 4 | spec: 5 | to: 6 | kind: Service 7 | name: spring-boot-camel-rest-sql 8 | -------------------------------------------------------------------------------- /src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | camel: 2 | springboot: 3 | # The Camel context name 4 | name: CamelRestSql 5 | component: 6 | servlet: 7 | mapping: 8 | enabled: true 9 | context-path: /camel-rest-sql/* 10 | 11 | # Binding health checks to a different port 12 | # change actuator endpoints path 13 | management: 14 | server: 15 | port: 8081 16 | 17 | # disable all management enpoints except health 18 | endpoints: 19 | enabled: false 20 | health: 21 | enabled: true 22 | 23 | spring: 24 | # Spring JDBC configuration 25 | datasource: 26 | # Let Spring Boot auto-configure an embedded HSQL database 27 | url: 28 | 29 | # The application configuration properties 30 | quickstart: 31 | generateOrderPeriod: 2s 32 | processOrderPeriod: 5s 33 | 34 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | camel: 2 | springboot: 3 | # The Camel context name 4 | name: CamelRestSql 5 | component: 6 | servlet: 7 | mapping: 8 | enabled: true 9 | context-path: /camel-rest-sql/* 10 | 11 | # Binding health checks to a different port 12 | # change actuator endpoints path 13 | management: 14 | server: 15 | port: 8081 16 | 17 | # disable all management enpoints except health 18 | endpoints: 19 | enabled: false 20 | health: 21 | enabled: true 22 | 23 | mysql: 24 | service: 25 | # The name of the service hosting the MySQL database server, 26 | # can be customized using the 'MYSQL_SERVICE_NAME' env variable to use a different service 27 | name: mysql 28 | # The database to use, can be customized using the 'MYSQL_SERVICE_DATABASE' env variable 29 | database: sampledb 30 | 31 | spring: 32 | # Spring JDBC configuration 33 | sql: 34 | init: 35 | username: ${mysql.service.username} 36 | password: ${mysql.service.password} 37 | platform: mysql 38 | mode: always 39 | datasource: 40 | username: ${mysql.service.username} 41 | password: ${mysql.service.password} 42 | url: jdbc:mysql://${${mysql.service.name}.service.host}:${${mysql.service.name}.service.port}/${mysql.service.database} 43 | main: 44 | allow-bean-definition-overriding: true 45 | 46 | # The application configuration properties 47 | quickstart: 48 | generateOrderPeriod: 10s 49 | processOrderPeriod: 30s 50 | -------------------------------------------------------------------------------- /src/main/resources/schema-mysql.sql: -------------------------------------------------------------------------------- 1 | drop table if exists orders; 2 | 3 | create table orders ( 4 | id integer primary key, 5 | item varchar(10), 6 | amount integer, 7 | description varchar(30), 8 | processed boolean 9 | ); -------------------------------------------------------------------------------- /src/main/resources/spring/camel-context.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 18 | 19 | -------------------------------------------------------------------------------- /src/test/java/io/fabric8/quickstarts/camel/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2005-2016 Red Hat, Inc. 3 | * 4 | * Red Hat licenses this file to you under the Apache License, version 5 | * 2.0 (the "License"); you may not use this file except in compliance 6 | * with the License. You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 | * implied. See the License for the specific language governing 14 | * permissions and limitations under the License. 15 | */ 16 | package io.fabric8.quickstarts.camel; 17 | 18 | import org.apache.camel.CamelContext; 19 | import org.apache.camel.builder.NotifyBuilder; 20 | import org.junit.Test; 21 | import org.junit.runner.RunWith; 22 | import org.springframework.beans.factory.annotation.Autowired; 23 | import org.springframework.boot.test.context.SpringBootTest; 24 | import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; 25 | import org.springframework.boot.test.web.client.TestRestTemplate; 26 | import org.springframework.core.ParameterizedTypeReference; 27 | import org.springframework.http.HttpMethod; 28 | import org.springframework.http.HttpStatus; 29 | import org.springframework.http.ResponseEntity; 30 | import org.springframework.test.context.junit4.SpringRunner; 31 | 32 | import java.util.List; 33 | import java.util.concurrent.TimeUnit; 34 | 35 | import static org.assertj.core.api.Assertions.assertThat; 36 | 37 | @RunWith(SpringRunner.class) 38 | @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) 39 | public class ApplicationTest { 40 | 41 | @Autowired 42 | private TestRestTemplate restTemplate; 43 | 44 | @Autowired 45 | private CamelContext camelContext; 46 | 47 | @Test 48 | public void newOrderTest() { 49 | // Wait enough time until the first order gets inserted and processed 50 | NotifyBuilder notify = new NotifyBuilder(camelContext) 51 | .fromRoute("generate-order") 52 | .whenDone(2) 53 | .and() 54 | .fromRoute("process-order") 55 | .whenDone(1) 56 | .create(); 57 | assertThat(notify.matches(10, TimeUnit.SECONDS)).isTrue(); 58 | 59 | // Then call the REST API 60 | ResponseEntity orderResponse = restTemplate.getForEntity("/camel-rest-sql/books/order/1", Order.class); 61 | assertThat(orderResponse.getStatusCode()).isEqualTo(HttpStatus.OK); 62 | Order order = orderResponse.getBody(); 63 | assertThat(order.getId()).isEqualTo(1); 64 | assertThat(order.getAmount()).isBetween(1, 10); 65 | assertThat(order.getItem()).isIn("Camel", "ActiveMQ"); 66 | assertThat(order.getDescription()).isIn("Camel in Action", "ActiveMQ in Action"); 67 | assertThat(order.isProcessed()).isTrue(); 68 | 69 | ResponseEntity> booksResponse = restTemplate.exchange("/camel-rest-sql/books", 70 | HttpMethod.GET, null, new ParameterizedTypeReference>(){}); 71 | assertThat(booksResponse.getStatusCode()).isEqualTo(HttpStatus.OK); 72 | List books = booksResponse.getBody(); 73 | assertThat(books).hasSize(2); 74 | assertThat(books.get(0).getDescription()).isIn("ActiveMQ in Action"); 75 | assertThat(books.get(1).getDescription()).isIn("Camel in Action"); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/test/resources/application.yml: -------------------------------------------------------------------------------- 1 | camel: 2 | springboot: 3 | name: CamelRestSql 4 | component: 5 | servlet: 6 | mapping: 7 | enabled: true 8 | context-path: /camel-rest-sql/* 9 | 10 | quickstart: 11 | generateOrderPeriod: 1s 12 | processOrderPeriod: 3s 13 | -------------------------------------------------------------------------------- /src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Copyright 2005-2015 Red Hat, Inc. 3 | # 4 | # Red Hat licenses this file to you under the Apache License, version 5 | # 2.0 (the "License"); you may not use this file except in compliance 6 | # with the License. You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 13 | # implied. See the License for the specific language governing 14 | # permissions and limitations under the License. 15 | # 16 | 17 | # 18 | # The logging properties used 19 | # 20 | log4j.rootLogger=INFO, stdout 21 | 22 | # uncomment the next line to debug Camel 23 | #log4j.logger.org.apache.camel=DEBUG 24 | 25 | # CONSOLE appender not used by default 26 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 27 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 28 | log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n --------------------------------------------------------------------------------