michaeljmarshall commented on issue #17987:
URL: https://github.com/apache/pulsar/issues/17987#issuecomment-1275533659

   Posting these for future reference:
   
   ```java
   import java.nio.charset.StandardCharsets;
   import java.util.Objects;
   import org.apache.pulsar.client.api.*;
   import org.apache.pulsar.client.impl.auth.AuthenticationTls;
   
   public class Producer {
       public static final String PULSAR_SERVICE_URL = 
"pulsar+ssl://localhost:6651/";
       public static final String TOPIC_NAME = 
"persistent://public/default/mytopic2";
   
       public static PulsarClient getPulsarClient() throws 
PulsarClientException {
           return PulsarClient.builder().serviceUrl(PULSAR_SERVICE_URL).build();
       }
   
       public static void main(String[] args) throws PulsarClientException {
   
   
           /*PulsarClient client = PulsarClient.builder()
                   .serviceUrl(PULSAR_SERVICE_URL)
                   .tlsTrustCertsFilePath("D:\\my-ca\\certs\\ca.cert.pem")
                   
.authentication("org.apache.pulsar.client.impl.auth.AuthenticationTls",
                           "tlsCertFile:D:\\my-ca\\client.cert.pem, 
tlsKeyFile:D:\\my-ca\\client.key-pk8.pem")
                   .build();*/
   
           AuthenticationFactory auth = new AuthenticationFactory();
           PulsarClient client = PulsarClient.builder()
                   
.tlsTrustCertsFilePath("/pulsar/conf/my-ca/certs/ca.cert.pem")
                   .authentication(auth.TLS(
                           "/pulsar/conf/my-ca/client.cert.pem",
                           "/pulsar/conf/my-ca/client.key-pk8.pem"))
                           .serviceUrl(PULSAR_SERVICE_URL)
                           .build();
   
   
           //PulsarClient pulsarClient = null;
           try {
   
               client = getPulsarClient();
               org.apache.pulsar.client.api.Producer<byte[]> producer = client
                       .newProducer()
                       .topic("mytopic2")
                       .create();
   
               for (int i = 0; i < 10; i++) {
                   TypedMessageBuilder<byte[]> typedMessageBuilder = 
producer.newMessage();
                   typedMessageBuilder.value(("Hi! There, How are you? Hope you 
are doing - " + i)
                           .getBytes(StandardCharsets.UTF_8));
                   MessageId messageId = typedMessageBuilder.send();
                   System.out.println("Message Id " + messageId);
               }
           }
   
           catch(Exception e)
           {
           e.printStackTrace();
           }
           finally {
               if (Objects.nonNull(client)) {
                   client.close();
               }
           }
       }
   }
   ```
   
   ```java
   import org.apache.pulsar.client.api.Message;
   import org.apache.pulsar.client.api.PulsarClient;
   import org.apache.pulsar.client.api.PulsarClientException;
   
   import java.nio.charset.StandardCharsets;
   import java.util.Objects;
   
   public class Consumer
   {
           public static final String PULSAR_SERVICE_URL = 
"pulsar+ssl://localhost:6651/";
   
           public static PulsarClient getPulsarClient() throws 
PulsarClientException {
               return 
PulsarClient.builder().serviceUrl(PULSAR_SERVICE_URL).build();
           }
   
           public static void main(String[] args) throws PulsarClientException {
               PulsarClient pulsarClient = null;
   
               try {
                   pulsarClient = getPulsarClient();
                   org.apache.pulsar.client.api.Consumer<byte[]> consumer = 
pulsarClient.newConsumer()
                           .topic("mytopic2")
                           .subscriptionName("test-sub")
                           .subscribe();
   
                   while(true) {
                       Message<byte[]> message = consumer.receive();
                       String messageValue = new String(message.getData(), 
StandardCharsets.UTF_8);
                       System.out.println("Message Received:  " + ", Value = " 
+ messageValue);
                       consumer.acknowledge(message.getMessageId());
                   }
   
               } finally {
                   if (Objects.nonNull(pulsarClient)) {
                       pulsarClient.close();
                   }
               }
           }
       }
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to