'Why Camel SCR deprecated?

I was looking into Camel-Scr and in pom.xml i saw

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

Why this is deprecated? what alternative would community use in future?



Solution 1:[1]

My guess is that it was simply too complex with all the annotations and properties and hence probably didn't get much use compared to much simpler OSGi blueprints.

Usage of Apache Camel with Declarative services or SCR is pretty straightforward with the help of OsgiDefaultCamelContext. You can create the context manually, add routes and configurations and register it to OSGi with bundleContext.registerService method.

Example:

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();
        }
    }
}

Dependencies

<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>

Now you could also use DS Service Components to register RouteBuilder service(s) and inject them to CamelContext using @Reference annotation and List<RouteBuilder>.

package com.example.routes;

import org.apache.camel.builder.RouteBuilder;
import org.osgi.service.component.annotations.Component;

@Component(
    immediate = true,
    property = {
        "target.context=exampleContext"
    },
    service = RouteBuilder.class
)
public class ExampleRouteBuilderService extends RouteBuilder {

    @Override
    public void configure() throws Exception {
     
        from("timer:exampleTimer?period=3000")
            .routeId("exampleTimer")
            .log("Hello from Camel using Declarative services");
    }
}
@Reference(
    target = "(target.context=exampleContext)",
    cardinality = ReferenceCardinality.AT_LEAST_ONE,
    policyOption = ReferencePolicyOption.GREEDY
)
List<RouteBuilder> routeBuilders;

Just be extra careful when using more advanced options like @Modified or policy = ReferencePolicy.DYNAMIC as these can prevent context from getting recreated when config changes or list gets modified. This can lead to issues like routes getting added twice.

Solution 2:[2]

There is no SCR out of the box, we'll support only OSGi blueprint.

You'll need to build your own scr support or re-use the camel-scr code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Oscerd