HiveMQ Plugins: Spring
One of the outstanding features of HiveMQ is the ability to customize the broker with custom Java plugin code, which allows to create virtually every integration you can imagine. Java has one of the best ecosystems in terms of available high quality third-party libraries, so it’s seldom needed to build complex integration functionality from scratch on your own.
The HiveMQ plugin system uses Guice for the dependency injection, which allows to write clean and reusable code. Many popular Java libraries rely on Spring in an intrusive way, though, so sometimes it’s needed to integrate Spring with Guice for your plugins.
There are a few things you have to take care of to use Spring dependency injection in HiveMQ Plugins. As HiveMQ uses Guice for depencendy injection, it is necessary to unterstand when you want Guice to inject something and when you want Spring to handle the injection.
Setting up the Spring ApplicationContext
Since HiveMQ Plugins have their own isolated ClassLoader, you have to setup Springs AplicationContext with a ClassPathXmlApplicationContext. Otherwise Spring won't be able to load your xml file.
Example:
You also need to setup the ApplicationContext within the configurePlugin()
method of your HiveMQPluginModule
. This is necessary to make your instances managed by Spring available to Guice. This means you can use Guice to inject Objects created by Spring.
Example:
for this to work you also need an additional depency in your pom.xml
If you want to use Guice to inject instances managed by Spring you need to use the @Named
Annotation, because every instance managed by Spring is registered with Guice as a named instance.
Example:
Injecting HiveMQ Plugin Services into Spring instances
If you want to use any of HiveMQ's Services (for example the PublishService
), you need to let Guice inject them into your instances.
For this to work with an instance managed by Spring, you cannot use @javax.inject.Inject
anywhere in the class (Because Guice and Spring both process this annotation). Instead you should use @Autowired
for instances you want to be injected by Spring and @com.google.inject.Inject
for the instances you want to be handled by Guice.
Example:
The next step is to tell Guice to take care of this instance created by Spring. You can do this easily by adding a line to the configurePlugin()
method of your HiveMQPluginModule
.
Example:
Register Callbacks managed by Spring
To register your callback implementations with HiveMQ's CallbackRegistry
you need to inject them into your PluginEntryPoint
by using Guice (even if they are managed by Spring), because the PluginEntryPoint must always be instantiated by Guice.
Remember: Spring managed instances need to be injected with @Named
for Guice to recognize them.
Example:
Example Callback managed by Spring:
Example Plugin
For a full code example see Github
HiveMQ Team
The HiveMQ team loves writing about MQTT, Sparkplug, Industrial IoT, protocols, how to deploy our platform, and more. We focus on industries ranging from energy, to transportation and logistics, to automotive manufacturing. Our experts are here to help, contact us with any questions.