BundleActivator

struct cppmicroservices::BundleActivator

Customizes the starting and stopping of a CppMicroServices bundle.

BundleActivator is an interface that can be implemented by CppMicroServices bundles. The CppMicroServices library can create instances of a bundle’s BundleActivator as required. If an instance’s BundleActivator::Start method executes successfully, it is guaranteed that the same instance’s BundleActivator::Stop method will be called when the bundle is to be stopped. The CppMicroServices library does not concurrently call a BundleActivator object.

BundleActivator is an abstract class interface whose implementations must be exported via a special macro. Implementations are usually declared and defined directly in .cpp files.

class MyActivator : public BundleActivator
{

public:
  void Start(BundleContext /*context*/)
  { /* register stuff */
  }

  void Stop(BundleContext /*context*/)
  { /* cleanup */
  }
};

CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR(MyActivator)

The class implementing the BundleActivator interface must have a public default constructor so that a BundleActivator object can be created by the CppMicroServices library.

Note

A bundle activator needs to be exported by using the CPPMICROSERVICES_EXPORT_BUNDLE_ACTIVATOR macro. The bundle manifest.json resource also needs to contain a

"bundle.activator" : true

element.

Public Functions

virtual ~BundleActivator()
virtual void Start(BundleContext context) = 0

Called when this bundle is started.

This method can be used to register services or to allocate any resources that this bundle may need globally (during the whole bundle lifetime).

This method must complete and return to its caller in a timely manner.

Parameters
  • context: The execution context of the bundle being started.
Exceptions
  • std::exception: If this method throws an exception, this bundle is marked as stopped and the framework will remove this bundle’s listeners, unregister all services registered by this bundle, and release all services used by this bundle.

virtual void Stop(BundleContext context) = 0

Called when this bundle is stopped.

In general, this method should undo the work that the BundleActivator::Start method started. There should be no active threads that were started by this bundle when this method returns.

This method must complete and return to its caller in a timely manner.

Parameters
  • context: The execution context of the bundle being stopped.
Exceptions
  • std::exception: If this method throws an exception, the bundle is still marked as stopped, and the framework will remove the bundle’s listeners, unregister all services registered by the bundle, and release all services used by the bundle.