V0.0.1 Simple Spring-boot Server Defined by Camel XML Routing

master
Ng Yat Yan 4 months ago
commit 58de3d3b14

4
.gitignore vendored

@ -0,0 +1,4 @@
/target/
/.classpath
/.project
/.settings

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="STDOUT"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/server.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/server.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<maxHistory>3</maxHistory>
<totalSizeCap>300MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.example.camel" level="DEBUG" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

@ -0,0 +1,2 @@
server:
port: 9090

@ -0,0 +1,92 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>camel-springboot-xml-example</artifactId>
<version>0.0.1</version>
<name>camel-springboot-xml-example</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.2</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<camel.version>4.7.0</camel.version>
<springboot.version>${project.parent.version}</springboot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-bom</artifactId>
<version>${camel.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-bom</artifactId>
<version>${camel.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-xml-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jsonpath</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.example.camel.Boot</mainClass>
</configuration>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,31 @@
package com.example.camel;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource("classpath:camel-route.xml")
public class Boot {
private static final Logger logger = LoggerFactory.getLogger(Boot.class);
public static void main(String[] args) {
String configDirectory = "conf";
if (args.length > 0) {
configDirectory = args[0];
}
logger.info("config directory: {}", configDirectory);
if (new File(configDirectory).exists() && new File(configDirectory).isDirectory()) {
System.setProperty("spring.config.location", configDirectory + "/springboot.yml");
System.setProperty("logging.config", configDirectory + "/logback.xml");
}
SpringApplication.run(Boot.class, args);
}
}

@ -0,0 +1,90 @@
<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">
<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
<route id="example-http-inbound">
<from uri="jetty:http://0.0.0.0:9090/example" />
<convertBodyTo type="java.lang.String" />
<log message="[EXAMPLE INBOUND] Received: ${body}" />
<choice>
<when>
<simple>${headers.CamelHttpMethod} == 'POST'</simple>
<setHeader name="type">
<jsonpath>$.notification.type</jsonpath>
</setHeader>
<choice>
<when>
<simple>${header.type} == 'email'</simple>
<log message="[EXAMPLE INBOUND] Received email notification" />
<to uri="direct:email" />
<setHeader name="Exchange.HTTP_RESPONSE_CODE">
<constant>200</constant>
</setHeader>
</when>
<when>
<simple>${header.type} == 'http'</simple>
<log message="[EXAMPLE INBOUND] Received http notification" />
<to uri="direct:http" />
<setHeader name="Exchange.HTTP_RESPONSE_CODE">
<constant>200</constant>
</setHeader>
</when>
<otherwise>
<log message="[EXAMPLE INBOUND] Unknown notification" />
<setBody>
<constant>{ "status": "reject", "type": "unknown" }</constant>
</setBody>
<setHeader name="Exchange.HTTP_RESPONSE_CODE">
<constant>400</constant>
</setHeader>
</otherwise>
</choice>
</when>
<otherwise>
<log
message="[EXAMPLE INBOUND] only POST is accepted (${headers.CamelHttpMethod})" />
<setBody>
<constant>{ "error": "only POST is accepted" }</constant>
</setBody>
<setHeader name="Exchange.HTTP_RESPONSE_CODE">
<constant>500</constant>
</setHeader>
</otherwise>
</choice>
</route>
<route id="example-email">
<from uri="direct:email" />
<log message="[EXAMPLE EMAIL] Sending notification email" />
<setHeader name="to">
<jsonpath>$.notification.to</jsonpath>
</setHeader>
<setHeader name="subject">
<constant>Notification</constant>
</setHeader>
<setBody>
<jsonpath>$.notification.message</jsonpath>
</setBody>
<!-- <to uri="smtp://localhost"/> -->
<setBody>
<simple>{ "status": "email sent", "to": "${header.to}", "subject":
"${header.subject}" }</simple>
</setBody>
</route>
<route id="example-http">
<from uri="direct:http" />
<log message="[EXAMPLE HTTP] Sending http notification" />
<setHeader name="service">
<jsonpath>$.notification.service</jsonpath>
</setHeader>
<!-- <to uri="jetty:..." /> -->
<setBody>
<simple>{ "status": "http requested", "service": "${header.service}"
}</simple>
</setBody>
</route>
</camel:camelContext>
</beans>
Loading…
Cancel
Save