Search in sources :

Example 36 with Producer

use of org.apache.pulsar.client.api.Producer in project incubator-pulsar by apache.

the class V1_ProducerConsumerTest method testECDSAEncryption.

@Test(groups = "encryption")
public void testECDSAEncryption() throws Exception {
    log.info("-- Starting {} test --", methodName);
    class EncKeyReader implements CryptoKeyReader {

        EncryptionKeyInfo keyInfo = new EncryptionKeyInfo();

        @Override
        public EncryptionKeyInfo getPublicKey(String keyName, Map<String, String> keyMeta) {
            String CERT_FILE_PATH = "./src/test/resources/certificate/public-key." + keyName;
            if (Files.isReadable(Paths.get(CERT_FILE_PATH))) {
                try {
                    keyInfo.setKey(Files.readAllBytes(Paths.get(CERT_FILE_PATH)));
                    return keyInfo;
                } catch (IOException e) {
                    Assert.fail("Failed to read certificate from " + CERT_FILE_PATH);
                }
            } else {
                Assert.fail("Certificate file " + CERT_FILE_PATH + " is not present or not readable.");
            }
            return null;
        }

        @Override
        public EncryptionKeyInfo getPrivateKey(String keyName, Map<String, String> keyMeta) {
            String CERT_FILE_PATH = "./src/test/resources/certificate/private-key." + keyName;
            if (Files.isReadable(Paths.get(CERT_FILE_PATH))) {
                try {
                    keyInfo.setKey(Files.readAllBytes(Paths.get(CERT_FILE_PATH)));
                    return keyInfo;
                } catch (IOException e) {
                    Assert.fail("Failed to read certificate from " + CERT_FILE_PATH);
                }
            } else {
                Assert.fail("Certificate file " + CERT_FILE_PATH + " is not present or not readable.");
            }
            return null;
        }
    }
    final int totalMsg = 10;
    Set<String> messageSet = Sets.newHashSet();
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    conf.setCryptoKeyReader(new EncKeyReader());
    Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/myecdsa-topic1", "my-subscriber-name", conf);
    ProducerConfiguration producerConf = new ProducerConfiguration();
    producerConf.addEncryptionKey("client-ecdsa.pem");
    producerConf.setCryptoKeyReader(new EncKeyReader());
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/myecdsa-topic1", producerConf);
    for (int i = 0; i < totalMsg; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    Message msg = null;
    for (int i = 0; i < totalMsg; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
    }
    // Acknowledge the consumption of all messages at once
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : Message(org.apache.pulsar.client.api.Message) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) EncryptionKeyInfo(org.apache.pulsar.client.api.EncryptionKeyInfo) IOException(java.io.IOException) CryptoKeyReader(org.apache.pulsar.client.api.CryptoKeyReader) Consumer(org.apache.pulsar.client.api.Consumer) Producer(org.apache.pulsar.client.api.Producer) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 37 with Producer

use of org.apache.pulsar.client.api.Producer in project incubator-pulsar by apache.

the class V1_ProducerConsumerTest method testRSAEncryption.

@Test(groups = "encryption")
public void testRSAEncryption() throws Exception {
    log.info("-- Starting {} test --", methodName);
    class EncKeyReader implements CryptoKeyReader {

        EncryptionKeyInfo keyInfo = new EncryptionKeyInfo();

        @Override
        public EncryptionKeyInfo getPublicKey(String keyName, Map<String, String> keyMeta) {
            String CERT_FILE_PATH = "./src/test/resources/certificate/public-key." + keyName;
            if (Files.isReadable(Paths.get(CERT_FILE_PATH))) {
                try {
                    keyInfo.setKey(Files.readAllBytes(Paths.get(CERT_FILE_PATH)));
                    return keyInfo;
                } catch (IOException e) {
                    Assert.fail("Failed to read certificate from " + CERT_FILE_PATH);
                }
            } else {
                Assert.fail("Certificate file " + CERT_FILE_PATH + " is not present or not readable.");
            }
            return null;
        }

        @Override
        public EncryptionKeyInfo getPrivateKey(String keyName, Map<String, String> keyMeta) {
            String CERT_FILE_PATH = "./src/test/resources/certificate/private-key." + keyName;
            if (Files.isReadable(Paths.get(CERT_FILE_PATH))) {
                try {
                    keyInfo.setKey(Files.readAllBytes(Paths.get(CERT_FILE_PATH)));
                    return keyInfo;
                } catch (IOException e) {
                    Assert.fail("Failed to read certificate from " + CERT_FILE_PATH);
                }
            } else {
                Assert.fail("Certificate file " + CERT_FILE_PATH + " is not present or not readable.");
            }
            return null;
        }
    }
    final int totalMsg = 10;
    Set<String> messageSet = Sets.newHashSet();
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    conf.setCryptoKeyReader(new EncKeyReader());
    Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/myrsa-topic1", "my-subscriber-name", conf);
    ProducerConfiguration producerConf = new ProducerConfiguration();
    producerConf.addEncryptionKey("client-rsa.pem");
    producerConf.setCryptoKeyReader(new EncKeyReader());
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/myrsa-topic1", producerConf);
    Producer producer2 = pulsarClient.createProducer("persistent://my-property/use/my-ns/myrsa-topic1", producerConf);
    for (int i = 0; i < totalMsg; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    for (int i = totalMsg; i < totalMsg * 2; i++) {
        String message = "my-message-" + i;
        producer2.send(message.getBytes());
    }
    Message msg = null;
    for (int i = 0; i < totalMsg * 2; i++) {
        msg = consumer.receive(5, TimeUnit.SECONDS);
        String receivedMessage = new String(msg.getData());
        log.debug("Received message: [{}]", receivedMessage);
        String expectedMessage = "my-message-" + i;
        testMessageOrderAndDuplicates(messageSet, receivedMessage, expectedMessage);
    }
    // Acknowledge the consumption of all messages at once
    consumer.acknowledgeCumulative(msg);
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : Message(org.apache.pulsar.client.api.Message) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) EncryptionKeyInfo(org.apache.pulsar.client.api.EncryptionKeyInfo) IOException(java.io.IOException) CryptoKeyReader(org.apache.pulsar.client.api.CryptoKeyReader) Consumer(org.apache.pulsar.client.api.Consumer) Producer(org.apache.pulsar.client.api.Producer) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 38 with Producer

use of org.apache.pulsar.client.api.Producer in project incubator-pulsar by apache.

the class V1_ProducerConsumerTest method testDeactivatingBacklogConsumer.

@Test
public void testDeactivatingBacklogConsumer() throws Exception {
    log.info("-- Starting {} test --", methodName);
    final long batchMessageDelayMs = 100;
    final int receiverSize = 10;
    final String topicName = "cache-topic";
    final String topic = "persistent://my-property/use/my-ns/" + topicName;
    final String sub1 = "faster-sub1";
    final String sub2 = "slower-sub2";
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Shared);
    conf.setReceiverQueueSize(receiverSize);
    ProducerConfiguration producerConf = new ProducerConfiguration();
    if (batchMessageDelayMs != 0) {
        producerConf.setBatchingEnabled(true);
        producerConf.setBatchingMaxPublishDelay(batchMessageDelayMs, TimeUnit.MILLISECONDS);
        producerConf.setBatchingMaxMessages(5);
    }
    // 1. Subscriber Faster subscriber: let it consume all messages immediately
    Consumer subscriber1 = pulsarClient.subscribe("persistent://my-property/use/my-ns/" + topicName, sub1, conf);
    // 1.b. Subscriber Slow subscriber:
    conf.setReceiverQueueSize(receiverSize);
    Consumer subscriber2 = pulsarClient.subscribe("persistent://my-property/use/my-ns/" + topicName, sub2, conf);
    Producer producer = pulsarClient.createProducer(topic, producerConf);
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topic);
    ManagedLedgerImpl ledger = (ManagedLedgerImpl) topicRef.getManagedLedger();
    // reflection to set/get cache-backlog fields value:
    final long maxMessageCacheRetentionTimeMillis = 100;
    Field backlogThresholdField = ManagedLedgerImpl.class.getDeclaredField("maxActiveCursorBacklogEntries");
    backlogThresholdField.setAccessible(true);
    Field field = ManagedLedgerImpl.class.getDeclaredField("maxMessageCacheRetentionTimeMillis");
    field.setAccessible(true);
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(ledger, maxMessageCacheRetentionTimeMillis);
    final long maxActiveCursorBacklogEntries = (long) backlogThresholdField.get(ledger);
    Message msg = null;
    final int totalMsgs = (int) maxActiveCursorBacklogEntries + receiverSize + 1;
    // 2. Produce messages
    for (int i = 0; i < totalMsgs; i++) {
        String message = "my-message-" + i;
        producer.send(message.getBytes());
    }
    // 3. Consume messages: at Faster subscriber
    for (int i = 0; i < totalMsgs; i++) {
        msg = subscriber1.receive(100, TimeUnit.MILLISECONDS);
        subscriber1.acknowledge(msg);
    }
    // wait : so message can be eligible to to be evict from cache
    Thread.sleep(maxMessageCacheRetentionTimeMillis);
    // 4. deactivate subscriber which has built the backlog
    ledger.checkBackloggedCursors();
    Thread.sleep(100);
    // 5. verify: active subscribers
    Set<String> activeSubscriber = Sets.newHashSet();
    ledger.getActiveCursors().forEach(c -> activeSubscriber.add(c.getName()));
    assertTrue(activeSubscriber.contains(sub1));
    assertFalse(activeSubscriber.contains(sub2));
    // 6. consume messages : at slower subscriber
    for (int i = 0; i < totalMsgs; i++) {
        msg = subscriber2.receive(100, TimeUnit.MILLISECONDS);
        subscriber2.acknowledge(msg);
    }
    ledger.checkBackloggedCursors();
    activeSubscriber.clear();
    ledger.getActiveCursors().forEach(c -> activeSubscriber.add(c.getName()));
    assertTrue(activeSubscriber.contains(sub1));
    assertTrue(activeSubscriber.contains(sub2));
}
Also used : ManagedLedgerImpl(org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl) Field(java.lang.reflect.Field) Consumer(org.apache.pulsar.client.api.Consumer) Producer(org.apache.pulsar.client.api.Producer) Message(org.apache.pulsar.client.api.Message) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) Test(org.testng.annotations.Test)

