Search in sources :

Example 51 with PulsarClient

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

the class ClientErrorsTest method subscribeSuccessAfterRetry.

private void subscribeSuccessAfterRetry(String topic) 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) {
            ctx.writeAndFlush(Commands.newSuccess(subscribe.getRequestId()));
            return;
        }
        ctx.writeAndFlush(Commands.newError(subscribe.getRequestId(), ServerError.ServiceNotReady, "msg"));
    });
    try {
        ConsumerConfiguration conf = new ConsumerConfiguration();
        conf.setSubscriptionType(SubscriptionType.Exclusive);
        Consumer consumer = client.subscribe(topic, "sub1", conf);
    } catch (Exception e) {
        fail("Should not fail");
    }
    mockBrokerService.resetHandleSubscribe();
    client.close();
}
Also used : Consumer(com.yahoo.pulsar.client.api.Consumer) 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 52 with PulsarClient

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

the class ClientErrorsTest method producerContinuousRetryAfterSendFail.

private void producerContinuousRetryAfterSendFail(String topic) throws Exception {
    PulsarClient client = PulsarClient.create("http://127.0.0.1:" + WEB_SERVICE_PORT);
    final AtomicInteger counter = new AtomicInteger(0);
    mockBrokerService.setHandleProducer((ctx, producer) -> {
        int i = counter.incrementAndGet();
        if (i == 1 || i == 5) {
            // succeed on 1st and 5th attempts
            ctx.writeAndFlush(Commands.newProducerSuccess(producer.getRequestId(), "default-producer"));
            return;
        }
        ctx.writeAndFlush(Commands.newError(producer.getRequestId(), ServerError.PersistenceError, "msg"));
    });
    final AtomicInteger msgCounter = new AtomicInteger(0);
    mockBrokerService.setHandleSend((ctx, send, headersAndPayload) -> {
        // fail send once, but succeed later
        if (msgCounter.incrementAndGet() == 1) {
            ctx.writeAndFlush(Commands.newSendError(0, 0, new IllegalStateException("Send Failed")));
            return;
        }
        ctx.writeAndFlush(Commands.newSendReceipt(0, 0, 1, 1));
    });
    try {
        Producer producer = client.createProducer(topic);
        producer.send("message".getBytes());
    } catch (Exception e) {
        fail("Should not fail");
    }
    mockBrokerService.resetHandleProducer();
    mockBrokerService.resetHandleSend();
    client.close();
}
Also used : Producer(com.yahoo.pulsar.client.api.Producer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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 53 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 54 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 55 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)

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