|
| 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.eventmesh.connector.http.sink; |
| 19 | + |
| 20 | +import org.apache.eventmesh.connector.http.sink.config.HttpSinkConfig; |
| 21 | +import org.apache.eventmesh.connector.http.sink.config.SinkConnectorConfig; |
| 22 | +import org.apache.eventmesh.connector.http.sink.handle.CommonHttpSinkHandler; |
| 23 | +import org.apache.eventmesh.connector.http.sink.handle.HttpSinkHandler; |
| 24 | +import org.apache.eventmesh.connector.http.sink.handle.RetryHttpSinkHandler; |
| 25 | +import org.apache.eventmesh.connector.http.sink.handle.WebhookHttpSinkHandler; |
| 26 | +import org.apache.eventmesh.openconnect.api.config.Config; |
| 27 | +import org.apache.eventmesh.openconnect.api.connector.ConnectorContext; |
| 28 | +import org.apache.eventmesh.openconnect.api.connector.SinkConnectorContext; |
| 29 | +import org.apache.eventmesh.openconnect.api.sink.Sink; |
| 30 | +import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord; |
| 31 | + |
| 32 | +import java.util.List; |
| 33 | +import java.util.Objects; |
| 34 | + |
| 35 | +import lombok.Getter; |
| 36 | +import lombok.SneakyThrows; |
| 37 | +import lombok.extern.slf4j.Slf4j; |
| 38 | + |
| 39 | +@Slf4j |
| 40 | +public class HttpSinkConnector implements Sink { |
| 41 | + |
| 42 | + private HttpSinkConfig httpSinkConfig; |
| 43 | + |
| 44 | + @Getter |
| 45 | + private HttpSinkHandler sinkHandler; |
| 46 | + |
| 47 | + @Override |
| 48 | + public Class<? extends Config> configClass() { |
| 49 | + return HttpSinkConfig.class; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void init(Config config) throws Exception { |
| 54 | + this.httpSinkConfig = (HttpSinkConfig) config; |
| 55 | + doInit(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void init(ConnectorContext connectorContext) throws Exception { |
| 60 | + SinkConnectorContext sinkConnectorContext = (SinkConnectorContext) connectorContext; |
| 61 | + this.httpSinkConfig = (HttpSinkConfig) sinkConnectorContext.getSinkConfig(); |
| 62 | + doInit(); |
| 63 | + } |
| 64 | + |
| 65 | + @SneakyThrows |
| 66 | + private void doInit() { |
| 67 | + // Fill default values if absent |
| 68 | + SinkConnectorConfig.populateFieldsWithDefaults(this.httpSinkConfig.connectorConfig); |
| 69 | + // Create different handlers for different configurations |
| 70 | + HttpSinkHandler nonRetryHandler; |
| 71 | + if (this.httpSinkConfig.connectorConfig.getWebhookConfig().isActivate()) { |
| 72 | + nonRetryHandler = new WebhookHttpSinkHandler(this.httpSinkConfig.connectorConfig); |
| 73 | + } else { |
| 74 | + nonRetryHandler = new CommonHttpSinkHandler(this.httpSinkConfig.connectorConfig); |
| 75 | + } |
| 76 | + |
| 77 | + int maxRetries = this.httpSinkConfig.connectorConfig.getRetryConfig().getMaxRetries(); |
| 78 | + if (maxRetries == 0) { |
| 79 | + // Use the original sink handler |
| 80 | + this.sinkHandler = nonRetryHandler; |
| 81 | + } else if (maxRetries > 0) { |
| 82 | + // Wrap the sink handler with a retry handler |
| 83 | + this.sinkHandler = new RetryHttpSinkHandler(this.httpSinkConfig.connectorConfig, nonRetryHandler); |
| 84 | + } else { |
| 85 | + throw new IllegalArgumentException("Max retries must be greater than or equal to 0."); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void start() throws Exception { |
| 91 | + this.sinkHandler.start(); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void commit(ConnectRecord record) { |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String name() { |
| 101 | + return this.httpSinkConfig.connectorConfig.getConnectorName(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void stop() throws Exception { |
| 106 | + this.sinkHandler.stop(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void put(List<ConnectRecord> sinkRecords) { |
| 111 | + for (ConnectRecord sinkRecord : sinkRecords) { |
| 112 | + try { |
| 113 | + if (Objects.isNull(sinkRecord)) { |
| 114 | + log.warn("ConnectRecord data is null, ignore."); |
| 115 | + continue; |
| 116 | + } |
| 117 | + // Handle the ConnectRecord |
| 118 | + this.sinkHandler.handle(sinkRecord); |
| 119 | + } catch (Exception e) { |
| 120 | + log.error("Failed to sink message via HTTP. ", e); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments