Java Code Examples for org.apache.pulsar.client.api.PulsarClientException

The following examples show how to use org.apache.pulsar.client.api.PulsarClientException. These examples are extracted from open source projects. 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 Project: hazelcast-jet-contrib   Author: hazelcast   File: PulsarConsumerBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Receive the messages as a batch. The {@link BatchReceivePolicy} is
 * configured while creating the Pulsar {@link Consumer}.
 * In this method, emitted items are created by applying the projection function
 * to the messages received from Pulsar client. If there is an event time
 * associated with the message, it sets the event time as the timestamp of the
 * emitted item. Otherwise, it sets the publish time(which always exists)
 * of the message as the timestamp.
 */
private void fillBuffer(SourceBuilder.TimestampedSourceBuffer<T> sourceBuffer) throws PulsarClientException {
    Messages<M> messages = consumer.batchReceive();
    for (Message<M> message : messages) {
        if (message.getEventTime() != 0) {
            sourceBuffer.add(projectionFn.apply(message), message.getEventTime());
        } else {
            sourceBuffer.add(projectionFn.apply(message), message.getPublishTime());
        }
    }
    consumer.acknowledgeAsync(messages)
            .exceptionally(t -> {
                logger.warning(buildLogMessage(messages));
                return null;
            });
}
 
Example #2
Source Project: pulsar   Author: apache   File: MultiTopicsConsumerImpl.java    License: Apache License 2.0 6 votes vote down vote up
private void failPendingReceive() {
    lock.readLock().lock();
    try {
        if (listenerExecutor != null && !listenerExecutor.isShutdown()) {
            while (!pendingReceives.isEmpty()) {
                CompletableFuture<Message<T>> receiveFuture = pendingReceives.poll();
                if (receiveFuture != null) {
                    receiveFuture.completeExceptionally(
                            new PulsarClientException.AlreadyClosedException("Consumer is already closed"));
                } else {
                    break;
                }
            }
        }
    } finally {
        lock.readLock().unlock();
    }
}
 
Example #3
Source Project: pulsar   Author: apache   File: ProducerImpl.java    License: Apache License 2.0 6 votes vote down vote up
private void batchMessageAndSend() {
    if (log.isTraceEnabled()) {
        log.trace("[{}] [{}] Batching the messages from the batch container with {} messages", topic, producerName,
                batchMessageContainer.getNumMessagesInBatch());
    }
    if (!batchMessageContainer.isEmpty()) {
        try {
            List<OpSendMsg> opSendMsgs;
            if (batchMessageContainer.isMultiBatches()) {
                opSendMsgs = batchMessageContainer.createOpSendMsgs();
            } else {
                opSendMsgs = Collections.singletonList(batchMessageContainer.createOpSendMsg());
            }
            batchMessageContainer.clear();
            for (OpSendMsg opSendMsg : opSendMsgs) {
                processOpSendMsg(opSendMsg);
            }
        } catch (PulsarClientException e) {
            Thread.currentThread().interrupt();
            semaphore.release(batchMessageContainer.getNumMessagesInBatch());
        } catch (Throwable t) {
            semaphore.release(batchMessageContainer.getNumMessagesInBatch());
            log.warn("[{}] [{}] error while create opSendMsg by batch message container", topic, producerName, t);
        }
    }
}
 
Example #4
Source Project: hazelcast-jet-contrib   Author: hazelcast   File: PulsarSinkTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPulsarSink() throws PulsarClientException {
    String topicName = randomName();
    Sink<Integer> pulsarSink = setupSink(topicName); // Its projection function -> Integer::doubleValue

    Pipeline p = Pipeline.create();
    List<Integer> numbers = IntStream.range(0, ITEM_COUNT).boxed().collect(Collectors.toList());
    p.readFrom(TestSources.items(numbers))
     .writeTo(pulsarSink);

    createJetMember().newJob(p).join();
    List<Double> list = consumeMessages(topicName, ITEM_COUNT);

    assertTrueEventually(() -> {
        Assert.assertEquals(ITEM_COUNT, list.size());
        for (double i = 0; i < ITEM_COUNT; i++) {
            assertTrue("missing entry: " + i, list.contains(i));
        }
    }, 10);
}
 