Example 39 with Producer

use of org.apache.pulsar.client.api.Producer in project incubator-pulsar by apache.

the class V1_ProducerConsumerTest method testSendTimeout.

@Test(dataProvider = "batch")
public void testSendTimeout(int batchMessageDelayMs) throws Exception {
    log.info("-- Starting {} test --", methodName);
    ConsumerConfiguration consumerConf = new ConsumerConfiguration();
    consumerConf.setSubscriptionType(SubscriptionType.Exclusive);
    Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic5", "my-subscriber-name", consumerConf);
    ProducerConfiguration producerConf = new ProducerConfiguration();
    if (batchMessageDelayMs != 0) {
        producerConf.setBatchingMaxPublishDelay(2 * batchMessageDelayMs, TimeUnit.MILLISECONDS);
        producerConf.setBatchingMaxMessages(5);
        producerConf.setBatchingEnabled(true);
    }
    producerConf.setSendTimeout(1, TimeUnit.SECONDS);
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic5", producerConf);
    final String message = "my-message";
    // Trigger the send timeout
    stopBroker();
    Future<MessageId> future = producer.sendAsync(message.getBytes());
    try {
        future.get();
        Assert.fail("Send operation should have failed");
    } catch (ExecutionException e) {
    // Expected
    }
    startBroker();
    // We should not have received any message
    Message msg = consumer.receive(3, TimeUnit.SECONDS);
    Assert.assertNull(msg);
    consumer.close();
    log.info("-- Exiting {} test --", methodName);
}
Also used : Consumer(org.apache.pulsar.client.api.Consumer) Producer(org.apache.pulsar.client.api.Producer) Message(org.apache.pulsar.client.api.Message) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) ProducerConfiguration(org.apache.pulsar.client.api.ProducerConfiguration) ExecutionException(java.util.concurrent.ExecutionException) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test)

