Skip to content

Commit

Permalink
yaml-dsl: enable flow mode deserialization by default
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jun 9, 2021
1 parent 2ec6ab7 commit b3e53c5
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/modules/ROOT/pages/reference/extensions/yaml-dsl.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,30 @@ Or add the coordinates to your existing project:
----

Check the xref:user-guide/index.adoc[User guide] for more information about writing Camel Quarkus applications.

== Additional Camel Quarkus configuration

[width="100%",cols="80,5,15",options="header"]
|===
| Configuration property | Type | Default


|icon:lock[title=Fixed at build time] [[quarkus.camel.yaml.flow-mode]]`link:#quarkus.camel.yaml.flow-mode[quarkus.camel.yaml.flow-mode]`

If `true` the YAML DSL support flow-mode which allow to write more concise routes as for EIPs that have their own output like filter, aggregate, split, etc. the `steps` element can be omitted an in that case, the next processing step is automatically wired to the EIP's outputs.
As example, a YAML DSL to process only the timer events from 5 to 10 would look like: `- from:
uri: "timer:tick"
steps:
- filter:
simple: "$++{++exchangeProperty.CamelTimerCounter` range '5..10'" steps: - to: "direct:filtered" ++}++ With the flow mode enabled the same logic can be expressed in a more concise way: `- from:
uri: "kamelet:source"
steps:
- filter:
simple: "$++{++exchangeProperty.CamelTimerCounter` range '5..10'" - to: "kamelet:sink" ++}++
| `boolean`
| `true`
|===

[.configuration-legend]
icon:lock[title=Fixed at build time] Configuration property fixed at build time. All other configuration properties are overridable at runtime.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
package org.apache.camel.quarkus.dsl.yaml.deployment;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.FeatureBuildItem;
import org.apache.camel.dsl.yaml.common.YamlDeserializationMode;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem;
import org.apache.camel.quarkus.dsl.yaml.YamlDslConfiguration;
import org.apache.camel.quarkus.dsl.yaml.YamlDslRecorder;

public class YamlDslProcessor {
private static final String FEATURE = "camel-yaml-dsl";
Expand All @@ -27,4 +33,16 @@ public class YamlDslProcessor {
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

@BuildStep
@Record(value = ExecutionTime.STATIC_INIT)
CamelContextCustomizerBuildItem enableFlowMode(
YamlDslRecorder recorder,
YamlDslConfiguration configuration) {
return new CamelContextCustomizerBuildItem(
recorder.setYamlDeserializationMode(
configuration.flowMode
? YamlDeserializationMode.FLOW.name()
: YamlDeserializationMode.CLASSIC.name()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.apache.camel.quarkus.dsl.yaml;

import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;

@ConfigRoot(name = "camel.yaml", phase = ConfigPhase.BUILD_TIME)
public class YamlDslConfiguration {
/**
* If {@code true} the YAML DSL support flow-mode which allow to write more concise routes as for EIPs that have
* their own output like filter, aggregate, split, etc. the {@code steps} element can be omitted an in that case,
* the next processing step is automatically wired to the EIP's outputs.
* <p/>
* As example, a YAML DSL to process only the timer events from 5 to 10 would look like:
*
* <pre>
* {@code
* - from:
* uri: "timer:tick"
* steps:
* - filter:
* simple: "${exchangeProperty.CamelTimerCounter} range '5..10'"
* steps:
* - to: "direct:filtered"
* }
* </pre>
*
* With the flow mode enabled the same logic can be expressed in a more concise way:
*
* <pre>
* {@code
* - from:
* uri: "kamelet:source"
* steps:
* - filter:
* simple: "${exchangeProperty.CamelTimerCounter} range '5..10'"
* - to: "kamelet:sink"
* }
* </pre>
*
*
*/
@ConfigItem(defaultValue = "true")
public boolean flowMode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.apache.camel.quarkus.dsl.yaml;

import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import org.apache.camel.CamelContext;
import org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader;
import org.apache.camel.spi.CamelContextCustomizer;

@Recorder
public class YamlDslRecorder {
public RuntimeValue<CamelContextCustomizer> setYamlDeserializationMode(String mode) {
return new RuntimeValue<>(new CamelContextCustomizer() {
@Override
public void configure(CamelContext camelContext) {
camelContext.getGlobalOptions().put(
YamlRoutesBuilderLoader.DESERIALIZATION_MODE,
mode);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.camel.quarkus.main;

import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.json.Json;
Expand All @@ -39,6 +41,7 @@ public class CoreMainYamlResource {
@Path("/main/describe")
@GET
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("unchecked")
public JsonObject describeMain() {
final ExtendedCamelContext camelContext = main.getCamelContext().adapt(ExtendedCamelContext.class);

Expand All @@ -57,6 +60,7 @@ public JsonObject describeMain() {
.findClass(YamlRoutesBuilderLoader.EXTENSION).get().getName())
.add("routeBuilders", routeBuilders)
.add("routes", routes)
.add("global-options", Json.createObjectBuilder((Map) main.getCamelContext().getGlobalOptions()).build())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import org.apache.camel.dsl.yaml.YamlRoutesBuilderLoader;
import org.apache.camel.dsl.yaml.common.YamlDeserializationMode;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -45,5 +46,7 @@ public void testMainInstanceWithYamlRoutes() {
.isEmpty();
assertThat(p.getList("routes", String.class))
.contains("my-yaml-route", "rest-route");
assertThat(p.getMap("global-options", String.class, String.class))
.containsEntry(YamlRoutesBuilderLoader.DESERIALIZATION_MODE, YamlDeserializationMode.FLOW.name());
}
}

0 comments on commit b3e53c5

Please sign in to comment.