|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.activemq.client; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.configuration.ReadonlyConfig; |
| 21 | +import org.apache.seatunnel.connectors.seatunnel.activemq.exception.ActivemqConnectorErrorCode; |
| 22 | +import org.apache.seatunnel.connectors.seatunnel.activemq.exception.ActivemqConnectorException; |
| 23 | + |
| 24 | +import org.apache.activemq.ActiveMQConnectionFactory; |
| 25 | + |
| 26 | +import lombok.AllArgsConstructor; |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | + |
| 29 | +import javax.jms.Connection; |
| 30 | +import javax.jms.Destination; |
| 31 | +import javax.jms.JMSException; |
| 32 | +import javax.jms.MessageProducer; |
| 33 | +import javax.jms.Session; |
| 34 | +import javax.jms.TextMessage; |
| 35 | + |
| 36 | +import java.nio.charset.StandardCharsets; |
| 37 | + |
| 38 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.ALWAYS_SESSION_ASYNC; |
| 39 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.ALWAYS_SYNC_SEND; |
| 40 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.CHECK_FOR_DUPLICATE; |
| 41 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.CLIENT_ID; |
| 42 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.CLOSE_TIMEOUT; |
| 43 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.CONSUMER_EXPIRY_CHECK_ENABLED; |
| 44 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.DISPATCH_ASYNC; |
| 45 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.NESTED_MAP_AND_LIST_ENABLED; |
| 46 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.PASSWORD; |
| 47 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.QUEUE_NAME; |
| 48 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.URI; |
| 49 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.USERNAME; |
| 50 | +import static org.apache.seatunnel.connectors.seatunnel.activemq.config.ActivemqConfig.WARN_ABOUT_UNSTARTED_CONNECTION_TIMEOUT; |
| 51 | + |
| 52 | +@Slf4j |
| 53 | +@AllArgsConstructor |
| 54 | +public class ActivemqClient { |
| 55 | + private final ReadonlyConfig config; |
| 56 | + private final ActiveMQConnectionFactory connectionFactory; |
| 57 | + private final Connection connection; |
| 58 | + |
| 59 | + public ActivemqClient(ReadonlyConfig config) { |
| 60 | + this.config = config; |
| 61 | + try { |
| 62 | + this.connectionFactory = getConnectionFactory(); |
| 63 | + log.info("connection factory created"); |
| 64 | + this.connection = createConnection(config); |
| 65 | + log.info("connection created"); |
| 66 | + |
| 67 | + } catch (Exception e) { |
| 68 | + e.printStackTrace(); |
| 69 | + throw new ActivemqConnectorException( |
| 70 | + ActivemqConnectorErrorCode.CREATE_ACTIVEMQ_CLIENT_FAILED, |
| 71 | + "Error while create AMQ client "); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public ActiveMQConnectionFactory getConnectionFactory() { |
| 76 | + log.info("broker url : " + config.get(URI)); |
| 77 | + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(config.get(URI)); |
| 78 | + |
| 79 | + if (config.get(ALWAYS_SESSION_ASYNC) != null) { |
| 80 | + factory.setAlwaysSessionAsync(config.get(ALWAYS_SESSION_ASYNC)); |
| 81 | + } |
| 82 | + |
| 83 | + if (config.get(CLIENT_ID) != null) { |
| 84 | + factory.setClientID(config.get(CLIENT_ID)); |
| 85 | + } |
| 86 | + |
| 87 | + if (config.get(ALWAYS_SYNC_SEND) != null) { |
| 88 | + factory.setAlwaysSyncSend(config.get(ALWAYS_SYNC_SEND)); |
| 89 | + } |
| 90 | + |
| 91 | + if (config.get(CHECK_FOR_DUPLICATE) != null) { |
| 92 | + factory.setCheckForDuplicates(config.get(CHECK_FOR_DUPLICATE)); |
| 93 | + } |
| 94 | + |
| 95 | + if (config.get(CLOSE_TIMEOUT) != null) { |
| 96 | + factory.setCloseTimeout(config.get(CLOSE_TIMEOUT)); |
| 97 | + } |
| 98 | + |
| 99 | + if (config.get(CONSUMER_EXPIRY_CHECK_ENABLED) != null) { |
| 100 | + factory.setConsumerExpiryCheckEnabled(config.get(CONSUMER_EXPIRY_CHECK_ENABLED)); |
| 101 | + } |
| 102 | + if (config.get(DISPATCH_ASYNC) != null) { |
| 103 | + factory.setDispatchAsync(config.get(DISPATCH_ASYNC)); |
| 104 | + } |
| 105 | + |
| 106 | + if (config.get(WARN_ABOUT_UNSTARTED_CONNECTION_TIMEOUT) != null) { |
| 107 | + factory.setWarnAboutUnstartedConnectionTimeout( |
| 108 | + config.get(WARN_ABOUT_UNSTARTED_CONNECTION_TIMEOUT)); |
| 109 | + } |
| 110 | + |
| 111 | + if (config.get(NESTED_MAP_AND_LIST_ENABLED) != null) { |
| 112 | + factory.setNestedMapAndListEnabled(config.get(NESTED_MAP_AND_LIST_ENABLED)); |
| 113 | + } |
| 114 | + return factory; |
| 115 | + } |
| 116 | + |
| 117 | + public void write(byte[] msg) { |
| 118 | + try { |
| 119 | + this.connection.start(); |
| 120 | + Session session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); |
| 121 | + Destination destination = session.createQueue(config.get(QUEUE_NAME)); |
| 122 | + MessageProducer producer = session.createProducer(destination); |
| 123 | + String messageBody = new String(msg, StandardCharsets.UTF_8); |
| 124 | + TextMessage objectMessage = session.createTextMessage(messageBody); |
| 125 | + producer.send(objectMessage); |
| 126 | + |
| 127 | + } catch (JMSException e) { |
| 128 | + throw new ActivemqConnectorException( |
| 129 | + ActivemqConnectorErrorCode.SEND_MESSAGE_FAILED, |
| 130 | + String.format( |
| 131 | + "Cannot send AMQ message %s at %s", |
| 132 | + config.get(QUEUE_NAME), config.get(CLIENT_ID)), |
| 133 | + e); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public void close() { |
| 138 | + try { |
| 139 | + if (connection != null) { |
| 140 | + connection.close(); |
| 141 | + } |
| 142 | + } catch (JMSException e) { |
| 143 | + throw new ActivemqConnectorException( |
| 144 | + ActivemqConnectorErrorCode.CLOSE_CONNECTION_FAILED, |
| 145 | + String.format( |
| 146 | + "Error while closing AMQ connection with %s", config.get(QUEUE_NAME))); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private Connection createConnection(ReadonlyConfig config) throws JMSException { |
| 151 | + if (config.get(USERNAME) != null && config.get(PASSWORD) != null) { |
| 152 | + return connectionFactory.createConnection(config.get(USERNAME), config.get(PASSWORD)); |
| 153 | + } |
| 154 | + return connectionFactory.createConnection(); |
| 155 | + } |
| 156 | +} |
0 commit comments