generated from oracle-devrel/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSoloOCIQueueStompDemoTool.groovy
532 lines (478 loc) · 18.8 KB
/
SoloOCIQueueStompDemoTool.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.stomp.Frame;
import io.vertx.ext.stomp.StompClient;
import io.vertx.ext.stomp.StompClientConnection;
import io.vertx.ext.stomp.StompClientOptions;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.util.Properties;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
* Implements or provides reference code to establish a STOMP connection for OCI Queue Service
* https://docs.oracle.com/en-us/iaas/Content/queue/messages-stomp.htm
*
* The example uses vertx-stomp libraries to create a stomp client to establish connection.
* OCI Queue service uses port 61613 for STOMP connection.
*/
public class SoloOCIQueueStompDemoTool {
private static final String QUEUEOCID = "QUEUEOCID";
private static final String REGION = "REGION";
private static final String ISVERBOSE = "VERBOSE";
private static final String AUTHTOKEN = "AUTHTOKEN";
private static final String POSTSENDDELAYSECS = "POSTSENDDELAYSECS";
private static final String POLLDURATIONSECS = "POLLDURATIONSECS";
private static final String UNAME = "USERNAME";
private static final String TENANCY = "TENANCY";
private static final String MAXSENDS="TOTALSEND";
private static final String DTG_FORMAT = "HH:mm:ss.SSS";
private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DTG_FORMAT);
private static final String ACTION_SEND = "send";
private static final String ACTION_CONSUME = "consume";
private static final String ACTION_TEST = "test";
private static final String ACTION_HELP = "help";
// derived internal values
private static final String LOGIN = "LOGIN";
private static final String HOSTNAME = "DP_HOSTNAME";
private static String action = null;
private static Properties props = new Properties();
private static boolean verbose = true;
private static StompClient stompClient = null;
private StompClientConnection stompClientConnection = null;
private Vertx vertx = null;
private CompletableFuture<Void> futureForAck = new CompletableFuture<>();
/*
* make sure we close connections cleanly
*/
public void finalize()
{
log ("finalizing ...");
if (stompClient != null)
{
stompClient.close();
}
if (vertx != null)
{
vertx.close();
}
}
/*
* constructor - just log creation of the object
*/
public SoloOCIQueueStompDemoTool () {
log ("SoloOCIQueueStompDemoTool instance created");
}
/*
* constructor - log creation of the object and store the
* connection object
*/
public SoloOCIQueueStompDemoTool (StompClientConnection conn) {
log ("SoloOCIQueueStompDemoTool instance created with connection object");
stompClientConnection = conn;
}
/*
* executes the app logic - driven by the env properties
* and CLI params received. All the actual core queue logic is
* delegated to internal methods
*/
public static void main(String[] args) {
configure (args);
// creates the vertx instance.
Vertx vertx = Vertx.vertx();
try {
String authCode = props.getProperty(AUTHTOKEN);
// The passcode should be base64 encoded.
String base64EncodedPasscode = Base64.getEncoder()
.encodeToString(authCode.getBytes(StandardCharsets.UTF_8));
final StompClientOptions options = new StompClientOptions()
.setHost(props.getProperty(HOSTNAME))
.setPort(61613)
.setSsl(true)
.setLogin(props.getProperty(LOGIN))
.setPasscode(base64EncodedPasscode);
options.setTcpKeepAlive(true);
log("Create STOMP client");
// Creates the stomp client with error frame handler
// and exception handler. Error frame handler is to handle the error frame
// received
// and exception handler is to handle the exception in case thrown.
stompClient = StompClient.create(vertx, options)
.errorFrameHandler(
frame -> log("Error Frame received! message: " + frame.toString()))
.exceptionHandler(
throwable -> log("Exception Occurs! message: " + throwable.toString()));
// establishes the connection, run the tests and join the results.
switch (action)
{
case ACTION_TEST:
getConnection(stompClient)
.thenCompose(conn -> new SoloOCIQueueStompDemoTool(conn).execTest())
.whenComplete((unused, error) -> {
// Error messages are already logged in respective futures
stompClient.close();
})
.join();
break;
case ACTION_SEND:
int sendCount = Integer.parseInt(props.getProperty(MAXSENDS, "0"));
if (sendCount > 0)
{
getConnection(stompClient)
.thenCompose(conn -> new SoloOCIQueueStompDemoTool(conn).execSendMultiple(sendCount))
.whenComplete((unused, error) -> {
// Error messages are already logged in respective futures
stompClient.close();
})
.join();
}
else
{
getConnection(stompClient)
.thenCompose(conn -> new SoloOCIQueueStompDemoTool(conn).execSend())
.whenComplete((unused, error) -> {
// Error messages are already logged in respective futures
stompClient.close();
})
.join();
}
break;
case ACTION_CONSUME:
int polldelay = Integer.parseInt(props.getProperty(POLLDURATIONSECS));
int maxCycles = Integer.parseInt(props.getProperty(MAXSENDS));
boolean completed = false;
while (maxCycles > 0)
{
getConnection(stompClient)
.thenCompose(conn -> new SoloOCIQueueStompDemoTool(conn).execConsume())
.whenComplete((unused, error) -> {
// Error messages are already logged in respective futures
stompClient.close();
})
.join();
maxCycles=maxCycles--;
takeNap(polldelay);
}
break;
case ACTION_HELP:
displayCLI();
break;
}
} finally {
// close the vertx instance.
vertx.close();
}
}
/**
* This gets the completable future of the stomp client connection.
* In case of an error, the future completes exceptionally.
*/
private static CompletableFuture<StompClientConnection> getConnection(StompClient stompClient) {
CompletableFuture<StompClientConnection> future = new CompletableFuture<>();
stompClient.connect(ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
return future;
}
/*
* Unsubscribes the given queue id. The queue id is the OCID of the queue to be
* unsubscribed.
*/
private CompletableFuture<Void> unsubscribe() {
CompletableFuture<Void> future = new CompletableFuture<>();
stompClientConnection.unsubscribe(props.getProperty(QUEUEOCID),
ar -> {
if (ar.succeeded()) {
log("RECEIPT for UNSUBSCRIBE frame received!");
future.complete(null);
} else {
log("Failure to process UNSUBSCRIBE frame!");
future.completeExceptionally(ar.cause());
}
});
log("UNSUBSCRIBE Frame is sent!");
return future;
}
/*
* This closes the connection. It throws an error in case there was an error in
* processing
* the disconnect frame otherwise sends the receipt back for disconnect call.
*/
private CompletableFuture<Void> disconnect() {
CompletableFuture<Void> future = new CompletableFuture<>();
stompClientConnection.disconnect(ar -> {
if (ar.succeeded()) {
log("RECEIPT for DISCONNECT frame received!");
future.complete(null);
} else {
log("Failure to process DISCONNECT frame!");
future.completeExceptionally(ar.cause());
}
});
log("DISCONNECT Frame is sent!");
return future;
}
/*
* This subscribes to the queue. Queue is OCID of the queue and is stored as
* constant in
* Environment variables java class. It has two handlers: message frame handler
* and receipt handler.
*/
private CompletableFuture<Void> subscribe() {
CompletableFuture<Void> future = new CompletableFuture<>();
final Map<String, String> queueSubscribeHeaders = new HashMap<>();
queueSubscribeHeaders.put(Frame.ACK, "client-individual");
stompClientConnection.subscribe(props.getProperty(QUEUEOCID),
queueSubscribeHeaders,
this::receiveMessage,
ar -> {
if (ar.succeeded()) {
log("RECEIPT for SUBSCRIBE frame received! subscription id=" + ar.result());
future.complete(null);
} else {
log("Failure to process SUBSCRIBE frame!");
future.completeExceptionally(ar.cause());
}
});
log("SUBSCRIBE Frame is sent!");
return future;
}
/*
* chains the call down to a common version of send message
* this means we can send a single message or multiple
*/
private CompletableFuture<Void> sendMessage() {
return sendMessage(0);
}
/*
* This sends the message to the queue. The queue is OCID of the destination
* queue.
*/
private CompletableFuture<Void> sendMessage(int msgNo) {
CompletableFuture<Void> future = new CompletableFuture<>();
String content = createMessage(msgNo);
stompClientConnection.send(props.getProperty(QUEUEOCID),
Buffer.buffer(content),
ar -> {
if (ar.succeeded()) {
log("RECEIPT for SEND frame received for " + content);
future.complete(null);
} else {
log("Failure to process SEND frame! cause:" + ar.cause());
future.completeExceptionally(ar.cause());
}
});
log("Send MESSAGE - " + content);
return future;
}
/*
* This processes the message received on the subscribed queue.
* It receives the ACK frame and completes the future for ACK.
*/
private void receiveMessage(Frame frame) {
String messageId = frame.getHeader("message-id");
String content = frame.getBodyAsString();
log("MESSAGE received, messageId=" + messageId +
" ,size=" + content.getBytes().length + ", message="+content);
stompClientConnection.ack(frame.getAck(),
ar -> {
if (ar.succeeded()) {
log("RECEIPT for ACK frame received!");
futureForAck.complete(null);
} else {
log("Failure to process ACK frame!");
futureForAck.completeExceptionally(ar.cause());
}
});
log("ACK frame sent");
}
/**
* Runs the test to chain the following:
* subscribe -> sendMessage -> getmessage -> deletemessage -> unsubscribe -> disconnect.
*/
private CompletableFuture<Void> execTest() {
return subscribe()
.thenCompose(unused -> sendMessage())
.thenCompose(unused -> futureForAck)
.thenCompose(unused -> unsubscribe())
.thenCompose(unused -> disconnect());
}
/*
* This drives the transmission of one or more messages
* when multiple messages are to be sent then the thread is put to sleep
* This means we don't make the logic run and excessive speed
*/
private CompletableFuture<Void> execSendMultiple(int noMessages) {
int napTime = Integer.parseInt(props.getProperty(POSTSENDDELAYSECS));
for (int ctr=0; ctr < noMessages-1; ctr++)
{
sendMessage(ctr).whenComplete((unused, error) -> {
// Error messages are already logged in respective futures
}).thenCompose(unused -> futureForAck);
takeNap(napTime);
}
return sendMessage(noMessages)
//.thenCompose(unused -> futureForAck)
.thenCompose(unused -> disconnect());
}
/*
* Drives the processing with a single message
*/
private CompletableFuture<Void> execSend() {
return sendMessage()
//.thenCompose(unused -> futureForAck)
.thenCompose(unused -> disconnect());
}
/*
* Runs the app in its consumer role. So we subscribe to receive messages
*/
private CompletableFuture<Void> execConsume() {
return subscribe()
.thenCompose(unused -> futureForAck)
.thenCompose(unused -> unsubscribe())
.thenCompose(unused -> disconnect());
}
/*
* Build a message to be sent
*/
private static String createMessage (int msgCtr)
{
LocalDateTime now = LocalDateTime.now();
String nowStr = now.format(dtf);
return "{\"MessageNo\" : " + msgCtr + ", \"at\": " + "\""+ nowStr + "\", \"sent\" : \"from client app\"}";
}
/*
* Simple utility function - trying to set a property with a null value triggers an exception - rather than wrapping all the code
* with the same If conditions - we've parameterized it.
*/
static void setPropertyFromVar(String propname, String envName, String defaultVal, Properties props)
{
String envVal = System.getenv(envName);
if ((envVal == null) && (defaultVal != null))
{
envVal = defaultVal;
}
if (envVal != null)
{
props.setProperty(propname, envVal);
}
}
/*
* To use a proper logging framework replace the calls to this method or direct the calls within this method to a logging framework
* This doesn't use a logging framework to minize the dependencies needing to be retrieved - making it easy to deploy and use as
* a single file application.
*/
private static void log (String msg)
{
if (verbose) {System.out.println (msg);}
}
/*
* to keep things from screaming through too quickly - we want to slow the main thread
* we can use this method to slow things down
*/
private static void takeNap(int napTime)
{
try
{
if (napTime > 0)
{
Thread.sleep (napTime*1000);
}
}
catch (Exception wokenUp)
{
log ("disturbed sleep");
}
}
/*
* Initialise all the control values and if there are anymissing default of kill the process
*/
private static void configure (String[] args)
{
setPropertyFromVar (REGION, REGION, null, props);
setPropertyFromVar (TENANCY, TENANCY, null, props);
setPropertyFromVar (AUTHTOKEN, AUTHTOKEN, null, props);
setPropertyFromVar (UNAME, UNAME, null, props);
setPropertyFromVar (QUEUEOCID, QUEUEOCID, null, props);
setPropertyFromVar (ISVERBOSE, ISVERBOSE, "TRUE", props);
setPropertyFromVar(MAXSENDS, MAXSENDS, "0", props);
setPropertyFromVar(POSTSENDDELAYSECS,POSTSENDDELAYSECS, "0", props);
setPropertyFromVar(POLLDURATIONSECS,POLLDURATIONSECS, "3", props);
verbose =((System.getenv(ISVERBOSE) == null) || (System.getenv(ISVERBOSE).trim().equalsIgnoreCase("true")));
if (props.getProperty(TENANCY) == null)
{
log("No tenancy set");
System.exit(1);
}
if (props.getProperty(UNAME) == null)
{
log ("No user defined");
System.exit(1);
}
if (props.getProperty(REGION) == null)
{
log ("No region set");
System.exit(1);
}
if (props.getProperty(AUTHTOKEN) == null)
{
props.setProperty(AUTHTOKEN, getTokenFromFile());
}
if (props.getProperty(AUTHTOKEN) == null)
{
log ("no auth token set");
System.exit(1);
}
props.setProperty(LOGIN, props.getProperty(TENANCY) + "/" + props.getProperty(UNAME)); // "YourTenancy/UserName"
props.setProperty(HOSTNAME, "cell-1.queue.messaging."+props.get(REGION)+".oci.oraclecloud.com");
if (args.length >0)
{
action = args[0].trim();
}
else
{
action = ACTION_TEST;
}
log ("action is:" + action);
}
/*
* As token strings can have characters that can be troublesome for env vars. We have the option to
* copy the token from a file called authtoken.txt which only contains the auth token
* @return the auth token in the file
*/
private static String getTokenFromFile()
{
String token = null;
try
{
BufferedReader objReader = new BufferedReader(new FileReader("authtoken.txt"));
token = objReader.readLine();
log ("pulled token from file>"+token+"<");
objReader.close();
}
catch (IOException ioErr)
{
log (ioErr.getMessage());
}
return token;
}
/*
* send to console the details of the command options available
*/
private static void displayCLI ()
{
System.out.println ("Options are: " + ACTION_SEND + " | " + ACTION_CONSUME + " | " + ACTION_TEST);
}
}