Red Hat Training

A Red Hat training course is available for Red Hat Fuse

Chapter 4. Develop an Application for the Spring Boot Image

4.1. Overview

This chapter explains how to develop applications for the Spring Boot image.

4.2. Create a Spring Boot Project using Maven Archetype

To create a Spring Boot project, follow these steps:

  1. Go to the appropriate directory on your system.
  2. Launch the mvn command to create Spring Boot project

    mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate \
      -DarchetypeCatalog=https://maven.repository.redhat.com/ga/io/fabric8/archetypes/archetypes-catalog/2.2.195.redhat-000017/archetypes-catalog-2.2.195.redhat-000017-archetype-catalog.xml \
      -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \
      -DarchetypeArtifactId=spring-boot-camel-xml-archetype \
      -DarchetypeVersion=2.2.195.redhat-000017

    The archetype plug-in switches to interactive mode to prompt you for the remaining fields

    Define value for property 'groupId': : org.example.fis
    Define value for property 'artifactId': : fis-spring-boot
    Define value for property 'version':  1.0-SNAPSHOT: :
    Define value for property 'package':  org.example.fis: :
    
    [INFO] Using property: spring-boot-version = 1.4.1.RELEASE
    Confirm properties configuration:
    groupId: org.example.fis
    artifactId: fis-spring-boot
    version: 1.0-SNAPSHOT
    package: org.example.fis
    spring-boot-version: 1.4.1.RELEASE
     Y: :

    When prompted, enter org.example.fis for the groupId value and fis-spring-boot for the artifactId value. Accept the defaults for the remaining fields.

Then, follow the instructions in the quickstart on how to build and deploy the example.

Note

For the full list of available Spring Boot archetypes, see Section 4.4, “Spring Boot Archetype Catalog”.

4.3. Structure of the Camel Spring Boot Application

The directory structure of a Camel Spring Boot application is as follows:

  ├── LICENSE.md
  ├── pom.xml
  ├── README.md
  └── src
      ├── main
      │   ├── fabric8
      │   │   └── deployment.yml
      │   ├── java
      │   │   └── org
      │   │       └── first1
      │   │           └── spring
      │   │               └── boot
      │   │                   └── project
      │   │                       ├── Application.java
      │   │                       └── MyTransformer.java
      │   └── resources
      │       ├── application.properties
      │       ├── logback.xml
      │       └── spring
      │           └── camel-context.xml
      └── test
          ├── java
          │   └── org
          │       └── first1
          │           └── spring
          │               └── boot
          │                   └── project
          │                       └── KubernetesIntegrationKT.java
          └── resources

Where the following files are important for developing an application:

pom.xml
Includes additional dependencies. Camel components that are compatible with Spring Boot are available in the starter version, for example camel-jdbc-starter or camel-infinispan-starter. Once the starters are included in the pom.xml they are automatically configured and registered with the Camel content at boot time. Users can configure the properties of the components using the application.properties file.
application.properties

It is an important file that allows you to externalize your configuration and work with the same application code in different environments. For details, see Externalized Configuration

For example, in this Camel application you can configure certain properties such as name of the application or the IP addresses, and so on.

application.properties

# the options from org.apache.camel.spring.boot.CamelConfigurationProperties can be configured here
camel.springboot.name=MyCamel

# lets listen on all ports to ensure we can be invoked from the pod IP
server.address=0.0.0.0
management.address=0.0.0.0

Application.java

It is an important file to run your application. As a user you will import here a file camel-context.xml to configure routes using the Spring DSL.

The Application.java file specifies the @SpringBootApplication annotation, which is equivalent to @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.

Application.java

@SpringBootApplication
// load regular Spring XML file from the classpath that contains the Camel XML DSL
@ImportResource({"classpath:spring/camel-context.xml"})

It must have a main method to run the Spring Boot application.

Application.java

