Skip to content

Commit

Permalink
Add a workaround for Camel's DI (see CAMEL-14271)
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Dec 7, 2019
1 parent 5b8497a commit bd646e7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import javax.inject.Named;

import io.quarkus.test.QuarkusUnitTest;
import org.apache.camel.BindToRegistry;
import org.apache.camel.Processor;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.Registry;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
Expand All @@ -34,17 +38,24 @@ public class CamelRegistryTest {
@RegisterExtension
static final QuarkusUnitTest CONFIG = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(BeanProducer.class));
.addClasses(BeanProducer.class, MyRoute.class));


@Inject
Registry registry;

@Test
public void testLookupRoutes() {
assertThat(registry.findByType(RoutesBuilder.class)).isNotEmpty();
}

@Test
public void testLookupByName() {
assertThat(registry.lookupByName("bean-1")).isInstanceOfSatisfying(String.class, s -> assertThat(s).isEqualTo("a"));
assertThat(registry.lookupByName("bean-2")).isInstanceOfSatisfying(String.class, s -> assertThat(s).isEqualTo("b"));
assertThat(registry.lookupByNameAndType("bean-1", String.class)).isEqualTo("a");
assertThat(registry.lookupByNameAndType("bean-2", String.class)).isEqualTo("b");
assertThat(registry.lookupByName("myProcessor")).isInstanceOf(Processor.class);
}

@Test
Expand All @@ -69,4 +80,16 @@ public String bean2() {
return "b";
}
}

public static class MyRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
}

@BindToRegistry
public Processor myProcessor() {
return e -> {
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@

import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.main.BaseMainSupport;
import org.apache.camel.main.MainConfigurationProperties;
import org.apache.camel.main.MainListener;
import org.apache.camel.spi.CamelBeanPostProcessor;
import org.apache.camel.support.service.ServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -44,13 +47,32 @@ protected void doStart() throws Exception {
}

postProcessCamelContext(getCamelContext());

//
// TODO: this is required as the bean post processor in camel main
// is not triggered after all the routes are collected so i.e.
// for those from the registry, Camel DI does not work.
// This hack should be removed after the issues is fixed in
// Camel, see https://issues.apache.org/jira/browse/CAMEL-14271
//
CamelBeanPostProcessor postProcessor = camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor();
for (RoutesBuilder builder : getRoutesBuilders()) {
postProcessor.postProcessBeforeInitialization(builder, builder.getClass().getName());
postProcessor.postProcessAfterInitialization(builder, builder.getClass().getName());
}

getCamelContext().start();

for (MainListener listener : listeners) {
listener.afterStart(this);
}
}

@Override
protected void loadRouteBuilders(CamelContext camelContext) throws Exception {
// classes are automatically discovered by build processors
}

@Override
protected void doStop() throws Exception {
try {
Expand Down

0 comments on commit bd646e7

Please sign in to comment.