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 a189091 commit ac39109
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 1 deletion.
Expand Up @@ -30,6 +30,11 @@
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.deployment.ApplicationArchive;
import io.quarkus.arc.deployment.BeanRegistrationPhaseBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem.BeanClassNamesExclusion;
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 @@ -284,11 +289,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 All @@ -312,6 +325,18 @@ void beans(BuildProducer<AdditionalBeanBuildItem> beanProducer) {
beanProducer.produce(AdditionalBeanBuildItem.unremovableOf(CamelMainProducers.class));
}

@BuildStep(onlyIf = Flags.MainEnabled.class)
UnremovableBeanBuildItem unremoveLazyBeans(
List<CamelRoutesBuilderClassBuildItem> camelRoutesClasses) {

final Set<String> lazyBeans = camelRoutesClasses.stream()
.map(buildItem -> buildItem.getDotName().toString())
.collect(Collectors.toSet());

return new UnremovableBeanBuildItem(new BeanClassNamesExclusion(lazyBeans));

}

@Overridable
@Record(value = ExecutionTime.RUNTIME_INIT, optional = true)
@BuildStep(onlyIf = Flags.MainEnabled.class)
Expand Down
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,81 @@ public class CamelResource {
@Inject
ProducerTemplate template;

@Inject
Counter counter;

@Inject
EagerAppScopedRouteBuilder 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 EagerAppScopedRouteBuilder.INSTANCE_COUNTER.get();
}

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

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

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

@Path("/camel-configure-counter")
@GET
@Produces(MediaType.TEXT_PLAIN)
public int lazyConfigureCounter() {
return CamelRoute.CONFIGURE_COUNTER.get();
}

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

}
Expand Up @@ -21,18 +21,31 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicInteger;

import io.quarkus.runtime.annotations.RegisterForReflection;
import org.apache.camel.AsyncCallback;
import org.apache.camel.AsyncProcessor;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.bean.BeanProcessor;
import org.apache.camel.support.DefaultExchange;

/**
* A {@link RouteBuilder} instantiated by Camel (not by Arc).
*/
public class CamelRoute extends RouteBuilder {

static final AtomicInteger CONFIGURE_COUNTER = new AtomicInteger(0);

@Override
public void addRoutesToCamelContext(CamelContext context) throws Exception {
CONFIGURE_COUNTER.incrementAndGet();
super.addRoutesToCamelContext(context);
}

@Override
public void configure() {
from("direct:process-order")
Expand Down
@@ -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();
}
}
@@ -0,0 +1,76 @@
/*
* 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;

/**
* A {@link RouteBuilder} injected into {@link CamelResource} and thus instantiated eagerly.
*/
@ApplicationScoped
public class EagerAppScopedRouteBuilder 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;
}

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

import org.apache.camel.builder.RouteBuilder;

/**
* An @ApplicationScoped {@link RouteBuilder} that would normally get removed from the CDI container because it is not
* injected anywhere and because it has no observer method, etc.
*/
@ApplicationScoped
public class LazyAppScopedRouteBuilder extends RouteBuilder {

@Override
public void configure() {
from("direct:lazy")
.setBody(() -> "lazy");
}

}
@@ -0,0 +1,48 @@
/*
* 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 javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

import org.apache.camel.builder.RouteBuilder;

/**
* A {@link RouteBuilder} with a @Produces method that makes it a CDI bean. It would normally get removed from the CDI
* container.
*/
public class RouteBuilderWithProducer extends RouteBuilder {

@Override
public void configure() {
from("direct:with-producer")
.setBody(() -> "with-producer");
}

@Produces
@ApplicationScoped
Hello hello() {
return new Hello();
}

public static class Hello {
public String getHello() {
return "hello";
}
}

}
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

0 comments on commit ac39109

Please sign in to comment.