Example #5
Source Project: pulsar   Author: apache   File: PulsarBolt.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes" })
@Override
public void prepare(Map conf, TopologyContext context, OutputCollector collector) {
    this.componentId = context.getThisComponentId();
    this.boltId = String.format("%s-%s", componentId, context.getThisTaskId());
    this.collector = collector;
    try {
        sharedPulsarClient = SharedPulsarClient.get(componentId, clientConf);
        producer = sharedPulsarClient.getSharedProducer(producerConf);
        LOG.info("[{}] Created a pulsar producer on topic {} to send messages", boltId, pulsarBoltConf.getTopic());
    } catch (PulsarClientException e) {
        LOG.error("[{}] Error initializing pulsar producer on topic {}", boltId, pulsarBoltConf.getTopic(), e);
        throw new IllegalStateException(
                format("Failed to initialize producer for %s : %s", pulsarBoltConf.getTopic(), e.getMessage()), e);
    }
    context.registerMetric(String.format("PulsarBoltMetrics-%s-%s", componentId, context.getThisTaskIndex()), this,
            pulsarBoltConf.getMetricsTimeIntervalInSecs());
}
 
Example #6
Source Project: pulsar   Author: apache   File: PulsarKafkaProducer.java    License: Apache License 2.0 6 votes vote down vote up
private org.apache.pulsar.client.api.Producer<byte[]> createNewProducer(String topic) {
    try {
        pulsarProducerBuilder.messageRoutingMode(MessageRoutingMode.CustomPartition);
        pulsarProducerBuilder.messageRouter(new MessageRouter() {
            private static final long serialVersionUID = 1L;

            @Override
            public int choosePartition(Message<?> msg, TopicMetadata metadata) {
                // https://kafka.apache.org/08/documentation.html#producerapi
                // The default partitioner is based on the hash of the key.
                return partitioner.partition(msg.getKey(), metadata.numPartitions());
            }
        });
        log.info("Creating producer for topic {} with config {}", topic, pulsarProducerBuilder.toString());
        return pulsarProducerBuilder.clone().topic(topic).create();
    } catch (PulsarClientException e) {
        throw new RuntimeException(e);
    }
}
 
Example #7
Source Project: pulsar   Author: apache   File: ZeroQueueConsumerImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
void receiveIndividualMessagesFromBatch(MessageMetadata msgMetadata, int redeliveryCount,
        List<Long> ackSet,
        ByteBuf uncompressedPayload, MessageIdData messageId, ClientCnx cnx) {
    log.warn(
            "Closing consumer [{}]-[{}] due to unsupported received batch-message with zero receiver queue size",
            subscription, consumerName);
    // close connection
    closeAsync().handle((ok, e) -> {
        // notify callback with failure result
        notifyPendingReceivedCallback(null,
                new PulsarClientException.InvalidMessageException(
                        format("Unsupported Batch message with 0 size receiver queue for [%s]-[%s] ",
                                subscription, consumerName)));
        return null;
    });
}
 
Example #8
Source Project: pulsar   Author: apache   File: ConnectionPool.java    License: Apache License 2.0 6 votes vote down vote up
public ConnectionPool(ClientConfigurationData conf, EventLoopGroup eventLoopGroup,
        Supplier<ClientCnx> clientCnxSupplier) throws PulsarClientException {
    this.eventLoopGroup = eventLoopGroup;
    this.clientConfig = conf;
    this.maxConnectionsPerHosts = conf.getConnectionsPerBroker();

    pool = new ConcurrentHashMap<>();
    bootstrap = new Bootstrap();
    bootstrap.group(eventLoopGroup);
    bootstrap.channel(EventLoopUtil.getClientSocketChannelClass(eventLoopGroup));

    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, conf.getConnectionTimeoutMs());
    bootstrap.option(ChannelOption.TCP_NODELAY, conf.isUseTcpNoDelay());
    bootstrap.option(ChannelOption.ALLOCATOR, PulsarByteBufAllocator.DEFAULT);

    try {
        channelInitializerHandler = new PulsarChannelInitializer(conf, clientCnxSupplier);
        bootstrap.handler(channelInitializerHandler);
    } catch (Exception e) {
        log.error("Failed to create channel initializer");
        throw new PulsarClientException(e);
    }

    this.dnsResolver = new DnsNameResolverBuilder(eventLoopGroup.next()).traceEnabled(true)
            .channelType(EventLoopUtil.getDatagramChannelClass(eventLoopGroup)).build();
}
 
