18
18
package org .apache .eventmesh .connector .http .sink .handle ;
19
19
20
20
import org .apache .eventmesh .common .exception .EventMeshException ;
21
+ import org .apache .eventmesh .connector .http .common .SynchronizedCircularFifoQueue ;
21
22
import org .apache .eventmesh .connector .http .sink .config .HttpWebhookConfig ;
22
23
import org .apache .eventmesh .connector .http .sink .config .SinkConnectorConfig ;
23
24
import org .apache .eventmesh .connector .http .sink .data .HttpConnectRecord ;
30
31
31
32
import java .net .URI ;
32
33
import java .time .LocalDateTime ;
33
- import java .util .ArrayList ;
34
- import java .util .Iterator ;
35
34
import java .util .List ;
36
35
import java .util .Objects ;
37
- import java .util .concurrent .ConcurrentLinkedQueue ;
38
36
import java .util .concurrent .TimeUnit ;
39
- import java .util .concurrent .atomic .AtomicInteger ;
40
37
41
38
import io .netty .handler .codec .http .HttpResponseStatus ;
42
39
import io .vertx .core .Future ;
@@ -73,25 +70,22 @@ public class WebhookHttpSinkHandler extends CommonHttpSinkHandler {
73
70
private HttpServer exportServer ;
74
71
75
72
// store the received data, when webhook is enabled
76
- private final ConcurrentLinkedQueue <HttpExportRecord > receivedDataQueue ;
77
-
78
- // the maximum queue size
79
- private final int maxQueueSize ;
80
-
81
- // the current queue size
82
- private final AtomicInteger currentQueueSize ;
73
+ private final SynchronizedCircularFifoQueue <HttpExportRecord > receivedDataQueue ;
83
74
84
75
public WebhookHttpSinkHandler (SinkConnectorConfig sinkConnectorConfig ) {
85
76
super (sinkConnectorConfig );
86
77
this .sinkConnectorConfig = sinkConnectorConfig ;
87
78
this .webhookConfig = sinkConnectorConfig .getWebhookConfig ();
88
- this .maxQueueSize = this .webhookConfig .getMaxStorageSize ();
89
- this .currentQueueSize = new AtomicInteger (0 );
90
- this .receivedDataQueue = new ConcurrentLinkedQueue <>();
79
+ int maxQueueSize = this .webhookConfig .getMaxStorageSize ();
80
+ this .receivedDataQueue = new SynchronizedCircularFifoQueue <>(maxQueueSize );
91
81
// init the export server
92
82
doInitExportServer ();
93
83
}
94
84
85
+ public SynchronizedCircularFifoQueue <HttpExportRecord > getReceivedDataQueue () {
86
+ return receivedDataQueue ;
87
+ }
88
+
95
89
/**
96
90
* Initialize the server for exporting the received data
97
91
*/
@@ -135,7 +129,7 @@ private void doInitExportServer() {
135
129
int pageNum = StringUtils .isBlank (pageNumStr ) ? 1 : Integer .parseInt (pageNumStr );
136
130
int pageSize = Integer .parseInt (pageSizeStr );
137
131
138
- if (currentQueueSize . get () == 0 ) {
132
+ if (receivedDataQueue . isEmpty () ) {
139
133
ctx .response ()
140
134
.putHeader (HttpHeaders .CONTENT_TYPE , "application/json; charset=utf-8" )
141
135
.setStatusCode (HttpResponseStatus .NO_CONTENT .code ())
@@ -148,12 +142,12 @@ private void doInitExportServer() {
148
142
List <HttpExportRecord > exportRecords ;
149
143
if (Objects .equals (type , TypeEnum .POLL .getValue ())) {
150
144
// If the type is poll, only the first page of data is exported and removed
151
- exportRecords = getDataFromQueue (0 , pageSize , true );
145
+ exportRecords = receivedDataQueue . fetchRange (0 , pageSize , true );
152
146
} else {
153
147
// If the type is peek, the specified page of data is exported without removing
154
148
int startIndex = (pageNum - 1 ) * pageSize ;
155
149
int endIndex = startIndex + pageSize ;
156
- exportRecords = getDataFromQueue (startIndex , endIndex , false );
150
+ exportRecords = receivedDataQueue . fetchRange (startIndex , endIndex , false );
157
151
}
158
152
159
153
// Create HttpExportRecordPage
@@ -242,63 +236,11 @@ public Future<HttpResponse<Buffer>> deliver(URI url, HttpConnectRecord httpConne
242
236
// create ExportRecord
243
237
HttpExportRecord exportRecord = new HttpExportRecord (httpExportMetadata , arr .succeeded () ? arr .result ().bodyAsString () : null );
244
238
// add the data to the queue
245
- addDataToQueue (exportRecord );
239
+ receivedDataQueue . offer (exportRecord );
246
240
});
247
241
}
248
242
249
243
250
- /**
251
- * Adds the received data to the queue.
252
- *
253
- * @param exportRecord the received data to add to the queue
254
- */
255
- public void addDataToQueue (HttpExportRecord exportRecord ) {
256
- // If the current queue size is greater than or equal to the maximum queue size, remove the oldest element
257
- if (currentQueueSize .get () >= maxQueueSize ) {
258
- Object removedData = receivedDataQueue .poll ();
259
- if (log .isDebugEnabled ()) {
260
- log .debug ("The queue is full, remove the oldest element: {}" , removedData );
261
- } else {
262
- log .info ("The queue is full, remove the oldest element" );
263
- }
264
- currentQueueSize .decrementAndGet ();
265
- }
266
- // Try to put the received data into the queue
267
- if (receivedDataQueue .offer (exportRecord )) {
268
- currentQueueSize .incrementAndGet ();
269
- log .debug ("Successfully put the received data into the queue: {}" , exportRecord );
270
- } else {
271
- log .error ("Failed to put the received data into the queue: {}" , exportRecord );
272
- }
273
- }
274
-
275
- /**
276
- * Gets the received data from the queue.
277
- *
278
- * @param startIndex the start index of the data to get
279
- * @param endIndex the end index of the data to get
280
- * @param removed whether to remove the data from the queue
281
- * @return the received data
282
- */
283
- private List <HttpExportRecord > getDataFromQueue (int startIndex , int endIndex , boolean removed ) {
284
- Iterator <HttpExportRecord > iterator = receivedDataQueue .iterator ();
285
-
286
- List <HttpExportRecord > pageItems = new ArrayList <>(endIndex - startIndex );
287
- int count = 0 ;
288
- while (iterator .hasNext () && count < endIndex ) {
289
- HttpExportRecord item = iterator .next ();
290
- if (count >= startIndex ) {
291
- pageItems .add (item );
292
- if (removed ) {
293
- iterator .remove ();
294
- currentQueueSize .decrementAndGet ();
295
- }
296
- }
297
- count ++;
298
- }
299
- return pageItems ;
300
- }
301
-
302
244
/**
303
245
* Cleans up and releases resources used by the HTTP/HTTPS handler.
304
246
*/
0 commit comments