Skip to content

Mqttv5PahoMessageDrivenChannelAdapter.addTopic(topic, qos) throws NPE #3732

@mrpiggi

Description

@mrpiggi

In what version(s) of Spring Integration are you seeing this issue?

5.5.9 and prior (5.5.5)

Describe the bug

Creating a new Mqttv5PahoMessageDrivenChannelAdapter and using addTopic(topic, qos) before onInit() was called throws a NPE as this.mqttClient.subscribe(topic, qos) is invoked. Same with Mqttv5PahoMessageDrivenChannelAdapter.removeTopic(String...) and this.mqttClient.unsubscribe(topic).

To Reproduce

final Mqttv5PahoMessageDrivenChannelAdapter adapter = new Mqttv5PahoMessageDrivenChannelAdapter(options, id);
adapter.addTopic(topic, 1);

Workaround

final Mqttv5PahoMessageDrivenChannelAdapter adapter = new Mqttv5PahoMessageDrivenChannelAdapter(options, id, topic);
adapter.setQos(1);

Expected behavior

Topic registration or removal without calling IMqttAsyncClient.subscribe(topic, qos) or IMqttAsyncClient.unsubscribe(String...) when not initialized yet.

The comparison between Mqttv5PahoMessageDrivenChannelAdapter and MqttPahoMessageDrivenChannelAdapter is quite obvious.

@Override
public void addTopic(String topic, int qos) {
this.topicLock.lock();
try {
this.mqttClient.subscribe(topic, qos).waitForCompletion(getCompletionTimeout());
super.addTopic(topic, qos);
}
catch (MqttException ex) {
throw new MessagingException("Failed to subscribe to topic " + topic, ex);
}
finally {
this.topicLock.unlock();
}
}
@Override
public void removeTopic(String... topic) {
this.topicLock.lock();
try {
this.mqttClient.unsubscribe(topic).waitForCompletion(getCompletionTimeout());
super.removeTopic(topic);
}
catch (MqttException ex) {
throw new MessagingException("Failed to unsubscribe from topic(s) " + Arrays.toString(topic), ex);
}
finally {
this.topicLock.unlock();
}
}

@Override
public void addTopic(String topic, int qos) {
this.topicLock.lock();
try {
super.addTopic(topic, qos);
if (this.client != null && this.client.isConnected()) {
this.client.subscribe(topic, qos);
}
}
catch (MqttException e) {
super.removeTopic(topic);
throw new MessagingException("Failed to subscribe to topic " + topic, e);
}
finally {
this.topicLock.unlock();
}
}
@Override
public void removeTopic(String... topic) {
this.topicLock.lock();
try {
if (this.client != null && this.client.isConnected()) {
this.client.unsubscribe(topic);
}
super.removeTopic(topic);
}
catch (MqttException e) {
throw new MessagingException("Failed to unsubscribe from topic(s) " + Arrays.toString(topic), e);
}
finally {
this.topicLock.unlock();
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions