...
AbstractCamelRunner
will make these properties available to your RouteBuilders with help of Camel's PropertiesComponent and it will also inject these values into your Service Component's and RouteBuilder's fields when their names match. The fields can be declared with any visibility level, and many types are supported (String, int, boolean, URL, ...).
Below is an example of a RouteBuilder class generated by camel-archetype-scr
:
Code Block | ||||
---|---|---|---|---|
| ||||
// This file was generated from org.apache.camel.archetypes/camel-archetype-scr/2.15-SNAPSHOT
package example.internal;
import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.commons.lang.Validate;
public class CamelScrExampleRoute extends RouteBuilder {
SimpleRegistry registry;
// Configured fields
private String camelRouteId;
private Integer maximumRedeliveries;
private Long redeliveryDelay;
private Double backOffMultiplier;
private Long maximumRedeliveryDelay;
public CamelScrExampleRoute(final SimpleRegistry registry) {
this.registry = registry;
}
@Override
public void configure() throws Exception {
checkProperties();
// Add a bean to Camel context registry
registry.put("test", "bean");
errorHandler(defaultErrorHandler()
.retryAttemptedLogLevel(LoggingLevel.WARN)
.maximumRedeliveries(maximumRedeliveries)
.redeliveryDelay(redeliveryDelay)
.backOffMultiplier(backOffMultiplier)
.maximumRedeliveryDelay(maximumRedeliveryDelay));
from("{{from}}")
.startupOrder(2)
.routeId(camelRouteId)
.onCompletion()
.to("direct:processCompletion")
.end()
.removeHeaders("CamelHttp*")
.to("{{to}}");
from("direct:processCompletion")
.startupOrder(1)
.routeId(camelRouteId + ".completion")
.choice()
.when(simple("${exception} == null"))
.log("{{messageOk}}")
.otherwise()
.log(LoggingLevel.ERROR, "{{messageError}}")
.end();
}
}
public void checkProperties() {
Validate.notNull(camelRouteId, "camelRouteId property is not set");
Validate.notNull(maximumRedeliveries, "maximumRedeliveries property is not set");
Validate.notNull(redeliveryDelay, "redeliveryDelay property is not set");
Validate.notNull(backOffMultiplier, "backOffMultiplier property is not set");
Validate.notNull(maximumRedeliveryDelay, "maximumRedeliveryDelay property is not set");
}
} |
Let's take a look at CamelScrExampleRoute in more detail.
Code Block |
---|
// Configured fields
private String camelRouteId;
private Integer maximumRedeliveries;
private Long redeliveryDelay;
private Double backOffMultiplier;
private Long maximumRedeliveryDelay; |
The values of these fields are set with values from properties by matching their names.
Code Block |
---|
// Add a bean to Camel context registry
registry.put("test", "bean"); |
If you need to add some beans to CamelContext's registry for your routes, you can do it like this.
Code Block |
---|
public void checkProperties() {
Validate.notNull(camelRouteId, "camelRouteId property is not set");
Validate.notNull(maximumRedeliveries, "maximumRedeliveries property is not set");
Validate.notNull(redeliveryDelay, "redeliveryDelay property is not set");
Validate.notNull(backOffMultiplier, "backOffMultiplier property is not set");
Validate.notNull(maximumRedeliveryDelay, "maximumRedeliveryDelay property is not set");
} |
It is a good idea to check that required parameters are set and they have meaningful values before allowing the routes to start.
Code Block |
---|
from("{{from}}")
.startupOrder(2)
.routeId(camelRouteId)
.onCompletion()
.to("direct:processCompletion")
.end()
.removeHeaders("CamelHttp*")
.to("{{to}}");
from("direct:processCompletion")
.startupOrder(1)
.routeId(camelRouteId + ".completion")
.choice()
.when(simple("${exception} == null"))
.log("{{messageOk}}")
.otherwise()
.log(LoggingLevel.ERROR, "{{messageError}}")
.end(); |
Note that pretty much everything in the route is configured with properties. This essentially makes your RouteBuilder a template. SCR allows you to create more instances of your routes just by providing alternative configurations. More on this in section Using Camel SCR bundle as a template.
AbstractCamelRunner's lifecycle in SCR
...
Here we send a message to a route in test.
Using Camel SCR bundle as a template
TODO