无法将Camel属性组件添加到部署到Karaf的Scr AbstractCamelRunner上下文

时间:2016-12-07 03:31:29

标签: java apache-camel osgi apache-karaf

我试图通过向Camel上下文添加新的属性文件来使用Camel属性组件。在我的测试代码中(不使用SCR,如果我添加这样的组件,它的工作完全正常:

@Override
protected CamelContext createCamelContext() throws Exception {
    CamelContext context = super.createCamelContext();

    ... Some other components, not relevant to this issue

    // Add properties component to the Camel Context
    PropertiesComponent pc = new PropertiesComponent();
    pc.setLocation("classpath:freemarker.properties");
    context.addComponent("properties", pc);

    return context;
}

但是,当我在SCR CamelRunner类中使用相同的方法时,在尝试在Karaf中部署路由时会出现异常。

以下是我的SCR定义:

@Component(label = FreemarkerRunner.COMPONENT_LABEL, description = FreemarkerRunner.COMPONENT_DESCRIPTION, immediate = true, metatype = true)
@Properties({
    @Property(name = "camelContextId", value = "freemarker-context"),
    @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 FreemarkerRunner extends AbstractCamelRunner {

    public static final String COMPONENT_LABEL = "com.mycompany.freemarker.scr.FreemarkerRunner";
    public static final String COMPONENT_DESCRIPTION = "This is the description for the freemarker SCR runner.";

    @Override
    protected List<RoutesBuilder> getRouteBuilders() {
        List<RoutesBuilder> routesBuilders = new ArrayList<>();
        routesBuilders.add(new FreemarkerRouteBuilder());
        return routesBuilders;
    }

    @Override
    protected void setupCamelContext(BundleContext bundleContext, String camelContextId) throws Exception{
        super.setupCamelContext(bundleContext, camelContextId);

        ... Some other component configs ...

        // Add properties component to the Camel Context
        PropertiesComponent pc = new PropertiesComponent();
        pc.setLocation("classpath:freemarker.properties");
        getContext().addComponent("properties", pc);
    }
}

将捆绑包部署到Karaf时,未部署Camel路由,并在Karaf日志中抛出以下异常:

2016-12-06 18:47:29,468 | ERROR | pool-14-thread-1 | renderer-freemarker              | 135 - com.mycompany.renderer-freemarker - 0.0.1.SNAPSHOT | [com.mycompany.freemarker.scr.FreemarkerRunner(5)] The activate method has thrown an exception
java.lang.IllegalArgumentException: Cannot add component as its already previously added: properties
    at org.apache.camel.impl.DefaultCamelContext.addComponent(DefaultCamelContext.java:369)
    at com.mycompany.freemarker.scr.FreemarkerRunner.setupCamelContext(FreemarkerRunner.java:81)[135:com.mycompany.renderer-freemarker:0.0.1.SNAPSHOT]
    at org.apache.camel.scr.AbstractCamelRunner.prepare(AbstractCamelRunner.java:90)[71:org.apache.camel.camel-scr:2.16.0]
    at org.apache.camel.scr.AbstractCamelRunner.activate(AbstractCamelRunner.java:79)[71:org.apache.camel.camel-scr:2.16.0]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_111]
    at org.apache.felix.scr.impl.inject.BaseMethod.invokeMethod(BaseMethod.java:224)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.inject.BaseMethod.access$500(BaseMethod.java:39)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.inject.BaseMethod$Resolved.invoke(BaseMethod.java:617)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.inject.BaseMethod.invoke(BaseMethod.java:501)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.inject.ActivateMethod.invoke(ActivateMethod.java:302)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.inject.ActivateMethod.invoke(ActivateMethod.java:294)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.manager.SingleComponentManager.createImplementationObject(SingleComponentManager.java:297)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.manager.SingleComponentManager.createComponent(SingleComponentManager.java:108)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.manager.SingleComponentManager.getService(SingleComponentManager.java:906)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.manager.SingleComponentManager.getServiceInternal(SingleComponentManager.java:879)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.manager.AbstractComponentManager.activateInternal(AbstractComponentManager.java:748)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.manager.AbstractComponentManager.enableInternal(AbstractComponentManager.java:674)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.manager.AbstractComponentManager.enable(AbstractComponentManager.java:429)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.manager.ConfigurableComponentHolder.enableComponents(ConfigurableComponentHolder.java:657)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.BundleComponentActivator.initialEnable(BundleComponentActivator.java:341)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.Activator.loadComponents(Activator.java:403)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.Activator.access$200(Activator.java:54)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.scr.impl.Activator$ScrExtension.start(Activator.java:278)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.utils.extender.AbstractExtender.createExtension(AbstractExtender.java:259)[78:org.apache.felix.scr:2.0.6]
    at org.apache.felix.utils.extender.AbstractExtender.modifiedBundle(AbstractExtender.java:232)[78:org.apache.felix.scr:2.0.6]
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:482)[org.osgi.core-6.0.0.jar:]
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:415)[org.osgi.core-6.0.0.jar:]
    at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:232)[org.osgi.core-6.0.0.jar:]
    at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:444)[org.osgi.core-6.0.0.jar:]
    at org.apache.felix.framework.util.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:916)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:835)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:517)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4541)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.felix.framework.Felix.startBundle(Felix.java:2172)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:998)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:984)[org.apache.felix.framework-5.4.0.jar:]
    at org.apache.karaf.features.internal.service.FeaturesServiceImpl.startBundle(FeaturesServiceImpl.java:1286)[8:org.apache.karaf.features.core:4.0.7]
    at org.apache.karaf.features.internal.service.Deployer.deploy(Deployer.java:846)[8:org.apache.karaf.features.core:4.0.7]
    at org.apache.karaf.features.internal.service.FeaturesServiceImpl.doProvision(FeaturesServiceImpl.java:1176)[8:org.apache.karaf.features.core:4.0.7]
    at org.apache.karaf.features.internal.service.FeaturesServiceImpl$1.call(FeaturesServiceImpl.java:1074)[8:org.apache.karaf.features.core:4.0.7]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[:1.8.0_111]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)[:1.8.0_111]
    at java.lang.Thread.run(Thread.java:745)[:1.8.0_111]

因此,似乎SCR组件已经将属性组件添加到Camel上下文中。问题是:我如何添加我的属性文件?

不确定我是否遗漏了一些明显的东西。

任何指针都将受到赞赏。

0 个答案:

没有答案