A implementation for dispatching message with topic-filter for a MQTT subscriber.
Examples for registering handlers with topic-filters.
// Instantiate TopicDispatcher. <Type> is message type.
TopicDispatcher<MqttMessage> topicDispatcher = new TopicDispatcher<>();
topicDispatcher.addHandler("example/topic1", (topic, message) -> {
// callback when received a MQTT message which topic is 'example/topic1'
// ...
});
topicDispatcher.addHandler("example/test/#", (topic, message) -> {
// callback when received a MQTT message which topic starts with 'example/test/'
// ...
});
topicDispatcher.addHandler("example/+/name", (topic, message) -> {
// callback when received a MQTT message which topic starts with 'example/' and ends with '/name'
// ...
});
A example for message dispatching using paho-mqtt library.
// ...
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
topicDispatcher.dispatch(topic, message); // Note: callback on same thread
}
// ...
- Fork it ( http://github.com/dash14/mqtt-topic-dispatcher/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
MIT License