davidzollo commented on code in PR #10575: URL: https://github.com/apache/seatunnel/pull/10575#discussion_r2924538541
########## seatunnel-connectors-v2/connector-mqtt/src/main/java/org/apache/seatunnel/connectors/seatunnel/mqtt/sink/MqttSinkWriter.java: ########## @@ -0,0 +1,245 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.seatunnel.connectors.seatunnel.mqtt.sink; + +import org.apache.seatunnel.api.configuration.ReadonlyConfig; +import org.apache.seatunnel.api.serialization.SerializationSchema; +import org.apache.seatunnel.api.sink.SinkWriter; +import org.apache.seatunnel.api.table.type.SeaTunnelRow; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; +import org.apache.seatunnel.connectors.seatunnel.mqtt.exception.MqttConnectorErrorCode; +import org.apache.seatunnel.connectors.seatunnel.mqtt.exception.MqttConnectorException; +import org.apache.seatunnel.format.json.JsonSerializationSchema; +import org.apache.seatunnel.format.text.TextSerializationSchema; + +import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; +import org.eclipse.paho.client.mqttv3.MqttCallback; +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; +import org.eclipse.paho.client.mqttv3.MqttException; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; + +import lombok.extern.slf4j.Slf4j; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * MQTT sink writer that publishes each {@link SeaTunnelRow} as an MQTT message. Uses Eclipse Paho + * with in-memory persistence to avoid container disk I/O. Each parallel subtask gets a unique + * client ID to prevent connection conflicts. + */ +@Slf4j +public class MqttSinkWriter implements SinkWriter<SeaTunnelRow, Void, Void>, MqttCallback { + + private static final String CLIENT_ID_PREFIX = "seatunnel_mqtt_sink_task_"; + private static final long RETRY_BACKOFF_MS = 200L; + + private final String topic; + private final int qos; + private final int retryTimeoutMs; + private final int batchSize; + private final SerializationSchema serializationSchema; + private final List<MqttMessage> messageBuffer; + private MqttClient mqttClient; + + public MqttSinkWriter( + SinkWriter.Context context, SeaTunnelRowType rowType, ReadonlyConfig pluginConfig) { + this.topic = pluginConfig.get(MqttSinkOptions.TOPIC); + this.qos = pluginConfig.get(MqttSinkOptions.QOS); + if (this.qos < 0 || this.qos > 1) { + throw new IllegalArgumentException( + "MQTT QoS must be 0 (at-most-once) or 1 (at-least-once), got: " + this.qos); + } + this.retryTimeoutMs = pluginConfig.get(MqttSinkOptions.RETRY_TIMEOUT); + this.batchSize = pluginConfig.get(MqttSinkOptions.BATCH_SIZE); + if (this.batchSize < 1) { + throw new IllegalArgumentException("batch_size must be >= 1, got: " + this.batchSize); + } + this.messageBuffer = new ArrayList<>(this.batchSize); + this.serializationSchema = createSerializationSchema(rowType, pluginConfig); + + // Each subtask appends its index to guarantee a unique MQTT client ID, + // preventing connection hijacking when running parallel tasks. + String clientId = CLIENT_ID_PREFIX + context.getIndexOfSubtask(); + + try { + // MemoryPersistence avoids file-system I/O; ideal for containerized deployments. + this.mqttClient = + new MqttClient( + pluginConfig.get(MqttSinkOptions.URL), + clientId, + new MemoryPersistence()); + this.mqttClient.setCallback(this); + + MqttConnectOptions options = buildConnectOptions(pluginConfig); + this.mqttClient.connect(options); + log.info( + "MQTT sink writer [{}] connected to {}", + clientId, + pluginConfig.get(MqttSinkOptions.URL)); + } catch (MqttException e) { + throw new MqttConnectorException( + MqttConnectorErrorCode.CONNECTION_FAILED, + "Failed to connect MQTT client [" + clientId + "]", + e); Review Comment: It is recommended to add if (this.mqttClient != null) { try { this.mqttClient.close(); } catch (MqttException ignored)... in the catch block. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