Example #9
Source Project: datacollector   Author: streamsets   File: TestPulsarMessageConsumerImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expected = StageException.class)
public void testTakeStageExceptionPulsarMessageConverterConvert() throws StageException {
  createPulsarMessageConsumerImplNoIssues();

  List<Stage.ConfigIssue> issues = pulsarMessageConsumerImplMock.init(contextMock);
  Assert.assertEquals(0, issues.size());

  try {
    Mockito.when(messageConsumerMock.receive(Mockito.anyInt(), Mockito.any()))
           .thenReturn(TestUtilsPulsar.getPulsarMessage());
    Mockito.when(pulsarMessageConverterMock.convert(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any()))
           .thenThrow(new StageException(PulsarErrors.PULSAR_09, "messageId", "param1", "param2"));
  } catch (PulsarClientException e) {
    Assert.fail("Error mocking Consumer.receive method in testTakeStageExceptionPulsarMessageConverterConvert");
  }
  pulsarMessageConsumerImplMock.take(Mockito.mock(BatchMaker.class), contextMock, 1);
  Assert.fail();
}
 
Example #10
Source Project: pulsar   Author: apache   File: BrokerServiceAutoSubscriptionCreationTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoSubscriptionCreationDisable() throws Exception {
    pulsar.getConfiguration().setAllowAutoSubscriptionCreation(false);

    final String topicName = "persistent://prop/ns-abc/test-subtopic";
    final String subscriptionName = "test-subtopic-sub";

    admin.topics().createNonPartitionedTopic(topicName);

    try {
        pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).subscribe();
        fail("Subscribe operation should have failed");
    } catch (Exception e) {
        assertTrue(e instanceof PulsarClientException);
    }
    assertFalse(admin.topics().getSubscriptions(topicName).contains(subscriptionName));
}
 
Example #11
Source Project: pulsar   Author: apache   File: TypedMessageBuilderPublish.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Void process(String input, Context context) {
    String publishTopic = (String) context.getUserConfigValueOrDefault("publish-topic", "publishtopic");
    String output = String.format("%s!", input);

    Map<String, String> properties = new HashMap<>();
    properties.put("input_topic", context.getCurrentRecord().getTopicName().get());
    properties.putAll(context.getCurrentRecord().getProperties());

    try {
        TypedMessageBuilder messageBuilder = context.newOutputMessage(publishTopic, Schema.STRING).
                value(output).properties(properties);
        if (context.getCurrentRecord().getKey().isPresent()){
            messageBuilder.key(context.getCurrentRecord().getKey().get());
        }
        messageBuilder.eventTime(System.currentTimeMillis()).sendAsync();
    } catch (PulsarClientException e) {
        context.getLogger().error(e.toString());
    }
    return null;
}
 
