Fuse example project with Spring Boot and Maven

This tutorial will discuss how to create a basic Red Hat Fuse Integration Project from the command line, using Maven archetypes using Spring Boot as container.

The list of Red Hat Fuse Maven archetypes are available here: https://maven.repository.redhat.com/ga/io/fabric8/archetypes/archetypes-catalog/

We will use the archetype version 2.2.0.fuse-sb2-780040-redhat-00002 to create a sample 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.0.fuse-sb2-780040-redhat-00002/archetypes-catalog-2.2.0.fuse-sb2-780040-redhat-00002-archetype-catalog.xml \   
   -DarchetypeGroupId=org.jboss.fuse.fis.archetypes \  
   -DarchetypeArtifactId=spring-boot-camel-xml-archetype \   
   -DarchetypeVersion=2.2.0.fuse-sb2-780040-redhat-00002

When the archetype generation begins, you will be asked to specify the GAV settings for your project:

Define value for property 'groupId': : com.example
Define value for property 'artifactId': : fuse-demo
Define value for property 'version':  1.0-SNAPSHOT: : 
Define value for property 'package':  com.example: : 
Confirm properties configuration:
groupId: com.example
artifactId: fuse-demo
version: 1.0-SNAPSHOT
package: com.example
 Y: : Y

The following project structure has been created:

├── configuration
│   └── settings.xml
├── LICENSE.md
├── pom.xml
└── src
    ├── data
    ├── main
    │   ├── fabric8
    │   │   └── deployment.yml
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           ├── Application.java
    │   │           └── MyTransformer.java
    │   └── resources
    │       ├── application.properties
    │       ├── logback.xml
    │       └── spring
    │           └── camel-context.xml
    └── test
        └── java
            └── com
                └── example

Here is the camel-context.xml file which contains the example Route:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <!-- Define a traditional camel context here -->
  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route id="simple-route">
      <from id="route-timer" uri="timer:foo?period=2000"/>
      <transform id="route-transform">
        <method ref="myTransformer"/>
      </transform>
      <log id="route-log" message=">>> ${body}"/>
    </route>
  </camelContext>

</beans>

Within the route, a Bean named “myTransformer” is referenced:

@Component(value = "myTransformer")
public class MyTransformer {

    public String transform() {
        // let's return a random string
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < 3; i++) {
            int number = (int) (Math.round(Math.random() * 1000) % 10);
            char letter = (char) ('0' + number);
            buffer.append(letter);
        }
        return buffer.toString();
    }

}

Building and Running the Fuse application

In order to build the application, you need to include either in your settings.xml or in your pom.xml the Repositories where the Red Hat libraries can be found:

  <repositories>
    <repository>
      <id>red-hat-ga-repository</id>
      <url>https://maven.repository.redhat.com/ga</url>
    </repository>
    <repository>
      <id>red-hat-early-access-repository</id>
      <url>https://maven.repository.redhat.com/earlyaccess/all</url>
    </repository>
    <repository>
      <id>jboss-early-access-repository</id>
      <url>https://repository.jboss.org/nexus/content/groups/ea</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <id>red-hat-ga-repository</id>
      <url>https://maven.repository.redhat.com/ga</url>
    </pluginRepository>
    <pluginRepository>
      <id>red-hat-early-access-repository</id>
      <url>https://maven.repository.redhat.com/earlyaccess/all</url>
    </pluginRepository>
    <pluginRepository>
      <id>jboss-early-access-repository</id>
      <url>https://repository.jboss.org/nexus/content/groups/ea</url>
    </pluginRepository>
  </pluginRepositories>

Here’s how you can build it and run it:

$ mvn package spring-boot:run

Here is your Fuse Spring Boot project up and running:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.4.RELEASE)

13:23:16.693 [main] INFO  com.example.Application - Starting Application on fedora with PID 14922 (/home/francesco/tmp/fuse-demo/target/classes started by francesco in /home/francesco/tmp/fuse-demo)
13:23:16.696 [main] INFO  com.example.Application - No active profile set, falling back to default profiles: default
13:23:18.677 [main] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.apache.camel.spring.boot.CamelAutoConfiguration' of type [org.apache.camel.spring.boot.CamelAutoConfiguration$$EnhancerBySpringCGLIB$$f223bcde] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
13:23:18.932 [main] WARN  io.undertow.websockets.jsr - UT026010: Buffer pool was not set on WebSocketDeploymentInfo, the default pool will be used
13:23:18.966 [main] INFO  io.undertow.servlet - Initializing Spring embedded WebApplicationContext
13:23:18.966 [main] INFO  o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 2228 ms
13:23:19.493 [main] INFO  o.a.c.i.c.DefaultTypeConverter - Type converters loaded (core: 195, classpath: 1)
13:23:19.671 [main] INFO  o.s.s.c.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'
13:23:20.165 [main] INFO  io.undertow - starting server: Undertow - 2.0.30.SP4-redhat-00001
13:23:20.173 [main] INFO  org.xnio - XNIO version 3.5.9.Final
13:23:20.180 [main] INFO  org.xnio.nio - XNIO NIO Implementation Version 3.5.9.Final
13:23:20.269 [main] INFO  o.s.b.w.e.undertow.UndertowWebServer - Undertow started on port(s) 8080 (http)
13:23:20.339 [main] INFO  io.undertow.servlet - Initializing Spring embedded WebApplicationContext
13:23:20.339 [main] INFO  o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 66 ms
13:23:20.354 [main] INFO  o.s.b.a.e.web.EndpointLinksResolver - Exposing 2 endpoint(s) beneath base path '/actuator'
13:23:20.391 [main] INFO  io.undertow - starting server: Undertow - 2.0.30.SP4-redhat-00001
13:23:20.393 [main] INFO  o.s.b.w.e.undertow.UndertowWebServer - Undertow started on port(s) 8081 (http)
13:23:20.409 [main] INFO  o.a.camel.spring.SpringCamelContext - Apache Camel 2.23.2.fuse-780036-redhat-00001 (CamelContext: MyCamel) is starting
13:23:20.410 [main] INFO  o.a.c.m.ManagedManagementStrategy - JMX is enabled
13:23:20.581 [main] INFO  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
13:23:20.613 [main] INFO  o.a.camel.spring.SpringCamelContext - Route: simple-route started and consuming from: timer://foo?period=2000
13:23:20.614 [main] INFO  o.a.camel.spring.SpringCamelContext - Total 1 routes, of which 1 are started
13:23:20.616 [main] INFO  o.a.camel.spring.SpringCamelContext - Apache Camel 2.23.2.fuse-780036-redhat-00001 (CamelContext: MyCamel) started in 0.206 seconds
13:23:20.639 [main] INFO  com.example.Application - Started Application in 4.497 seconds (JVM running for 7.871)
13:23:21.628 [Camel (MyCamel) thread #1 - timer://foo] INFO  simple-route - >>> 815

We have just covered how to create a Fuse Spring Boot project from the Command Line.

Continue learning, how to create a Fuse Project using Code Ready Studio: Creating JBoss Fuse Spring Boot projects with Red Hat Code Ready Studio