2121import org .apache .eventmesh .connector .http .sink .config .SinkConnectorConfig ;
2222import org .apache .eventmesh .connector .http .sink .data .HttpConnectRecord ;
2323import org .apache .eventmesh .connector .http .util .HttpUtils ;
24+ import org .apache .eventmesh .openconnect .offsetmgmt .api .callback .SendExceptionContext ;
25+ import org .apache .eventmesh .openconnect .offsetmgmt .api .callback .SendResult ;
2426import org .apache .eventmesh .openconnect .offsetmgmt .api .data .ConnectRecord ;
2527
2628import java .net .URI ;
@@ -111,14 +113,70 @@ public void handle(ConnectRecord record) {
111113 // convert ConnectRecord to HttpConnectRecord
112114 String type = String .format ("%s.%s.%s" , connectorConfig .getConnectorName (), url .getScheme (), "common" );
113115 HttpConnectRecord httpConnectRecord = HttpConnectRecord .convertConnectRecord (record , type );
114- deliver (url , httpConnectRecord );
116+ // get timestamp and offset
117+ Long timestamp = httpConnectRecord .getData ().getTimestamp ();
118+ Map <String , ?> offset = null ;
119+ try {
120+ // May throw NullPointerException.
121+ offset = ((HttpRecordOffset ) httpConnectRecord .getData ().getPosition ().getRecordOffset ()).getOffsetMap ();
122+ } catch (NullPointerException e ) {
123+ // ignore null pointer exception
124+ }
125+ final Map <String , ?> finalOffset = offset ;
126+ Future <HttpResponse <Buffer >> responseFuture = deliver (url , httpConnectRecord );
127+ responseFuture .onSuccess (res -> {
128+ log .info ("Request sent successfully. Record: timestamp={}, offset={}" , timestamp , finalOffset );
129+ // log the response
130+ if (HttpUtils .is2xxSuccessful (res .statusCode ())) {
131+ if (log .isDebugEnabled ()) {
132+ log .debug ("Received successful response: statusCode={}. Record: timestamp={}, offset={}, responseBody={}" ,
133+ res .statusCode (), timestamp , finalOffset , res .bodyAsString ());
134+ } else {
135+ log .info ("Received successful response: statusCode={}. Record: timestamp={}, offset={}" , res .statusCode (), timestamp ,
136+ finalOffset );
137+ }
138+ record .getCallback ().onSuccess (convertToSendResult (record ));
139+ } else {
140+ if (log .isDebugEnabled ()) {
141+ log .warn ("Received non-2xx response: statusCode={}. Record: timestamp={}, offset={}, responseBody={}" ,
142+ res .statusCode (), timestamp , finalOffset , res .bodyAsString ());
143+ } else {
144+ log .warn ("Received non-2xx response: statusCode={}. Record: timestamp={}, offset={}" , res .statusCode (), timestamp ,
145+ finalOffset );
146+ }
147+ record .getCallback ()
148+ .onException (buildSendExceptionContext (record , new RuntimeException ("HTTP response code: " + res .statusCode ())));
149+ }
150+ }).onFailure (err -> {
151+ log .error ("Request failed to send. Record: timestamp={}, offset={}" , timestamp , finalOffset , err );
152+ record .getCallback ().onException (buildSendExceptionContext (record , err ));
153+ });
154+ }
155+ }
156+
157+ private SendResult convertToSendResult (ConnectRecord record ) {
158+ SendResult result = new SendResult ();
159+ result .setMessageId (record .getRecordId ());
160+ if (org .apache .commons .lang3 .StringUtils .isNotEmpty (record .getExtension ("topic" ))) {
161+ result .setTopic (record .getExtension ("topic" ));
162+ }
163+ return result ;
164+ }
165+
166+ private SendExceptionContext buildSendExceptionContext (ConnectRecord record , Throwable e ) {
167+ SendExceptionContext sendExceptionContext = new SendExceptionContext ();
168+ sendExceptionContext .setMessageId (record .getRecordId ());
169+ sendExceptionContext .setCause (e );
170+ if (org .apache .commons .lang3 .StringUtils .isNotEmpty (record .getExtension ("topic" ))) {
171+ sendExceptionContext .setTopic (record .getExtension ("topic" ));
115172 }
173+ return sendExceptionContext ;
116174 }
117175
118176
119177 /**
120- * Processes HttpConnectRecord on specified URL while returning its own processing logic.
121- * This method sends the HttpConnectRecord to the specified URL using the WebClient.
178+ * Processes HttpConnectRecord on specified URL while returning its own processing logic. This method sends the HttpConnectRecord to the specified
179+ * URL using the WebClient.
122180 *
123181 * @param url URI to which the HttpConnectRecord should be sent
124182 * @param httpConnectRecord HttpConnectRecord to process
@@ -130,48 +188,13 @@ public Future<HttpResponse<Buffer>> deliver(URI url, HttpConnectRecord httpConne
130188 MultiMap headers = HttpHeaders .headers ()
131189 .set (HttpHeaderNames .CONTENT_TYPE , "application/json; charset=utf-8" )
132190 .set (HttpHeaderNames .ACCEPT , "application/json; charset=utf-8" );
133-
134- // get timestamp and offset
135- Long timestamp = httpConnectRecord .getData ().getTimestamp ();
136- Map <String , ?> offset = null ;
137- try {
138- // May throw NullPointerException.
139- offset = ((HttpRecordOffset ) httpConnectRecord .getData ().getPosition ().getRecordOffset ()).getOffsetMap ();
140- } catch (NullPointerException e ) {
141- // ignore null pointer exception
142- }
143- final Map <String , ?> finalOffset = offset ;
144-
145191 // send the request
146192 return this .webClient .post (url .getPath ())
147193 .host (url .getHost ())
148194 .port (url .getPort () == -1 ? (Objects .equals (url .getScheme (), "https" ) ? 443 : 80 ) : url .getPort ())
149195 .putHeaders (headers )
150196 .ssl (Objects .equals (url .getScheme (), "https" ))
151- .sendJson (httpConnectRecord )
152- .onSuccess (res -> {
153- log .info ("Request sent successfully. Record: timestamp={}, offset={}" , timestamp , finalOffset );
154- // log the response
155- if (HttpUtils .is2xxSuccessful (res .statusCode ())) {
156- if (log .isDebugEnabled ()) {
157- log .debug ("Received successful response: statusCode={}. Record: timestamp={}, offset={}, responseBody={}" ,
158- res .statusCode (), timestamp , finalOffset , res .bodyAsString ());
159- } else {
160- log .info ("Received successful response: statusCode={}. Record: timestamp={}, offset={}" , res .statusCode (), timestamp ,
161- finalOffset );
162- }
163- } else {
164- if (log .isDebugEnabled ()) {
165- log .warn ("Received non-2xx response: statusCode={}. Record: timestamp={}, offset={}, responseBody={}" ,
166- res .statusCode (), timestamp , finalOffset , res .bodyAsString ());
167- } else {
168- log .warn ("Received non-2xx response: statusCode={}. Record: timestamp={}, offset={}" , res .statusCode (), timestamp ,
169- finalOffset );
170- }
171- }
172-
173- })
174- .onFailure (err -> log .error ("Request failed to send. Record: timestamp={}, offset={}" , timestamp , finalOffset , err ));
197+ .sendJson (httpConnectRecord );
175198 }
176199
177200
0 commit comments