Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MQTT client crashes when subscriber throws exception #3719

Merged
merged 1 commit into from
Jul 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.io.transport.mqtt.MqttMessageSubscriber;
import org.openhab.core.util.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hivemq.client.mqtt.mqtt3.message.publish.Mqtt3Publish;
import com.hivemq.client.mqtt.mqtt5.message.publish.Mqtt5Publish;
Expand All @@ -31,6 +34,7 @@
*/
@NonNullByDefault
public class Subscription {
private final Logger logger = LoggerFactory.getLogger(Subscription.class);
private final Map<String, byte[]> retainedMessages = new ConcurrentHashMap<>();
private final Collection<MqttMessageSubscriber> subscribers = ConcurrentHashMap.newKeySet();

Expand Down Expand Up @@ -81,10 +85,15 @@ public void messageArrived(String topic, byte[] payload, boolean retain) {
if (retain || retainedMessages.containsKey(topic)) {
retainedMessages.put(topic, payload);
}
subscribers.stream().forEach(subscriber -> processMessage(subscriber, topic, payload));
subscribers.forEach(subscriber -> processMessage(subscriber, topic, payload));
}

private void processMessage(MqttMessageSubscriber subscriber, String topic, byte[] payload) {
subscriber.processMessage(topic, payload);
try {
subscriber.processMessage(topic, payload);
} catch (RuntimeException e) {
logger.warn("A subscriber of type '{}' failed to process message '{}' to topic '{}'.",
subscriber.getClass(), HexUtils.bytesToHex(payload), topic);
}
}
}