|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iggy.examples.tcptls.producer; |
| 21 | + |
| 22 | +import org.apache.iggy.client.blocking.tcp.IggyTcpClient; |
| 23 | +import org.apache.iggy.identifier.StreamId; |
| 24 | +import org.apache.iggy.identifier.TopicId; |
| 25 | +import org.apache.iggy.message.Message; |
| 26 | +import org.apache.iggy.message.Partitioning; |
| 27 | +import org.apache.iggy.stream.StreamDetails; |
| 28 | +import org.apache.iggy.topic.CompressionAlgorithm; |
| 29 | +import org.apache.iggy.topic.TopicDetails; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +import java.math.BigInteger; |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Optional; |
| 37 | + |
| 38 | +import static java.util.Optional.empty; |
| 39 | + |
| 40 | +/** |
| 41 | + * TCP/TLS Producer Example |
| 42 | + * |
| 43 | + * <p>Demonstrates producing messages over a TLS-encrypted TCP connection |
| 44 | + * using custom certificates from core/certs/. |
| 45 | + * |
| 46 | + * <p>Prerequisites: Start the Iggy server with TLS enabled: |
| 47 | + * <pre> |
| 48 | + * IGGY_TCP_TLS_ENABLED=true \ |
| 49 | + * IGGY_TCP_TLS_CERT_FILE=core/certs/iggy_cert.pem \ |
| 50 | + * IGGY_TCP_TLS_KEY_FILE=core/certs/iggy_key.pem \ |
| 51 | + * cargo r --bin iggy-server |
| 52 | + * </pre> |
| 53 | + */ |
| 54 | +public final class TcpTlsProducer { |
| 55 | + |
| 56 | + private static final String STREAM_NAME = "tls-stream"; |
| 57 | + private static final StreamId STREAM_ID = StreamId.of(STREAM_NAME); |
| 58 | + |
| 59 | + private static final String TOPIC_NAME = "tls-topic"; |
| 60 | + private static final TopicId TOPIC_ID = TopicId.of(TOPIC_NAME); |
| 61 | + |
| 62 | + private static final long PARTITION_ID = 0L; |
| 63 | + |
| 64 | + private static final int BATCHES_LIMIT = 5; |
| 65 | + |
| 66 | + private static final int MESSAGES_PER_BATCH = 10; |
| 67 | + private static final long INTERVAL_MS = 500; |
| 68 | + |
| 69 | + private static final Logger log = LoggerFactory.getLogger(TcpTlsProducer.class); |
| 70 | + |
| 71 | + private TcpTlsProducer() {} |
| 72 | + |
| 73 | + public static void main(String[] args) { |
| 74 | + // Build a TCP client with TLS enabled. |
| 75 | + // enableTls() activates TLS on the TCP transport |
| 76 | + // tlsCertificate(...) points to the CA certificate used to verify the server cert |
| 77 | + var client = IggyTcpClient.builder() |
| 78 | + .host("localhost") |
| 79 | + .port(8090) |
| 80 | + .enableTls() |
| 81 | + .tlsCertificate("../../core/certs/iggy_ca_cert.pem") |
| 82 | + .credentials("iggy", "iggy") |
| 83 | + .buildAndLogin(); |
| 84 | + |
| 85 | + createStream(client); |
| 86 | + createTopic(client); |
| 87 | + produceMessages(client); |
| 88 | + } |
| 89 | + |
| 90 | + private static void produceMessages(IggyTcpClient client) { |
| 91 | + log.info( |
| 92 | + "Messages will be sent to stream: {}, topic: {}, partition: {} with interval {}ms.", |
| 93 | + STREAM_NAME, |
| 94 | + TOPIC_NAME, |
| 95 | + PARTITION_ID, |
| 96 | + INTERVAL_MS); |
| 97 | + |
| 98 | + int currentId = 0; |
| 99 | + int sentBatches = 0; |
| 100 | + |
| 101 | + Partitioning partitioning = Partitioning.partitionId(PARTITION_ID); |
| 102 | + |
| 103 | + while (sentBatches < BATCHES_LIMIT) { |
| 104 | + try { |
| 105 | + Thread.sleep(INTERVAL_MS); |
| 106 | + } catch (InterruptedException e) { |
| 107 | + Thread.currentThread().interrupt(); |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + List<Message> messages = new ArrayList<>(); |
| 112 | + for (int i = 0; i < MESSAGES_PER_BATCH; i++) { |
| 113 | + currentId++; |
| 114 | + String payload = "message-" + currentId; |
| 115 | + messages.add(Message.of(payload)); |
| 116 | + } |
| 117 | + |
| 118 | + client.messages().sendMessages(STREAM_ID, TOPIC_ID, partitioning, messages); |
| 119 | + sentBatches++; |
| 120 | + log.info("Sent {} message(s).", MESSAGES_PER_BATCH); |
| 121 | + } |
| 122 | + |
| 123 | + log.info("Sent {} batches of messages, exiting.", sentBatches); |
| 124 | + } |
| 125 | + |
| 126 | + private static void createStream(IggyTcpClient client) { |
| 127 | + Optional<StreamDetails> stream = client.streams().getStream(STREAM_ID); |
| 128 | + if (stream.isPresent()) { |
| 129 | + return; |
| 130 | + } |
| 131 | + client.streams().createStream(STREAM_NAME); |
| 132 | + log.info("Stream {} was created.", STREAM_NAME); |
| 133 | + } |
| 134 | + |
| 135 | + private static void createTopic(IggyTcpClient client) { |
| 136 | + Optional<TopicDetails> topic = client.topics().getTopic(STREAM_ID, TOPIC_ID); |
| 137 | + if (topic.isPresent()) { |
| 138 | + log.warn("Topic already exists and will not be created again."); |
| 139 | + return; |
| 140 | + } |
| 141 | + client.topics() |
| 142 | + .createTopic( |
| 143 | + STREAM_ID, |
| 144 | + 1L, |
| 145 | + CompressionAlgorithm.None, |
| 146 | + BigInteger.ZERO, |
| 147 | + BigInteger.ZERO, |
| 148 | + empty(), |
| 149 | + TOPIC_NAME); |
| 150 | + log.info("Topic {} was created.", TOPIC_NAME); |
| 151 | + } |
| 152 | +} |
0 commit comments