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