0

What I am trying to achieve

Using netbeans modular system to prepare the application for us, as we implement the modules in OSGi using declarative-services via apache felix. Here is the module setup:

  • Module A - Provides service A and B
  • Module B - Provides service C, Reference service A and B
  • Module C - Provides service D, handles the installation of module A and B
  • Module D - Reference service D and B.

Module C installs modules through the following code:

BundleContext context = FrameworkUtil.getBundle(class).getBundleContext();
Bundle tcp = context.installBundle("file:Location");
tcp.start();

Where everything installs and starts without error. However the declared references are not being bound to the services.

For more specific on how everything holds together I added the code to github https://github.com/jonaslagoni/server. Remember to change the lines in file: module C, dk.sdu.ace.dp.Controller, line 27-28 to the location of module B and A.

  • Module A is "TCP server OSGi Bundle"
  • Module B is "Game world OSGi Bundle"
  • Module C is "Dependency Controller OSGi Bundle"
  • Module D is "Server Engine OSGi Bundle".

What I have tried so far

  1. Based on the active issue I tried ensuring the build order of the installation so module A gets installed before module B and even the other way around just to be sure. I even tried installing the bundle after the second bundle reached the state "started".
  2. Calling update on the module B after module A is started.
  3. Looked up if I needed to do anything regarding registering and provide the different services manually when installing bundles, I could not find anything, or simply did not understood the answers..
  4. I have tried so many silly things which probably does not make sense to try or describe here.

Extra questions

Should the declarative-services automatically bind the providers with the references when installed and started through the bundlecontext?

Is this way of structuring the application even viable, or should we pursue another way? if so which?

jonaslagoni
  • 663
  • 7
  • 23
  • 1
    OSGi has no start ordering and Felix SCR is very good in this. The beauty is that all dependencies are dynamically bound. A system where order is important is a bad OSGi system. You can use Apache Felix gogo and use the scr:list ad scr:info command to see why you're services are not satisfied. – Peter Kriens Mar 14 '19 at 19:13

1 Answers1

1

Your DS references are using an optional cardinality while not using greedy policy option. (reluctant policy option is the default for historical backwards compatibility reasons.)

With reluctant, the reference will not be rebound when additional/better services are later registered such as the bundle providing the service starting after the bundle consuming the service. With greedy, the reference will be rebound.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27
  • I cant find exactly how to do it, but is it just by adding policy="dynamic" policy-option="greedy" to each reference usage? – jonaslagoni Mar 14 '19 at 20:38
  • policy-option="greedy" is what you need. If you want to use policy="dynamic", then your code needs to be prepared for injection at any time not just during component activation. – BJ Hargrave Mar 17 '19 at 13:03
  • I think you are right, however my project is not ready to handle the new version where policy-option is added, I use v.1.1.0 where i need to use v1.4.0 in the xmlns:scr. So I cannot use it right off the bat without updating the project dependencies. However I am having trouble doing just that... – jonaslagoni Mar 17 '19 at 21:22
  • policy-option="greedy" was added in DS v1.2.0. – BJ Hargrave Mar 19 '19 at 12:04
  • Thanks BJ, this was part of the problem, but we had to convert the whole project to pure OSGi, since we were not able to update the dependencies to use a newer OSGi together with equinox. After switching to apache Felix gogo it worked smooth, after fixing some more errors. – jonaslagoni Mar 20 '19 at 10:09