use of org.apache.pulsar.client.api.PulsarClientException in project flink by apache.
the class PulsarRuntimeOperator method createConsumer.
@SuppressWarnings("unchecked")
private <T> Consumer<T> createConsumer(String topic, Schema<T> schema) throws PulsarClientException {
TopicName topicName = TopicName.get(topic);
String name = topicName.getPartitionedTopicName();
int index = topicName.getPartitionIndex();
ConcurrentHashMap<Integer, Consumer<?>> topicConsumers = consumers.computeIfAbsent(name, d -> new ConcurrentHashMap<>());
return (Consumer<T>) topicConsumers.computeIfAbsent(index, i -> {
try {
return client().newConsumer(schema).topic(topic).subscriptionName(SUBSCRIPTION_NAME).subscriptionMode(Durable).subscriptionType(Exclusive).subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();
} catch (PulsarClientException e) {
sneakyThrow(e);
return null;
}
});
}
use of org.apache.pulsar.client.api.PulsarClientException in project flink by apache.
the class PulsarPartitionSplitReaderTestBase method seekStartPositionAndHandleSplit.
private void seekStartPositionAndHandleSplit(PulsarPartitionSplitReaderBase<String> reader, String topicName, int partitionId, MessageId startPosition) {
TopicPartition partition = new TopicPartition(topicName, partitionId, createFullRange());
PulsarPartitionSplit split = new PulsarPartitionSplit(partition, StopCursor.never(), null, null);
SplitsAddition<PulsarPartitionSplit> addition = new SplitsAddition<>(singletonList(split));
// create consumer and seek before split changes
try (Consumer<byte[]> consumer = reader.createPulsarConsumer(partition)) {
// inclusive messageId
StartCursor startCursor = StartCursor.fromMessageId(startPosition);
startCursor.seekPosition(partition.getTopic(), partition.getPartitionId(), consumer);
} catch (PulsarClientException e) {
sneakyThrow(e);
}
reader.handleSplitsChanges(addition);
}
use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.
the class ContextImpl method publish.
@Override
public <O> CompletableFuture<Void> publish(String topicName, O object, String serDeClassName) {
if (!publishProducers.containsKey(topicName)) {
try {
publishProducers.put(topicName, pulsarClient.createProducer(topicName, producerConfiguration));
} catch (PulsarClientException ex) {
CompletableFuture<Void> retval = new CompletableFuture<>();
retval.completeExceptionally(ex);
return retval;
}
}
if (!publishSerializers.containsKey(serDeClassName)) {
SerDe serDe;
if (serDeClassName.equals(DefaultSerDe.class.getName())) {
if (!DefaultSerDe.IsSupportedType(object.getClass())) {
throw new RuntimeException("Default Serializer does not support " + object.getClass());
}
serDe = new DefaultSerDe(object.getClass());
} else {
try {
Class<? extends SerDe> serDeClass = (Class<? extends SerDe>) Class.forName(serDeClassName);
serDe = Reflections.createInstance(serDeClassName, serDeClass, classLoader);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
publishSerializers.put(serDeClassName, serDe);
}
byte[] bytes = publishSerializers.get(serDeClassName).serialize(object);
return publishProducers.get(topicName).sendAsync(bytes).thenApply(msgId -> null);
}
use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.
the class ConsumerImpl method unsubscribeAsync.
@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);
log.info("[{}][{}] Successfully unsubscribed from topic", topic, subscription);
unAckedMessageTracker.close();
unsubscribeFuture.complete(null);
setState(State.Closed);
}).exceptionally(e -> {
log.error("[{}][{}] Failed to unsubscribe: {}", topic, subscription, e.getCause().getMessage());
unsubscribeFuture.completeExceptionally(e.getCause());
setState(State.Ready);
return null;
});
} else {
unsubscribeFuture.completeExceptionally(new PulsarClientException("Not connected to broker"));
}
return unsubscribeFuture;
}
use of org.apache.pulsar.client.api.PulsarClientException in project incubator-pulsar by apache.
the class ConsumerImpl method fetchSingleMessageFromBroker.
private Message<T> fetchSingleMessageFromBroker() throws PulsarClientException {
checkArgument(conf.getReceiverQueueSize() == 0);
// Just being cautious
if (incomingMessages.size() > 0) {
log.error("The incoming message queue should never be greater than 0 when Queue size is 0");
incomingMessages.clear();
}
Message<T> message;
try {
// is cnx is null or if the connection breaks the connectionOpened function will send the flow again
waitingOnReceiveForZeroQueueSize = true;
synchronized (this) {
if (isConnected()) {
sendFlowPermitsToBroker(cnx(), 1);
}
}
do {
message = incomingMessages.take();
lastDequeuedMessage = message.getMessageId();
ClientCnx msgCnx = ((MessageImpl<?>) message).getCnx();
// synchronized need to prevent race between connectionOpened and the check "msgCnx == cnx()"
synchronized (ConsumerImpl.this) {
// latest flow command
if (msgCnx == cnx()) {
waitingOnReceiveForZeroQueueSize = false;
break;
}
}
} while (true);
stats.updateNumMsgsReceived(message);
return message;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
stats.incrementNumReceiveFailed();
throw new PulsarClientException(e);
} finally {
// Finally blocked is invoked in case the block on incomingMessages is interrupted
waitingOnReceiveForZeroQueueSize = false;
// Clearing the queue in case there was a race with messageReceived
incomingMessages.clear();
}
}
Aggregations