I'm trying to use Declarative Services as outlined here
https://blog.osgi.org/2018/03/osgi-r7-highlights-declarative-services.html 
... and I have the problem when referenced service does not re-appear after
restart of its bundle.

In my scenario i need to dispatch invocation of a method call to number of
implementations.
To do this I create 3 implementation bundles, each of them has component
like this

        public interface MyApi {
                String sayHello(String why);
        }

        @Component
        public class MyApiImpl1 implements MyApi {

                public MyApiImpl1() {
                        sayHello("startup");
                }

                public String sayHello(String why) {
                        System.out.println("Hello from Impl1: " + why);
                        return "impl1";
                }
        }

And i also have dispatcher bundle #4, having following code

        @Component
        public class MyApiDispatcher {
                @Reference(service = MyApi.class)
                List<MyApi> implementations;

                boolean stopped;
                Thread ping = new Thread(() -> {
                        while( !stopped ) {
                                test("ping");
                                try {
                                        Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                        e.printStackTrace();
                                }
                        }
                });

                public MyApiDispatcher() {
                        ping.start();
                }               
                
                @Deactivate
                public void deactivate() {
                        stopped = true;
                }

                public void test(String source) {
                        if( implementations == null ) {
                                System.out.println("implementations = <null>");
                                return;
                        }

                        List<String> calledImpls = new ArrayList<>();

                        for (MyApi impl : implementations) {
                                calledImpls.add(impl.sayHello(source));
                        }

                        System.out.println("calledImpls(" + source + ") = " + 
calledImpls);
                }
        }

Behaviour as I see it now is following:

1. OK: initial startup - all 3 implementations are present in
MyApiDispatcher
2. OK: after i stop bundle with Impl#1 - it disappears from
MyApiDispatcher.implementations
3. BAD: after restart bundle with Impl#1 - it DOES NOT re-appear in
MyApiDispatcher.implementations
4. WORKAROUND: after restart of bundle with dispatcher - it receives all 3
implementations again

Behaviour #3 is very unexpected. 

Is it bug or OSGI spec defines such behaviour? 
How can I make impl re-appear after restart of its bundle?

My current environment is following:

  Karaf version               4.2.3
  OSGi Framework              org.apache.felix.framework-5.6.12









--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html

Reply via email to