|
| 1 | +package com.oci.stream; |
| 2 | + |
| 3 | +import com.google.common.util.concurrent.Uninterruptibles; |
| 4 | +import com.oracle.bmc.ConfigFileReader; |
| 5 | +import com.oracle.bmc.auth.AuthenticationDetailsProvider; |
| 6 | +import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider; |
| 7 | +import com.oracle.bmc.streaming.StreamClient; |
| 8 | +import com.oracle.bmc.streaming.model.CreateGroupCursorDetails; |
| 9 | +import com.oracle.bmc.streaming.model.Message; |
| 10 | +import com.oracle.bmc.streaming.requests.CreateGroupCursorRequest; |
| 11 | +import com.oracle.bmc.streaming.requests.GetMessagesRequest; |
| 12 | +import com.oracle.bmc.streaming.responses.CreateGroupCursorResponse; |
| 13 | +import com.oracle.bmc.streaming.responses.GetMessagesResponse; |
| 14 | + |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | + |
| 19 | + |
| 20 | +public class Consumer { |
| 21 | + public static void main(String[] args) throws Exception { |
| 22 | + final String configurationFilePath = "/home/fernando_h/.oci/config"; |
| 23 | + final String profile = "DEFAULT"; |
| 24 | + final String ociStreamOcid = "ocid1.stream.oc1.eu-frankfurt-1.amaaaaaaue..."; |
| 25 | + final String ociMessageEndpoint = "https://cell-1.streaming.eu-frankfurt-1.oci.oraclecloud.com"; |
| 26 | + |
| 27 | + final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault(); |
| 28 | + final AuthenticationDetailsProvider provider = |
| 29 | + new ConfigFileAuthenticationDetailsProvider(configFile); |
| 30 | + |
| 31 | + // Streams are assigned a specific endpoint url based on where they are provisioned. |
| 32 | + // Create a stream client using the provided message endpoint. |
| 33 | + StreamClient streamClient = StreamClient.builder().endpoint(ociMessageEndpoint).build(provider); |
| 34 | + |
| 35 | + // A cursor can be created as part of a consumer group. |
| 36 | + // Committed offsets are managed for the group, and partitions |
| 37 | + // are dynamically balanced amongst consumers in the group. |
| 38 | + System.out.println("Starting a simple message loop with a group cursor"); |
| 39 | + String groupCursor = |
| 40 | + getCursorByGroup(streamClient, ociStreamOcid, "exampleGroup", "exampleInstance-1"); |
| 41 | + simpleMessageLoop(streamClient, ociStreamOcid, groupCursor); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + private static void simpleMessageLoop( |
| 46 | + StreamClient streamClient, String streamId, String initialCursor) { |
| 47 | + String cursor = initialCursor; |
| 48 | + for (int i = 0; i < 10; i++) { |
| 49 | + |
| 50 | + GetMessagesRequest getRequest = |
| 51 | + GetMessagesRequest.builder() |
| 52 | + .streamId(streamId) |
| 53 | + .cursor(cursor) |
| 54 | + .limit(25) |
| 55 | + .build(); |
| 56 | + |
| 57 | + GetMessagesResponse getResponse = streamClient.getMessages(getRequest); |
| 58 | + |
| 59 | + // process the messages |
| 60 | + System.out.println(String.format("Read %s messages.", getResponse.getItems().size())); |
| 61 | + for (Message message : ((GetMessagesResponse) getResponse).getItems()) { |
| 62 | + System.out.println( |
| 63 | + String.format( |
| 64 | + "%s: %s", |
| 65 | + message.getKey() == null ? "Null" :new String(message.getKey(), UTF_8), |
| 66 | + new String(message.getValue(), UTF_8))); |
| 67 | + } |
| 68 | + |
| 69 | + // getMessages is a throttled method; clients should retrieve sufficiently large message |
| 70 | + // batches, as to avoid too many http requests. |
| 71 | + Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); |
| 72 | + |
| 73 | + // use the next-cursor for iteration |
| 74 | + cursor = getResponse.getOpcNextCursor(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static String getCursorByGroup( |
| 79 | + StreamClient streamClient, String streamId, String groupName, String instanceName) { |
| 80 | + System.out.println( |
| 81 | + String.format( |
| 82 | + "Creating a cursor for group %s, instance %s.", groupName, instanceName)); |
| 83 | + |
| 84 | + CreateGroupCursorDetails cursorDetails = |
| 85 | + CreateGroupCursorDetails.builder() |
| 86 | + .groupName(groupName) |
| 87 | + .instanceName(instanceName) |
| 88 | + .type(CreateGroupCursorDetails.Type.TrimHorizon) |
| 89 | + .commitOnGet(true) |
| 90 | + .build(); |
| 91 | + |
| 92 | + CreateGroupCursorRequest createCursorRequest = |
| 93 | + CreateGroupCursorRequest.builder() |
| 94 | + .streamId(streamId) |
| 95 | + .createGroupCursorDetails(cursorDetails) |
| 96 | + .build(); |
| 97 | + |
| 98 | + CreateGroupCursorResponse groupCursorResponse = |
| 99 | + streamClient.createGroupCursor(createCursorRequest); |
| 100 | + return groupCursorResponse.getCursor().getValue(); |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments