Camel Integration
Introduction
Apache Camel is a powerful, lightweight integration framework that enables developers to seamlessly connect diverse systems and applications using well-established Enterprise Integration Patterns (EIPs). It simplifies complex integration challenges by providing a domain-specific language (DSL) to define routing and mediation rules in a readable, declarative manner.
Camel Integration Support
For Domain Services based on Java Spring Boot Stack 2.0, IBM Industry Solutions Workbench provides various capabilities, to easily integrate with the help of the Opensource integration framework Apache Camel
The extension is available for the following project types:
Enable Extension
Before using Apache Camel, you need to enable the Camel extension in the k5-project.yaml
file.
.....
camelIntegrationSupport:
enabled: true
The Camel extension will be automatically enabled when the Saga extension is enabled.
POM Dependencies
When camel integration extension is enabled the following dependencies will be added automatically to your project pom.xml file
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-lra-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-log</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-security-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet</artifactId>
</dependency>
</dependencies>
Servlet Initialization
If you want to customize any servlet property for your local development edit application-local.yaml
camel:
servlet:
mapping:
enabled: true
context-path: /camel/*
servlet-name: customServlet
Deployment
You can customize the same properties for single deployment using API Configuration Management.
Use POST /api/cfg/v1/runtimes/{runtimeName}/solutions/{solutionAcronym}/configurations/{configurationName}
with body like the following
extraConfiguration:
camel:
servlet:
mapping:
enabled: true
context-path: /camel/*
servlet-name: customServlet
For application composition use Configuring components with Custom configuration
like the following
extraConfiguration:
camel:
servlet:
mapping:
enabled: true
context-path: /camel/*
servlet-name: customServlet
Camel Security
If you want to secure your camel routes you can enable camel security. Enable secure camel will make sure that all your camel routes like "servicePath/camel/firstRoute" are protected with authorization token
Check Servlet Initialization to customize your context path.
If you want to enable security for your local development edit application-local.yaml
feature:
secure-camel:
enabled: false
Deployment
You can enable security for single deployment using API Configuration Management.
Use POST /api/cfg/v1/runtimes/{runtimeName}/solutions/{solutionAcronym}/configurations/{configurationName}
extraConfiguration:
feature:
secure-camel:
enabled: false
For application composition use Configuring components with Custom configuration
like the following
extraConfiguration:
feature:
secure-camel:
enabled: false
Implement Camel Routes
Apache Camel is a powerful integration framework for Java Spring Boot applications, designed to simplify message routing and processing, an implementation stub is generated under "src/main/java/yourProject/camel/camelRoutes.java
Example:
@Component
public class CamelRoutes extends RouteBuilder {
@Override
public void configure() throws Exception {
// Using Rest DSL
rest("/")
.get("order/{id}?matchOnUriPrefix=true").to("direct:getOrder")
.post("order/{id}?matchOnUriPrefix=true").to("direct:postOrder");
// Using Direct rest
from("rest:post:/postOrderRest?matchOnUriPrefix=true")
.routeId("postOrderRest")
.log("REST: Processing new order: ${body}")
.to("direct:saveOrder");
// Direct
from("direct:saveOrder")
.routeId("saveOrder")
.log("Saving new order: ${body}")
.to("mock:database");
// Direct
from("direct:getOrder")
.routeId("getOrder")
.log("Direct: Getting order: ${header.id}")
.setBody(simple("Direct: Get Order Details for ID ${header.id}"));
// Direct
from("direct:postOrder")
.routeId("postOrder")
.log("Direct: Posting order: ${header.id}")
.setBody(simple("Direct: Post Order Details for ID ${header.id}"));
// secured servlet
from("servlet:/secured").process(new Processor() {
public void process(Exchange exchange) throws Exception {
// Access HTTP headers sent by the client
Message message = exchange.getMessage();
String contentType = message.getHeader(Exchange.CONTENT_TYPE, String.class);
String httpUri = message.getHeader(Exchange.HTTP_URI, String.class);
String authHeader = message.getHeader("Authorization", String.class);
// Set the response body
message.setBody("<b>Got Token: " + authHeader + "Content-Type: " + contentType + ", URI: " + httpUri + "</b>");
}
});
}
The Camel integration is only available for Domain Services (Java) based on Java Spring Boot Stack 2.0