ramu11 commented on code in PR #26006: URL: https://github.com/apache/camel/pull/26006#discussion_r3910193502
########## components/camel-hivemq/src/main/java/org/apache/camel/component/hivemq/HiveMQEndpoint.java: ########## @@ -0,0 +1,203 @@ +/* + * 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.camel.component.hivemq; + +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import com.hivemq.client.mqtt.MqttClient; +import com.hivemq.client.mqtt.MqttClientState; +import com.hivemq.client.mqtt.mqtt5.Mqtt5AsyncClient; +import com.hivemq.client.mqtt.mqtt5.Mqtt5ClientBuilder; +import com.hivemq.client.mqtt.mqtt5.message.auth.Mqtt5SimpleAuth; +import com.hivemq.client.mqtt.mqtt5.message.auth.Mqtt5SimpleAuthBuilder; +import org.apache.camel.Category; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.spi.EndpointServiceLocation; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriPath; +import org.apache.camel.support.DefaultEndpoint; + +@UriEndpoint(firstVersion = "4.23.0", scheme = "hivemq", title = "HiveMQ", syntax = "hivemq:topic", + category = { Category.MESSAGING, Category.IOT }, headersClass = HiveMQConstants.class) +public class HiveMQEndpoint extends DefaultEndpoint implements EndpointServiceLocation { + + /** + * The MQTT topic name or pattern to subscribe to or publish on. + */ + @UriPath + @Metadata(required = true) + private String topic; + + /** + * The HiveMQ component configuration options. + */ + @UriParam + @Metadata(description = "To use a custom HiveMQConfiguration") + private HiveMQConfiguration configuration; + + private final Map<Mqtt5AsyncClient, AtomicBoolean> reconnectCancellations = new ConcurrentHashMap<>(); + + public HiveMQEndpoint(String uri, HiveMQComponent component, HiveMQConfiguration configuration, String topic) { + super(uri, component); + this.configuration = configuration; + this.topic = topic; + } + + @Override + public Producer createProducer() throws Exception { + return new HiveMQProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + HiveMQConsumer consumer = new HiveMQConsumer(this, processor); + configureConsumer(consumer); + return consumer; + } + + public Mqtt5AsyncClient createClient() { + AtomicBoolean cancelReconnect = new AtomicBoolean(); + AtomicReference<Mqtt5AsyncClient> clientRef = new AtomicReference<>(); + Mqtt5ClientBuilder builder = MqttClient.builder() + .serverHost(configuration.getHost()) + .serverPort(configuration.getPort()) + .automaticReconnectWithDefaultConfig() + .addDisconnectedListener(context -> { + // Initial connect() does not complete while auto-reconnect keeps retrying (HiveMQ #302). + // Also honour an explicit stop so DISCONNECTED_RECONNECT / CONNECTING_RECONNECT are cancelled. + if (cancelReconnect.get() || context.getClientConfig().getState() == MqttClientState.CONNECTING) { + context.getReconnector().reconnect(false); + } + }) + .addConnectedListener(context -> { + // HiveMQ schedules reconnect after listeners return; cancelReconnect cannot abort that delay. + // If a reconnect succeeds after Camel stop, disconnect immediately (USER source skips auto-reconnect). + if (cancelReconnect.get()) { + Mqtt5AsyncClient started = clientRef.get(); + if (started != null && started.getState().isConnected()) { + try { + started.disconnect(); + } catch (Exception e) { + // Already disconnecting or not connected + } + } + } + }) + .useMqttVersion5(); + + if (configuration.getClientId() != null) { + builder.identifier(configuration.getClientId()); + } + + if (configuration.isSsl()) { + builder.sslWithDefaultConfig(); Review Comment: Yes, this is intentional for the Preview-level first cut. The initial implementation supports TLS using HiveMQ's default SSL configuration and does not currently integrate with Camel's `SSLContextParameters`/JSSE utilities for custom trust/key stores or client certificates. We can consider adding `SSLContextParameters` support as a follow-up enhancement to provide more flexible TLS configuration. -- 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]
