531651225 commented on code in PR #3312: URL: https://github.com/apache/incubator-seatunnel/pull/3312#discussion_r1029344205
########## seatunnel-connectors-v2/connector-rabbitmq/src/main/java/org/apache/seatunnel/connectors/seatunnel/rabbitmq/client/RabbitmqClient.java: ########## @@ -0,0 +1,187 @@ +/* + * 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.rabbitmq.client; + +import static org.apache.seatunnel.connectors.seatunnel.rabbitmq.exception.RabbitmqConnectorErrorCode.CLOSE_CONNECTION_FAILED; +import static org.apache.seatunnel.connectors.seatunnel.rabbitmq.exception.RabbitmqConnectorErrorCode.CREATE_RABBITMQ_CLIENT_FAILED; +import static org.apache.seatunnel.connectors.seatunnel.rabbitmq.exception.RabbitmqConnectorErrorCode.SEND_MESSAGE_FAILED; + +import org.apache.seatunnel.common.Handover; +import org.apache.seatunnel.connectors.seatunnel.rabbitmq.config.RabbitmqConfig; +import org.apache.seatunnel.connectors.seatunnel.rabbitmq.exception.RabbitmqConnectorException; + +import com.rabbitmq.client.Channel; +import com.rabbitmq.client.Connection; +import com.rabbitmq.client.ConnectionFactory; +import com.rabbitmq.client.DefaultConsumer; +import com.rabbitmq.client.Delivery; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.concurrent.TimeoutException; + +@Slf4j +@AllArgsConstructor +public class RabbitmqClient { + private RabbitmqConfig config; + private ConnectionFactory connectionFactory; + private Connection connection; + private Channel channel; + + public RabbitmqClient(RabbitmqConfig config) { + this.config = config; + try { + this.connectionFactory = getConnectionFactory(); + this.connection = connectionFactory.newConnection(); + this.channel = connection.createChannel(); + //set channel prefetch count + if (config.getPrefetchCount() != null) { + channel.basicQos(config.getPrefetchCount(), true); + } + setupQueue(); + } catch (Exception e) { + throw new RabbitmqConnectorException(CREATE_RABBITMQ_CLIENT_FAILED, String.format("Error while create RMQ client with %s at %s", config.getQueueName(), config.getHost()), e); + } + + } + + public Channel getChannel() { + return channel; + } + + public DefaultConsumer getQueueingConsumer(Handover<Delivery> handover) { + DefaultConsumer consumer = new QueueingConsumer(channel, handover); + return consumer; + } + + public ConnectionFactory getConnectionFactory() + throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException { + ConnectionFactory factory = new ConnectionFactory(); + if (!StringUtils.isEmpty(config.getUri())) { + try { + factory.setUri(config.getUri()); + } catch (URISyntaxException e) { + log.error("Failed to parse uri", e); + throw e; + } catch (KeyManagementException e) { + // this should never happen + log.error("Failed to initialize ssl context.", e); + throw e; + } catch (NoSuchAlgorithmException e) { + // this should never happen + log.error("Failed to setup ssl factory.", e); + throw e; Review Comment: > the same as above thinks, fix above, PTAL. -- 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]