Example #12
Source Project: pulsar   Author: apache   File: ConsumerConnector.java    License: Apache License 2.0 6 votes vote down vote up
public <K, V> Map<String, List<PulsarKafkaStream<K, V>>> createMessageStreamsByFilter(TopicFilter topicFilter,
        Map<String, Integer> topicCountMap, Decoder<K> keyDecoder, Decoder<V> valueDecoder) {

    Map<String, List<PulsarKafkaStream<K, V>>> streams = Maps.newHashMap();

    topicCountMap.forEach((topic, count) -> {
        try {
            Consumer<byte[]> consumer = consumerBuilder.topic(topic).subscribe();
            resetOffsets(consumer, strategy);
            log.info("Creating stream for {}-{} with config {}", topic, groupId, consumerBuilder.toString());
            for (int i = 0; i < count; i++) {
                PulsarKafkaStream<K, V> stream = new PulsarKafkaStream<>(keyDecoder, valueDecoder, consumer,
                        isAutoCommit, clientId);
                // if multiple thread-count present then client expects multiple streams reading from the same
                // topic. so, create multiple stream using the same consumer
                streams.computeIfAbsent(topic, key -> Lists.newArrayList()).add(stream);
                topicStreams.add(stream);
            }
        } catch (PulsarClientException e) {
            log.error("Failed to subscribe on topic {} with group-id {}, {}", topic, groupId, e.getMessage(), e);
            throw new RuntimeException("Failed to subscribe on topic " + topic, e);
        }
    });
    return streams;
}
 
Example #13
Source Project: pulsar   Author: apache   File: ConsumerBase.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void reconsumeLater(Message<?> message, long delayTime, TimeUnit unit) throws PulsarClientException {
    if (!conf.isRetryEnable()) {
        throw new PulsarClientException("reconsumeLater method not support!");
    }
    try {
        reconsumeLaterAsync(message, delayTime, unit).get();
    } catch (Exception e) {
        Throwable t = e.getCause();
        if (t instanceof PulsarClientException) {
            throw (PulsarClientException) t;
        } else {
            throw new PulsarClientException(t);
        }
    }
}
 
Example #14
Source Project: pulsar   Author: apache   File: MessageIdTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 10000)
public void producerSend() throws PulsarClientException {
    // 1. Basic Config
    String key = "producerSend";
    final String topicName = "persistent://prop/cluster/namespace/topic-" + key;
    final String subscriptionName = "my-subscription-" + key;
    final String messagePredicate = "my-message-" + key + "-";
    final int numberOfMessages = 30;

    // 2. Create Producer
    Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();

    // 3. Create Consumer
    Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName)
            .subscribe();

    // 4. Publish message and get message id
    Set<MessageId> messageIds = new HashSet<>();
    for (int i = 0; i < numberOfMessages; i++) {
        String message = messagePredicate + i;
        messageIds.add(producer.send(message.getBytes()));
    }

    // 4. Check if message Ids are correct
    log.info("Message IDs = " + messageIds);
    Assert.assertEquals(messageIds.size(), numberOfMessages, "Not all messages published successfully");

    for (int i = 0; i < numberOfMessages; i++) {
        Assert.assertTrue(messageIds.remove(consumer.receive().getMessageId()), "Failed to receive Message");
    }
    log.info("Message IDs = " + messageIds);
    Assert.assertEquals(messageIds.size(), 0, "Not all messages received successfully");
    consumer.unsubscribe();
    ;
}
 
Example #15
Source Project: pulsar   Author: apache   File: ConsumerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected CompletableFuture<Void> doAcknowledge(MessageId messageId, AckType ackType,
                                                Map<String,Long> properties,
                                                TransactionImpl txnImpl) {
    checkArgument(messageId instanceof MessageIdImpl);
    if (getState() != State.Ready && getState() != State.Connecting) {
        stats.incrementNumAcksFailed();
        PulsarClientException exception = new PulsarClientException("Consumer not ready. State: " + getState());
        if (AckType.Individual.equals(ackType)) {
            onAcknowledge(messageId, exception);
        } else if (AckType.Cumulative.equals(ackType)) {
            onAcknowledgeCumulative(messageId, exception);
        }
        return FutureUtil.failedFuture(exception);
    }

    if (messageId instanceof BatchMessageIdImpl) {
        if (markAckForBatchMessage((BatchMessageIdImpl) messageId, ackType, properties)) {
            // all messages in batch have been acked so broker can be acked via sendAcknowledge()
            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] acknowledging message - {}, acktype {}", subscription, consumerName, messageId,
                        ackType);
            }
        } else {
            BatchMessageIdImpl batchMessageId = (BatchMessageIdImpl) messageId;
            acknowledgmentsGroupingTracker.addBatchIndexAcknowledgment(batchMessageId, batchMessageId.getBatchIndex(),
                batchMessageId.getBatchSize(), ackType, properties);
            // other messages in batch are still pending ack.
            return CompletableFuture.completedFuture(null);
        }
    }
    return sendAcknowledge(messageId, ackType, properties, txnImpl);
}
 
