I'm busy coming to grips with Camel and Karaf. I've built a project with two bundles:
I followed the instructions from Jamie Goodyear's Karaf Cookbook
Both routes are super simple and I deploy them using a feature file. They deploy perfectly and also run exactly as planned:
Bundle A moves files from /tmp/in
to /tmp/out
Bundle B moves files from /tmp/in2
to tmp/out2
All good.
However, if I run the Karaf command camel:route-list
then only the Blueprint route is shown
Also, if I run camel:context-list
then only the context defined in Bundle A is shown.
Just to reiterate, both routes work correctly, it's just the the Java ones aren't showing up in the list.
Am I missing something here?
Here's my Java Route:
public class FileRouter extends RouteBuilder {
public void configure()
{
from ("file:/tmp/in2?noop=true")
.log("Java DSL doing the heavy lifting")
.to("file:/tmp/out2");
}
}
And the Bundle Activator:
public class Activator implements BundleActivator {
DefaultCamelContext camelContext;
public void start(BundleContext context) {
System.out.println("Starting the bundle");
camelContext = new DefaultCamelContext();
try {
camelContext.setName("JavaDSLContext");
camelContext.addRoutes(new FileRouter());
camelContext.start();
} catch (Exception ex) {
System.out.println("Exception occured! " + ex.getMessage());
}
}
public void stop(BundleContext context) {
System.out.println("Stopping the bundle");
if (camelContext != null) {
try {
camelContext.stop();
} catch (Exception ex) {
System.out.println("Exception occured during stop context.");
}
}
}
}
And thanks to Claus - I've worked through the material on Camel-SCR and eventually got that approach to work as well. My thinking is that Camel-SCR is probably the cleaner solution, because it makes it very easy to pass properties to the JavaDSL router.
Here's my final solution, for completeness' sake, then I'll close up this question: The file router now looks like this:
public class ScrFileRouter extends RouteBuilder {
// Configured fields
private String camelRouteId;
@Override
public void configure() throws Exception {
// Add a bean to Camel context registry
AbstractCamelRunner.getRegistry(getContext(), SimpleRegistry.class).put("testString", "this is a test");
from("file:/tmp/in6?noop=true").routeId(camelRouteId)
.to("file:/tmp/out6");
}
}
And the SCR-based Camel runner looks like this:
@Component(label = ScrRunner.COMPONENT_LABEL, description = ScrRunner.COMPONENT_DESCRIPTION, immediate = true, metatype = true)
@Properties({
@Property(name = "camelContextId", value = "scr-runner"),
@Property(name = "camelRouteId", value = "scr-file-router"),
@Property(name = "active", value = "true"),
})
@References({
@Reference(name = "camelComponent",referenceInterface = ComponentResolver.class,
cardinality = ReferenceCardinality.MANDATORY_MULTIPLE, policy = ReferencePolicy.DYNAMIC,
policyOption = ReferencePolicyOption.GREEDY, bind = "gotCamelComponent", unbind = "lostCamelComponent")
})
public class ScrRunner extends AbstractCamelRunner {
public static final String COMPONENT_LABEL = "ScrRunner";
public static final String COMPONENT_DESCRIPTION = "This is the description for ScrRunner";
@Override
protected List<RoutesBuilder> getRouteBuilders() {
List<RoutesBuilder> routesBuilders = new ArrayList<>();
routesBuilders.add(new ScrFileRouter());
return routesBuilders;
}
@Override
protected void setupCamelContext(BundleContext bundleContext, String camelContextId)throws Exception{
super.setupCamelContext(bundleContext, camelContextId);
// Use MDC logging
getContext().setUseMDCLogging(true);
// Use breadcrumb logging
getContext().setUseBreadcrumb(true);
}
}
I followed the information on the Camel SCR website closely and almost got things working. Then I used the archetype proposed (camel-archetype-scr), which worked nicely.
So in the end I also had to make some changes to my POM file (Effectively just using the POM provided by the Archetype.)
Thanks to all for the assistance. I think I'll be able to get some traction now.
Cheers!