Search in sources :

Example 46 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ClientErrorsTest method testProducerReconnect.

// Run this test multiple times to reproduce race conditions on reconnection logic
@Test(invocationCount = 100)
public void testProducerReconnect() throws Exception {
    AtomicInteger numOfConnections = new AtomicInteger();
    AtomicReference<ChannelHandlerContext> channelCtx = new AtomicReference<>();
    AtomicBoolean msgSent = new AtomicBoolean();
    mockBrokerService.setHandleConnect((ctx, connect) -> {
        channelCtx.set(ctx);
        ctx.writeAndFlush(Commands.newConnected(connect));
        if (numOfConnections.incrementAndGet() == 2) {
            // close the cnx immediately when trying to conenct the 2nd time
            ctx.channel().close();
        }
    });
    mockBrokerService.setHandleProducer((ctx, produce) -> {
        ctx.writeAndFlush(Commands.newProducerSuccess(produce.getRequestId(), "default-producer"));
    });
    mockBrokerService.setHandleSend((ctx, sendCmd, headersAndPayload) -> {
        msgSent.set(true);
        ctx.writeAndFlush(Commands.newSendReceipt(0, 0, 1, 1));
    });
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
    Producer producer = client.createProducer("persistent://prop/use/ns/t1");
    // close the cnx after creating the producer
    channelCtx.get().channel().close().get();
    producer.send(new byte[0]);
    assertEquals(msgSent.get(), true);
    assertTrue(numOfConnections.get() >= 3);
    mockBrokerService.resetHandleConnect();
    mockBrokerService.resetHandleProducer();
    mockBrokerService.resetHandleSend();
    client.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Producer(com.yahoo.pulsar.client.api.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Example 47 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ClientErrorsTest method subscribeFailDoesNotFailOtherConsumer.

private void subscribeFailDoesNotFailOtherConsumer(String topic1, String topic2) throws Exception {
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
    final AtomicInteger counter = new AtomicInteger(0);
    mockBrokerService.setHandleSubscribe((ctx, subscribe) -> {
        if (counter.incrementAndGet() == 2) {
            // fail second producer
            ctx.writeAndFlush(Commands.newError(subscribe.getRequestId(), ServerError.AuthenticationError, "msg"));
            return;
        }
        ctx.writeAndFlush(Commands.newSuccess(subscribe.getRequestId()));
    });
    ConsumerConfiguration conf = new ConsumerConfiguration();
    conf.setSubscriptionType(SubscriptionType.Exclusive);
    ConsumerBase consumer1 = (ConsumerBase) client.subscribe(topic1, "sub1", conf);
    ConsumerBase consumer2 = null;
    try {
        consumer2 = (ConsumerBase) client.subscribe(topic2, "sub1", conf);
        fail("Should have failed");
    } catch (Exception e) {
    // ok
    }
    assertTrue(consumer1.isConnected());
    assertFalse(consumer2 != null && consumer2.isConnected());
    mockBrokerService.resetHandleSubscribe();
    client.close();
}
Also used : ConsumerBase(com.yahoo.pulsar.client.impl.ConsumerBase) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfiguration(com.yahoo.pulsar.client.api.ConsumerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) LookupException(com.yahoo.pulsar.client.api.PulsarClientException.LookupException) ExecutionException(java.util.concurrent.ExecutionException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException)

Example 48 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ContinuousAsyncProducer method main.

public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
    PulsarClient pulsarClient = PulsarClient.create("http://127.0.0.1:8080");
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic");
    while (true) {
        producer.sendAsync("my-message".getBytes());
    }
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient)

Example 49 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class ContinuousProducer method main.

public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
    PulsarClient pulsarClient = PulsarClient.create("http://127.0.0.1:8080");
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic");
    while (true) {
        try {
            producer.send("my-message".getBytes());
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
            break;
        }
    }
    pulsarClient.close();
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) IOException(java.io.IOException)

Example 50 with PulsarClient

use of com.yahoo.pulsar.client.api.PulsarClient in project pulsar by yahoo.

the class SampleAsyncProducer method main.

public static void main(String[] args) throws PulsarClientException, InterruptedException, IOException {
    PulsarClient pulsarClient = PulsarClient.create("http://127.0.0.1:8080");
    ProducerConfiguration conf = new ProducerConfiguration();
    conf.setSendTimeout(3, TimeUnit.SECONDS);
    Producer producer = pulsarClient.createProducer("persistent://my-property/use/my-ns/my-topic", conf);
    List<CompletableFuture<MessageId>> futures = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        final String content = "my-message-" + i;
        CompletableFuture<MessageId> future = producer.sendAsync(content.getBytes());
        future.handle((v, ex) -> {
            if (ex == null) {
                log.info("Message persisted: {}", content);
            } else {
                log.error("Error persisting message: {}", content, ex);
            }
            return null;
        });
        futures.add(future);
    }
    log.info("Waiting for async ops to complete");
    for (CompletableFuture<MessageId> future : futures) {
        future.join();
    }
    log.info("All operations completed");
    pulsarClient.close();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Producer(com.yahoo.pulsar.client.api.Producer) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) MessageId(com.yahoo.pulsar.client.api.MessageId)

Aggregations

PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)60 Test (org.testng.annotations.Test)39 Consumer (com.yahoo.pulsar.client.api.Consumer)32 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)32 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)28 Producer (com.yahoo.pulsar.client.api.Producer)26 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 LookupException (com.yahoo.pulsar.client.api.PulsarClientException.LookupException)13 ExecutionException (java.util.concurrent.ExecutionException)13 ProducerConfiguration (com.yahoo.pulsar.client.api.ProducerConfiguration)10 PersistentTopicStats (com.yahoo.pulsar.common.policies.data.PersistentTopicStats)10 Message (com.yahoo.pulsar.client.api.Message)9 BacklogQuota (com.yahoo.pulsar.common.policies.data.BacklogQuota)9 IOException (java.io.IOException)8 URI (java.net.URI)6 URL (java.net.URL)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)5