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();
}
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();
}
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();
}
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();
}
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());
}
}
Aggregations