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 11, 2019
1 parent 4872cf8 commit ac51838
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 3 deletions.
Expand Up @@ -25,6 +25,9 @@

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.arc.processor.BuildExtension;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
Expand Down Expand Up @@ -222,11 +225,19 @@ public List<CamelRoutesBuilderClassBuildItem> discoverRoutesBuilderClassNames(
public List<CamelBeanBuildItem> collectRoutes(
List<CamelRoutesBuilderClassBuildItem> camelRoutesBuilders,
CamelMainRecorder recorder,
BeanRegistrationPhaseBuildItem beanRegistrationPhase,
RecorderContext recorderContext) {

final Set<DotName> arcBeanClasses = beanRegistrationPhase.getContext().get(BuildExtension.Key.BEANS)
.stream()
.map(BeanInfo::getImplClazz)
.map(ClassInfo::name)
.collect(Collectors.toSet());

final List<CamelBeanBuildItem> result = new ArrayList<CamelBeanBuildItem>();
camelRoutesBuilders.stream()
.map(CamelRoutesBuilderClassBuildItem::getDotName)
.filter(dotName -> !arcBeanClasses.contains(dotName))
.forEach(dotName -> {
final String className = dotName.toString();
final RuntimeValue<Object> value = recorderContext.newInstance(className);
Expand Down
@@ -0,0 +1,73 @@
/*
* 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.CamelContext;
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 addRoutesToCamelContext(CamelContext context) throws Exception {
CONFIGURE_COUNTER.incrementAndGet();
super.addRoutesToCamelContext(context);
}

@Override
public void configure() {

/*
* 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();
}

}
@@ -0,0 +1,34 @@
/*
* 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.enterprise.context.ApplicationScoped;

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

public int increment() {
return value.incrementAndGet();
}

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"));
}

}
Expand Up @@ -17,7 +17,7 @@
package org.apache.camel.quarkus.component.mail;

import java.util.Properties;
import javax.enterprise.inject.Produces;

import javax.mail.Session;

import org.apache.camel.builder.RouteBuilder;
Expand All @@ -35,7 +35,6 @@ public void configure() {
.to("smtp://localhost?initialDelay=100&delay=100");
}

@Produces
MailComponent smtp() {
MailComponent mail = new MailComponent(getContext());
Session session = Session.getInstance(new Properties());
Expand Down

0 comments on commit ac51838

Please sign in to comment.