Example #16
Source Project: pulsar   Author: apache   File: SmokeTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void checkMessages() throws PulsarClientException {

    @Cleanup
    PulsarClient client = PulsarClient.builder()
            .serviceUrl(pulsarContainer.getPlainTextPulsarBrokerUrl())
            .build();

    final String inputTopic = "input";

    Producer<String> producer = client.newProducer(Schema.STRING)
            .topic(inputTopic)
            .enableBatching(false)
            .create();

    Consumer<String> consumer = client.newConsumer(Schema.STRING)
            .topic(inputTopic)
            .subscriptionName("test-subs")
            .ackTimeout(10, TimeUnit.SECONDS)
            .subscriptionType(SubscriptionType.Exclusive)
            .subscribe();

    producer.send("Hello!");
    Message<String> message = consumer.receive(10, TimeUnit.SECONDS);

    Assert.assertEquals(message.getValue(), "Hello!");

}
 
Example #17
Source Project: hazelcast-jet-contrib   Author: hazelcast   File: PulsarTestSupport.java    License: Apache License 2.0 5 votes vote down vote up
protected static List<Double> consumeMessages(String topicName, int count)
        throws PulsarClientException {
    List<Double> list = new LinkedList<>();
    for (int i = 0; i < count; i++) {
        list.add(consumeMessage(topicName).getValue());
    }
    return list;
}
 
Example #18
Source Project: datacollector   Author: streamsets   File: TestPulsarMessageConsumerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInitWithPulsarClientCreationIssues() {
  createPulsarMessageConsumerImplNoIssues();

  Mockito.when(contextMock.createConfigIssue(Mockito.anyString(),
      Mockito.anyString(),
      Mockito.any(),
      Mockito.any(),
      Mockito.any()
  )).thenReturn(getConfigIssue(PulsarErrors.PULSAR_00));


  try {
    Mockito.when(pulsarClientBuilderMock.build()).thenThrow(new PulsarClientException("pulsar client builder" +
        "build exception"));
  } catch (PulsarClientException e) {
    Assert.fail("Error mocking pulsarClientBuilderMock.build method in testInitWithPulsarClientCreationIssues");
  }

  pulsarMessageConsumerImplMock = new PulsarMessageConsumerImpl(TestUtilsPulsar.getBasicConfig(),
      TestUtilsPulsar.getSourceConfig(),
      pulsarMessageConverterMock
  );

  List<Stage.ConfigIssue> issues = pulsarMessageConsumerImplMock.init(contextMock);
  Assert.assertEquals(1, issues.size());
  Assert.assertTrue(PulsarErrors.PULSAR_00.getCode().equals(issues.get(0).toString()));
}
 
Example #19
Source Project: pulsar   Author: apache   File: PersistentReplicator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void sendComplete(Exception exception) {
    if (exception != null && !(exception instanceof PulsarClientException.InvalidMessageException)) {
        log.error("[{}][{} -> {}] Error producing on remote broker", replicator.topicName,
                replicator.localCluster, replicator.remoteCluster, exception);
        // cursor should be rewinded since it was incremented when readMoreEntries
        replicator.cursor.rewind();
    } else {
        if (log.isDebugEnabled()) {
            log.debug("[{}][{} -> {}] Message persisted on remote broker", replicator.topicName,
                    replicator.localCluster, replicator.remoteCluster);
        }
        replicator.cursor.asyncDelete(entry.getPosition(), replicator, entry.getPosition());
    }
    entry.release();

    int pending = PENDING_MESSAGES_UPDATER.decrementAndGet(replicator);

    // In general, we schedule a new batch read operation when the occupied queue size gets smaller than half
    // the max size, unless another read operation is already in progress.
    // If the producer is not currently writable (disconnected or TCP window full), we want to defer the reads
    // until we have emptied the whole queue, and at that point we will read a batch of 1 single message if the
    // producer is still not "writable".
    if (pending < replicator.producerQueueThreshold //
            && HAVE_PENDING_READ_UPDATER.get(replicator) == FALSE //
    ) {
        if (pending == 0 || replicator.producer.isWritable()) {
            replicator.readMoreEntries();
        } else {
            if (log.isDebugEnabled()) {
                log.debug("[{}][{} -> {}] Not resuming reads. pending: {} is-writable: {}",
                        replicator.topicName, replicator.localCluster, replicator.remoteCluster, pending,
                        replicator.producer.isWritable());
            }
        }
    }

    recycle();
}
 