Example 40 with Producer

use of org.apache.pulsar.client.api.Producer in project incubator-pulsar by apache.

the class V1_ProducerConsumerTest method testInvalidSequence.

@Test
public void testInvalidSequence() throws Exception {
    log.info("-- Starting {} test --", methodName);
    PulsarClient client1 = PulsarClient.create("http://127.0.0.1:" + BROKER_WEBSERVICE_PORT);
    client1.close();
    ConsumerConfiguration consumerConf = new ConsumerConfiguration();
    consumerConf.setSubscriptionType(SubscriptionType.Exclusive);
    try {
        client1.subscribe("persistent://my-property/use/my-ns/my-topic6", "my-subscriber-name", consumerConf);
        Assert.fail("Should fail");
    } catch (PulsarClientException e) {
        Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
    }
    try {
        client1.createProducer("persistent://my-property/use/my-ns/my-topic6");
        Assert.fail("Should fail");
    } catch (PulsarClientException e) {
        Assert.assertTrue(e instanceof PulsarClientException.AlreadyClosedException);
    }
    Consumer consumer = pulsarClient.subscribe("persistent://my-property/use/my-ns/my-topic6", "my-subscriber-name", consumerConf);
    try {
        Message msg = MessageBuilder.create().setContent("InvalidMessage".getBytes()).build();
        consumer.acknowledge(msg);
    } catch (PulsarClientException.InvalidMessageException e) {
    // ok
    }
    consumer.close();
    try {
        consumer.receive();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    try {
        consumer.unsubscribe();
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic6");
    producer.close();
    try {
        producer.send("message".getBytes());
        Assert.fail("Should fail");
    } catch (PulsarClientException.AlreadyClosedException e) {
    // ok
    }
}
Also used : Consumer(org.apache.pulsar.client.api.Consumer) Message(org.apache.pulsar.client.api.Message) Producer(org.apache.pulsar.client.api.Producer) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

Producer (org.apache.pulsar.client.api.Producer)56 Test (org.testng.annotations.Test)47 Message (org.apache.pulsar.client.api.Message)39 Consumer (org.apache.pulsar.client.api.Consumer)36 ProducerConfiguration (org.apache.pulsar.client.api.ProducerConfiguration)32 ConsumerConfiguration (org.apache.pulsar.client.api.ConsumerConfiguration)29 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)20 IOException (java.io.IOException)18 ExecutionException (java.util.concurrent.ExecutionException)17 CompletableFuture (java.util.concurrent.CompletableFuture)16 MessageId (org.apache.pulsar.client.api.MessageId)16 Map (java.util.Map)14 PulsarClient (org.apache.pulsar.client.api.PulsarClient)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 List (java.util.List)10 Future (java.util.concurrent.Future)10 TimeUnit (java.util.concurrent.TimeUnit)10 Lists (com.google.common.collect.Lists)9 ExecutorService (java.util.concurrent.ExecutorService)9 ConsumerImpl (org.apache.pulsar.client.impl.ConsumerImpl)9