public class Application {
    /**
     * A main method to start this application.
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

camel-context.xml

The src/main/resources/spring/camel-context.xml is an important file for developing application as it contains the Camel routes.

Note

You can find more information on developing Spring-Boot applications at Developing your first Spring Boot Application

src/main/fabric8/deployment.yml

Provides additional configuration that is merged with the default OpenShift configuration file generated by the fabric8-maven-plugin.

Note

This file is not used part of Spring Boot application but it is used in all quickstarts to limit the resources such as CPU and memory usage.

KubernetesIntegrationKT.java
An Arquillian based integration test that test deploying into OpenShift and making sure the container can boot correctly.

4.4. Spring Boot Archetype Catalog

The Spring Boot Archetype catalog includes the following examples.

Table 4.1. Spring Boot Maven Archetypes

NameDescription

spring-boot-camel-archetype

Demonstrates how to use Apache Camel with Spring Boot based on a fabric8 Java base image.

spring-boot-camel-amq-archetype

Demonstrates how to connect a Spring-Boot application to an ActiveMQ broker and use JMS messaging between two Camel routes using Kubernetes or OpenShift.

spring-boot-camel-config-archetype

Demonstrates how to configure a Spring-Boot application using Kubernetes ConfigMaps and Secrets.

spring-boot-camel-drools-archetype

Demonstrates how to use Apache Camel to integrate a Spring-Boot application running on Kubernetes or OpenShift with a remote Kie Server.

spring-boot-camel-infinispan-archetype

Demonstrates how to connect a Spring-Boot application to a JBoss Data Grid or Infinispan server using the Hot Rod protocol.

spring-boot-camel-rest-sql-archetype

Demonstrates how to use SQL via JDBC along with Camel’s REST DSL to expose a RESTful API.

spring-boot-camel-teiid-archetype

Demonstrates how to connect Apache Camel to a remote JBoss Data Virtualization (or Teiid) Server using the JDBC protocol.

spring-boot-camel-xml-archetype

Demonstrates how to configure Camel routes in Spring Boot via a Spring XML configuration file.

spring-boot-cxf-jaxrs-archetype

Demonstrates how to use Apache CXF with Spring Boot based on a fabric8 Java base image. The quickstart uses Spring Boot to configure an application that includes a CXF JAXRS endpoint with Swagger enabled.

spring-boot-cxf-jaxws-archetype

Demonstrates how to use Apache CXF with Spring Bootbased on a fabric8 Java base image. The quickstart uses Spring Boot to configure an application that includes a CXF JAXWS endpoint.

4.5. Camel Starter Modules

4.5.1. Overview

Starters are Apache Camel modules intended to be used in Spring Boot applications. There is a camel-xxx-starter module for each Camel component (with few exceptions listed below).

Starters meet the following requirements:

  • Allow auto-configuration of the component using native Spring Boot configuration system which is compatible with IDE tooling.
  • Allows auto-configuration of data formats and languages.
  • Manage transitive logging dependencies to integrate with Spring Boot logging system.
  • Include additional dependencies and align transitive dependencies to minimize the effort of creating a working Spring Boot application.

Each starter has its own integration test in tests/camel-itest-spring-boot, that verifies the compatibility with the current release of Spring Boot.

4.5.2. Using Camel Starter Modules

Apache Camel provides a starter module that allows you to develop Spring Boot applications using starters.

To use the Spring Boot starter:

  1. Add the following to your Spring Boot pom.xml file:

    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>
  2. Add classes with your Camel routes such as:

    package com.example;
    
    import org.apache.camel.builder.RouteBuilder;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyRoute extends RouteBuilder {
    
        @Override
        public void configure() throws Exception {
            from("timer:foo")
              .to("log:bar");
        }
    }

These routes will be started automatically.

Note

To keep the main thread blocked so that Camel stays up, either include the spring-boot-starter-web dependency, or add camel.springboot.main-run-controller=true to your application.properties or application.yml file.

You can customize the Camel application in the application.properties or application.yml file with camel.springboot.* properties.

4.6. Unsupported Starter Modules

The following components do not have a starter because of compatibility issues:

  • camel-blueprint (intended for OSGi only)
  • camel-cdi (intended for CDI only)
  • camel-core-osgi (intended for OSGi only)
  • camel-ejb (intended for JEE only)
  • camel-eventadmin (intended for OSGi only)
  • camel-ibatis (camel-mybatis-starter is included)
  • camel-jclouds
  • camel-mina (camel-mina2-starter is included)
  • camel-paxlogging (intended for OSGi only)
  • camel-quartz (camel-quartz2-starter is included)
  • camel-spark-rest
  • camel-swagger (camel-swagger-java-starter is included)