Example #20
Source Project: pulsar   Author: apache   File: AvroSchemaHandler.java    License: Apache License 2.0 5 votes vote down vote up
AvroSchemaHandler(TopicName topicName,
                  PulsarConnectorConfig pulsarConnectorConfig,
                  SchemaInfo schemaInfo,
                  List<PulsarColumnHandle> columnHandles) throws PulsarClientException {
    this(new PulsarSqlSchemaInfoProvider(topicName,
                            pulsarConnectorConfig.getPulsarAdmin()), schemaInfo, columnHandles);
}
 
Example #21
Source Project: pulsar   Author: apache   File: PulsarSpout.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void close() throws PulsarClientException {
    try {
        reader.close();
    } catch (IOException e) {
        throw new PulsarClientException(e);
    }
}
 
Example #22
Source Project: pulsar   Author: apache   File: ConsumerBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void acknowledgeCumulative(MessageId messageId) throws PulsarClientException {
    try {
        acknowledgeCumulativeAsync(messageId).get();
    } catch (Exception e) {
        throw PulsarClientException.unwrap(e);
    }
}
 
Example #23
Source Project: pulsar-flink   Author: streamnative   File: FlinkPulsarSource.java    License: Apache License 2.0 5 votes vote down vote up
protected PulsarMetadataReader createMetadataReader() throws PulsarClientException {
    return new PulsarMetadataReader(
            adminUrl,
            clientConfigurationData,
            getSubscriptionName(),
            caseInsensitiveParams,
            taskIndex,
            numParallelTasks,
            startupMode == StartupMode.EXTERNAL_SUBSCRIPTION);
}
 
Example #24
Source Project: pulsar   Author: apache   File: ClientCnx.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    super.channelInactive(ctx);
    log.info("{} Disconnected", ctx.channel());
    if (!connectionFuture.isDone()) {
        connectionFuture.completeExceptionally(new PulsarClientException("Connection already closed"));
    }

    PulsarClientException e = new PulsarClientException(
            "Disconnected from server at " + ctx.channel().remoteAddress());

    // Fail out all the pending ops
    pendingRequests.forEach((key, future) -> future.completeExceptionally(e));
    pendingLookupRequests.forEach((key, future) -> future.completeExceptionally(e));
    waitingLookupRequests.forEach(pair -> pair.getRight().getRight().completeExceptionally(e));
    pendingGetLastMessageIdRequests.forEach((key, future) -> future.completeExceptionally(e));
    pendingGetTopicsRequests.forEach((key, future) -> future.completeExceptionally(e));
    pendingGetSchemaRequests.forEach((key, future) -> future.completeExceptionally(e));

    // Notify all attached producers/consumers so they have a chance to reconnect
    producers.forEach((id, producer) -> producer.connectionClosed(this));
    consumers.forEach((id, consumer) -> consumer.connectionClosed(this));
    transactionMetaStoreHandlers.forEach((id, handler) -> handler.connectionClosed(this));

    pendingRequests.clear();
    pendingLookupRequests.clear();
    waitingLookupRequests.clear();
    pendingGetLastMessageIdRequests.clear();
    pendingGetTopicsRequests.clear();

    producers.clear();
    consumers.clear();

    timeoutTask.cancel(true);
}
 
Example #25
Source Project: pulsar-flink   Author: streamnative   File: PulsarTestBase.java    License: Apache License 2.0 5 votes vote down vote up
public static <T> List<MessageId> sendTypedMessages(
        String topic,
        SchemaType type,
        List<T> messages,
        Optional<Integer> partition) throws PulsarClientException {

    return sendTypedMessages(topic, type, messages, partition, null);
}
 
Example #26
Source Project: pulsar-flink   Author: streamnative   File: TestMetadataReader.java    License: Apache License 2.0 5 votes vote down vote up
public TestMetadataReader(
        Map<String, String> caseInsensitiveParams,
        int indexOfThisSubtask,
        int numParallelSubtasks,
        List<Set<String>> mockGetAllTopicsReturnSequence) throws PulsarClientException {
    super("", new ClientConfigurationData(), "", caseInsensitiveParams, indexOfThisSubtask, numParallelSubtasks);
    this.mockGetAllTopicsReturnSequence = mockGetAllTopicsReturnSequence;
}
 
Example #27
Source Project: eventeum   Author: ConsenSys   File: PulsarBlockChainEventBroadcaster.java    License: Apache License 2.0 5 votes vote down vote up
@PreDestroy
public void destroy() {
	if (client != null) {
		try {
			client.close();
		} catch (PulsarClientException e) {
			log.warn("couldn't close Pulsar client", e);
		} finally {
			client = null;
			blockEventProducer = null;
			contractEventProducer = null;
		}
	}
}
 
Example #28
Source Project: pulsar   Author: apache   File: AuthenticationBasic.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public AuthenticationDataProvider getAuthData() throws PulsarClientException {
    try {
        return new AuthenticationDataBasic(userId, password);
    } catch (Exception e) {
        throw PulsarClientException.unwrap(e);
    }
}
 
Example #29
Source Project: pulsar   Author: apache   File: ConsumerImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> unsubscribeAsync() {
    if (getState() == State.Closing || getState() == State.Closed) {
        return FutureUtil
                .failedFuture(new PulsarClientException.AlreadyClosedException("Consumer was already closed"));
    }
    final CompletableFuture<Void> unsubscribeFuture = new CompletableFuture<>();
    if (isConnected()) {
        setState(State.Closing);
        long requestId = client.newRequestId();
        ByteBuf unsubscribe = Commands.newUnsubscribe(consumerId, requestId);
        ClientCnx cnx = cnx();
        cnx.sendRequestWithId(unsubscribe, requestId).thenRun(() -> {
            cnx.removeConsumer(consumerId);
            unAckedMessageTracker.close();
            if (possibleSendToDeadLetterTopicMessages != null) {
                possibleSendToDeadLetterTopicMessages.clear();
            }
            client.cleanupConsumer(ConsumerImpl.this);
            log.info("[{}][{}] Successfully unsubscribed from topic", topic, subscription);
            setState(State.Closed);
            unsubscribeFuture.complete(null);
        }).exceptionally(e -> {
            log.error("[{}][{}] Failed to unsubscribe: {}", topic, subscription, e.getCause().getMessage());
            setState(State.Ready);
            unsubscribeFuture.completeExceptionally(
                PulsarClientException.wrap(e.getCause(),
                    String.format("Failed to unsubscribe the subscription %s of topic %s",
                        topicName.toString(), subscription)));
            return null;
        });
    } else {
        unsubscribeFuture.completeExceptionally(
            new PulsarClientException(
                String.format("The client is not connected to the broker when unsubscribing the " +
                    "subscription %s of the topic %s", subscription, topicName.toString())));
    }
    return unsubscribeFuture;
}
 
Example #30
Source Project: pulsar   Author: apache   File: ReplicatorTestBase.java    License: Apache License 2.0 5 votes vote down vote up
public void close() {
    try {
        client.close();
    } catch (PulsarClientException e) {
        log.warn("Failed to close client", e);
    }
}