Skip to content

Commit

Permalink
Fix #136 @ConfigProperty and @Inject do not work in RouteBuilders
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Nov 8, 2019
1 parent 396e008 commit b14487b
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 43 deletions.
Expand Up @@ -186,18 +186,6 @@ CamelRuntimeRegistryBuildItem bindRuntimeBeansToRegistry(
* disabled by setting quarkus.camel.disable-main = true
*/
public static class Main {
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
public List<CamelRoutesBuilderBuildItem> collectRoutes(
CombinedIndexBuildItem combinedIndex,
CamelMainRecorder recorder,
RecorderContext recorderContext) {

return CamelSupport.getRouteBuilderClasses(combinedIndex.getIndex())
.map(recorderContext::<RoutesBuilder> newInstance)
.map(CamelRoutesBuilderBuildItem::new)
.collect(Collectors.toList());
}

@Overridable
@BuildStep
Expand All @@ -207,8 +195,16 @@ public CamelRoutesCollectorBuildItem createRoutesCollector(CamelMainRecorder rec
}

@BuildStep(onlyIf = Flags.MainEnabled.class)
void beans(BuildProducer<AdditionalBeanBuildItem> beanProducer) {
void beans(BuildProducer<AdditionalBeanBuildItem> beanProducer, CombinedIndexBuildItem combinedIndex) {
beanProducer.produce(AdditionalBeanBuildItem.unremovableOf(CamelMainProducers.class));

final List<String> routeBuilderClasses = CamelSupport.getRouteBuilderClasses(combinedIndex.getIndex())
.collect(Collectors.toList());
beanProducer.produce(
AdditionalBeanBuildItem.builder()
.addBeanClasses(routeBuilderClasses)
.setUnremovable()
.build());
}

@Overridable
Expand All @@ -232,7 +228,6 @@ CamelMainBuildItem main(
CamelContextBuildItem context,
CamelRoutesCollectorBuildItem routesCollector,
List<CamelMainListenerBuildItem> listeners,
List<CamelRoutesBuilderBuildItem> routesBuilders,
BeanContainerBuildItem beanContainer) {

RuntimeValue<CamelMain> main = recorder.createCamelMain(
Expand All @@ -243,9 +238,6 @@ CamelMainBuildItem main(
for (CamelMainListenerBuildItem listener : listeners) {
recorder.addListener(main, listener.getListener());
}
for (CamelRoutesBuilderBuildItem routesBuilder : routesBuilders) {
recorder.addRouteBuilder(main, routesBuilder.getInstance());
}

return new CamelMainBuildItem(main);
}
Expand Down
Expand Up @@ -66,18 +66,6 @@ public void addRouteBuilder(
}
}

public void addRouteBuilder(
RuntimeValue<CamelMain> main,
RuntimeValue<RoutesBuilder> routesBuilder) {

try {
main.getValue().addRoutesBuilder(routesBuilder.getValue());
} catch (Exception e) {
throw new RuntimeException("Could not add route builder '" + routesBuilder.getValue().getClass().getName() + "'",
e);
}
}

public void addListener(RuntimeValue<CamelMain> main, RuntimeValue<MainListener> listener) {
main.getValue().addMainListener(listener.getValue());
}
Expand Down
@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.component.bean;

import java.util.concurrent.atomic.AtomicInteger;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.apache.camel.builder.RouteBuilder;
import org.eclipse.microprofile.config.inject.ConfigProperty;

@ApplicationScoped
public class AppScopedRouteBuilder extends RouteBuilder {

static final AtomicInteger INSTANCE_COUNTER = new AtomicInteger(0);
static final AtomicInteger CONFIGURE_COUNTER = new AtomicInteger(0);

@Inject
Counter counter;

@ConfigProperty(name = "my.foo.property", defaultValue = "not found")
String myFooValue;

@PostConstruct
public void postConstruct() {
INSTANCE_COUNTER.incrementAndGet();
}

@Override
public void configure() {
CONFIGURE_COUNTER.incrementAndGet();

/*
* counter and config-property should actually work without the bean extension. Doing it here because we have
* quarkus.camel.enable-main=true in the core itest
*/
from("direct:counter")
.id("counter")
.setBody(() -> counter.increment())
.to("log:counter");
from("direct:config-property")
.id("config-property")
.setBody(() -> "myFooValue = " + myFooValue)
.to("log:config-property");
}

public Counter getCounter() {
return counter;
}

}
Expand Up @@ -19,6 +19,7 @@
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
Expand All @@ -32,11 +33,60 @@ public class CamelResource {
@Inject
ProducerTemplate template;

@Inject
Counter counter;

@Inject
AppScopedRouteBuilder routeBuilder;

@Path("/process-order")
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.TEXT_PLAIN)
public String processOrder(String statement) {
return template.requestBody("direct:process-order", statement, String.class);
}

@Path("/increment")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String increment() {
return template.requestBody("direct:counter", null, String.class);
}

@Path("/counter")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int counter() {
return counter.getValue();
}

@Path("/config-property")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String configProperty() {
return template.requestBody("direct:config-property", null, String.class);
}

@Path("/route-builder-instance-counter")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int routeBuilderInstanceCounter() {
return AppScopedRouteBuilder.INSTANCE_COUNTER.get();
}

@Path("/route-builder-configure-counter")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int routeBuilderConfigureCounter() {
return AppScopedRouteBuilder.CONFIGURE_COUNTER.get();
}

@Path("/route-builder-injected-count")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int routeBuilderInjectedCount() {
return routeBuilder.getCounter().getValue();
}

}
Expand Up @@ -14,23 +14,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.core.deployment;
package org.apache.camel.quarkus.component.bean;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.RoutesBuilder;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Holds the {@link RoutesBuilder} {@link RuntimeValue}.
*/
public final class CamelRoutesBuilderBuildItem extends MultiBuildItem {
private final RuntimeValue<RoutesBuilder> routesBuilder;
import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class Counter {
private final AtomicInteger value = new AtomicInteger(0);

public CamelRoutesBuilderBuildItem(RuntimeValue<RoutesBuilder> routesBuilder) {
this.routesBuilder = routesBuilder;
public int increment() {
return value.incrementAndGet();
}

public RuntimeValue<RoutesBuilder> getInstance() {
return routesBuilder;
public int getValue() {
return value.get();
}
}
Expand Up @@ -27,4 +27,7 @@ quarkus.camel.dump-routes=true
#
# Camel
#
camel.context.name = quarkus-camel-example
camel.context.name = quarkus-camel-example

# A test value
my.foo.property = foo
Expand Up @@ -31,4 +31,39 @@ public void testRoutes() {
.body(equalTo("{success=true, lines=[(id=1,item=nuts), (id=2,item=bolts)]}"));
}

@Test
public void inject() {

/* Ensure that @Inject works */
RestAssured.when().get("/bean/counter").then().body(equalTo("0"));
RestAssured.when().get("/bean/route-builder-injected-count").then().body(equalTo("0"));
RestAssured.when().get("/bean/increment").then().body(equalTo("1"));
RestAssured.when().get("/bean/counter").then().body(equalTo("1"));
RestAssured.when().get("/bean/route-builder-injected-count").then().body(equalTo("1"));
RestAssured.when().get("/bean/increment").then().body(equalTo("2"));
RestAssured.when().get("/bean/counter").then().body(equalTo("2"));
RestAssured.when().get("/bean/route-builder-injected-count").then().body(equalTo("2"));

/* Ensure that @ConfigProperty works */
RestAssured.when()
.get("/bean/config-property")
.then()
.statusCode(200)
.body(equalTo("myFooValue = foo"));

/* Ensure that the bean was not instantiated multiple times */
RestAssured.when()
.get("/bean/route-builder-instance-counter")
.then()
.statusCode(200)
.body(equalTo("1"));

/* Ensure that the RoutesBuilder.configure() was not called multiple times */
RestAssured.when()
.get("/bean/route-builder-configure-counter")
.then()
.statusCode(200)
.body(equalTo("1"));
}

}

0 comments on commit b14487b

Please sign in to comment.