Java Code Examples for org.apache.pulsar.client.api.PulsarClientException#AlreadyClosedException
The following examples show how to use
org.apache.pulsar.client.api.PulsarClientException#AlreadyClosedException .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConsumerBase.java From pulsar with Apache License 2.0 | 6 votes |
private void verifyConsumerState() throws PulsarClientException { switch (getState()) { case Ready: case Connecting: break; // Ok case Closing: case Closed: throw new PulsarClientException.AlreadyClosedException("Consumer already closed"); case Terminated: throw new PulsarClientException.AlreadyClosedException("Topic was terminated"); case Failed: case Uninitialized: throw new PulsarClientException.NotConnectedException(); default: break; } }
Example 2
Source File: PersistentTopicE2ETest.java From pulsar with Apache License 2.0 | 4 votes |
@Test public void testSimpleConsumerEvents() throws Exception { final String topicName = "persistent://prop/ns-abc/topic1"; final String subName = "sub1"; final int numMsgs = 10; // 1. client connect Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName).subscribe(); PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName).get(); PersistentSubscription subRef = topicRef.getSubscription(subName); assertNotNull(topicRef); assertNotNull(subRef); assertTrue(subRef.getDispatcher().isConsumerConnected()); Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertEquals(getAvailablePermits(subRef), 1000 /* default */); Producer<byte[]> producer = pulsarClient.newProducer() .topic(topicName) .enableBatching(false) .messageRoutingMode(MessageRoutingMode.SinglePartition) .create(); for (int i = 0; i < numMsgs * 2; i++) { String message = "my-message-" + i; producer.send(message.getBytes()); } assertTrue(subRef.getDispatcher().isConsumerConnected()); rolloverPerIntervalStats(); assertEquals(subRef.getNumberOfEntriesInBacklog(false), numMsgs * 2); // 2. messages pushed before client receive Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertEquals(getAvailablePermits(subRef), 1000 - numMsgs * 2); Message<byte[]> msg = null; for (int i = 0; i < numMsgs; i++) { msg = consumer.receive(); // 3. in-order message delivery assertEquals(new String(msg.getData()), "my-message-" + i); consumer.acknowledge(msg); } rolloverPerIntervalStats(); // 4. messages deleted on individual acks Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertEquals(subRef.getNumberOfEntriesInBacklog(false), numMsgs); for (int i = 0; i < numMsgs; i++) { msg = consumer.receive(); if (i == numMsgs - 1) { consumer.acknowledgeCumulative(msg); } } rolloverPerIntervalStats(); // 5. messages deleted on cumulative acks Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); assertEquals(subRef.getNumberOfEntriesInBacklog(false), 0); // 6. consumer unsubscribe consumer.unsubscribe(); // 6. consumer graceful close consumer.close(); // 7. consumer unsubscribe try { consumer.unsubscribe(); fail("Should have failed"); } catch (PulsarClientException.AlreadyClosedException e) { // ok } Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); subRef = topicRef.getSubscription(subName); assertNull(subRef); producer.close(); Thread.sleep(ASYNC_EVENT_COMPLETION_WAIT); }