This tutorial assumes you have the same environment set up in the quickstart guide, and offers a way to write a similar application, but this time using the MAMA Resource Pool.
The MAMA Resource pool is a way to manage resources which would otherwise need to be created manually along with their dependencies. It provides optimization out of the box and encourages configuration rather than code to configure behaviour for things like sources and transports.
It also manages some of the more tricky aspects of managing an OpenMAMA application such as setup, shutdown and cleanup in a thread safe manner.
The first thing that we should do when using a mama resource pool is to create an instance. This can be done by simply creating an instance by name:
This will create a new MAMA resource pool with the name “default” and will inherit all the default
properties for the MAMA resource pool with that name from configuration
mama.resource_pool.<pool name>.<property>, e.g.:
# The number of threads / queues to use in the pool's queue group per bridge mama.resource_pool.default.queues=4 # List of bridges to load mama.resource_pool.default.bridges=qpid, nng # List of options to use when creating a subscription in this pool mama.resource_pool.default.options.retries = 5 mama.resource_pool.default.options.timeout = 1
After this is done, you can set up your dictionary the same as in the quickstart guide.
Then you can set callback handlers as normal
before creating a subscription. Note that
when using the resource pool, only the
onMsg callback is mandatory:
Next you can create your subscription using any of the mamaResourcePool calls assuming that:
All you need to do is call one of the
And that creates the subscription… but it is not yet activated. This is deliberate so that you can apply further mamaSubscription API calls if you want before activating it:
In our tutorial example. this then calls MAMA’s
startAll function to begin dispatching events for this subscription, and will then unblock when MAMA is interrupted.
Note it is also possible to destroy an individual subscription during normal operation as well using MAMA Resource Pool’s
After the application is ready to shut down, cleanup is as simple as:
Which will clean up not only the MAMA Resource Pool but also all underlying transports, queues and subscriptions in a thread safe manner so you can safely call MAMA’s
Note that if using the pool, any transports, sources and subscriptions using MAMA Resource Pool should be exclusively destroyed by MAMA Resource Pool. Attempting to also destroy in your own application can lead to a double destroy and undefined behaviour.
For complete functioning example code, please see: