Fork me on GitHub
Edit on GitHub << back to documentation

Intro

These examples shows howto create and interact with services by example.

In both examples there is a provider and consumer bundle. The provider bundle creates services and registers them to the Celix framework so that these services are available for use (provide). And the consumer bundle request the services from the Celix framework and uses (consumes) them.

The examples use the celix_bundleContext_registerService to provide services and uses a combination of celix_bundleContext_useService, celix_bundleContext_useServices and celix_bundleContext_trackServices to consume services.

The bundle context is the proxy to the Celix framework for a bundle and can be used to:

  • Register services
  • Register service factories
  • Use services
  • Track services
  • Track bundles
  • Track service trackers

See the bundle_context.h for documentation about these - and other -functions

Simple Service Provider & Consumer Example

The simple provider/consumer example can be executed by launching the services_example_c executable target (build in ${CMAKE_BUILD_DIR}/deploy/services_example/services_example_c)

In this example the provider bundle only register one calc service. And the consumer bundle tries to use this during startup and registered a service tracker for the calc service.

+----------------------+                  +------------------------+
|                      |                  |                        |
|                      |                  |                        |
|   provider_example   |   example_calc   |    consumer_example    |
|                      +--------O)--------+                        |
|       <bundle>       |                  |       <bundle>         |
|                      |                  |                        |
|                      |                  |                        |
+----------------------+                  +------------------------+

Try stopping/starting the provider/consumer bundles (respectively bundle id 3 & 4) using the interactive shell to see how this works at runtime. I.e. type stop 3, stop 4, start 3, start 4 in different combinations.

Dynamic Service Provider & Consumer Example

The dynamic provider/consumer example can be executed by launching the dynamic_services_example_c executable target (build in ${CMAKE_BUILD_DIR}/deploy/services_example/dynamic_services_example_c)

The dynamic service provide / consumer example show how the framework copes with the dynamic behaviour of services.

This this example the provided dynamically register more and less calc services in a thread. The consumer bundle uses these services in a every 5 seconds and print some info.

This example should give an idea how services can be safely used and tracked in a dynamic environment.