为什么不推荐使用Camel SCR?

7bsow1i6  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(300)

我正在查看Camel-Scr,在pom.xml中我看到

<artifactId>camel-scr</artifactId>
 <name>Camel :: SCR (deprecated)</name>
 <description>Camel with OSGi SCR (Declarative Services)</description>

为什么不赞成使用?社区将来会使用什么替代方案?

jckbn6z7

jckbn6z71#

我的猜测是,它的注解和属性太复杂了,因此与简单得多的OSGi蓝图相比,可能没有得到太多的使用。
OsgiDefaultCamelContext的帮助下,将Apache Camel与声明性服务或SCR一起使用非常简单。您可以手动创建上下文,添加路由和配置,并使用bundleContext.registerService方法将其注册到OSGi。

示例:

package com.example;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.core.osgi.OsgiDefaultCamelContext;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(
    immediate = true
)
public class OsgiDSCamelContextComponent {

    private final static Logger LOGGER = LoggerFactory.getLogger(ExampleCamelContext.class);

    CamelContext camelContext;
    ServiceRegistration<CamelContext> camelContextRegistration;

    @Activate
    public void onActivate(BundleContext bundleContext, Map<String, ?> configs){

        // Create new OsgiDefaultCamelContext with injected bundleContext
        OsgiDefaultCamelContext newCamelContext = new OsgiDefaultCamelContext(bundleContext);
        newCamelContext.setName("OsgiDSCamelContext");

        // Add configs from com.example.OsgiDSCamelContextComponent.cfg 
        // available for use with property placeholders 
        Properties properties = new Properties();
        properties.putAll(configs);
        newCamelContext.getPropertiesComponent()
            .setInitialProperties(properties);

        camelContext = newCamelContext;

        try {
            // In Apache Camel 3.x CamelContext needs to be started before adding RouteBuilders. 
            camelContext.start();
            camelContext.addRoutes(new RouteBuilder() {

                @Override
                public void configure() throws Exception {

                    from("timer:exampleTimer?period=3000")
                        .routeId("exampleTimer")
                        .log("Hello from Camel using Declarative services");
                }
            });

            //Create dictionary holding properties for the CamelContext service.
            Dictionary serviceProperties = new Hashtable<>();
            serviceProperties.put("context.name", "OsgiDSCamelContext");
            serviceProperties.put("some.property", "SomeValue");

            // Register the new CamelContext instance as a service to Karaf with given properties
            camelContextRegistration = bundleContext.registerService(CamelContext.class, 
                camelContext, serviceProperties);

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

    @Deactivate
    public void onDeactivate(){

        // Stop camel context when bundle is stopped
        if(camelContext != null){
            camelContext.stop();
        }

        // unregister camel context service when bundle is stopped
        if(camelContextRegistration != null){
            camelContextRegistration.unregister();
        }
    }
}

现在,您还可以使用DS服务组件来注册RouteBuilder服务,并使用@Reference注解和List<RouteBuilder>将它们注入CamelContext。
第一个
使用@Modifiedpolicy = ReferencePolicy.DYNAMIC等高级选项时要格外小心,因为这些选项会在配置更改或列表修改时阻止重新创建上下文。这可能会导致路由添加两次等问题。

OSGI R6的依赖关系

<dependencies>
    <!-- OSGI -->
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>osgi.core</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>osgi.annotation</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>osgi.cmpn</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Camel -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.karaf</groupId>
        <artifactId>camel-core-osgi</artifactId>
        <version>${camel.version}</version>
    </dependency>
</dependencies>

OSGI R8的依赖关系

<dependencies>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>osgi.core</artifactId>
        <version>${osgi.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.service.component.annotations</artifactId>
        <version>1.4.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.service.metatype.annotations</artifactId>
        <version>1.4.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- Camel -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.karaf</groupId>
        <artifactId>camel-core-osgi</artifactId>
        <version>${camel.version}</version>
    </dependency>
</dependencies>
zdwk9cvp

zdwk9cvp2#

没有现成的SCR,我们将只支持OSGi蓝图。
您需要构建自己的scr支持或重用camel-scr代码。

相关问题