@Override public void logEvent(String event, Map<String, Object> producerConfig) { String output = event; if (flatten) { try { output = jsonUtils.flattenJson(event); } catch (IOException ex) { log.error("Error flattening json. Unable to send event [ " + event + " ]", ex); return; } } if (sync) { try { producer.send(output.getBytes()); } catch (PulsarClientException e) { log.error("Unable to send event to Pulsar", e); } } else { producer.sendAsync(output.getBytes()); } }
@Override public void append(LogEvent logEvent) { producer.sendAsync(logEvent.getMessage().getFormattedMessage().getBytes()); }
public void start() throws Exception { Producer producer = getNewProducer(); final Consumer consumer = consumerFuture.get(); while (!stop.get()) { final MutableBoolean wellnessFlag = new MutableBoolean(); final Function<Throwable, ? extends MessageId> exceptionHandler = e -> { // Unset the well flag in the case of an exception so we can // try to get a new Producer. wellnessFlag.value = false; return null; }; while (!stop.get() && wellnessFlag.value) { producer.sendAsync(payload.get()).exceptionally(exceptionHandler); rateLimiter.acquire(); } producer.closeAsync(); if (!stop.get()) { // The Producer failed due to an exception: attempt to get // another producer. producer = getNewProducer(); } else { // We are finished: close the consumer. consumer.closeAsync(); } } } }
@SuppressWarnings("unchecked") public <O> CompletableFuture<Void> publish(String topicName, O object, Schema<O> schema) { Producer<O> producer = (Producer<O>) publishProducers.get(topicName); if (producer == null) { try { Producer<O> newProducer = ((ProducerBuilderImpl<O>) producerBuilder.clone()) .schema(schema).topic(topicName).create(); Producer<O> existingProducer = (Producer<O>) publishProducers.putIfAbsent(topicName, newProducer); if (existingProducer != null) { // The value in the map was not updated after the concurrent put newProducer.close(); producer = existingProducer; } else { producer = newProducer; } } catch (PulsarClientException e) { logger.error("Failed to create Producer while doing user publish", e); return FutureUtil.failedFuture(e); } } return producer.sendAsync(object).thenApply(msgId -> null); }
producer.sendAsync(payloadData).thenRun(() -> { messagesSent.increment(); bytesSent.add(payloadData.length);
@Override public void invoke(IN value, Context context) throws Exception { checkErroneous(); byte[] serializedValue = schema.serialize(value); MessageBuilder msgBuilder = MessageBuilder.create(); if (null != context.timestamp()) { msgBuilder = msgBuilder.setEventTime(context.timestamp()); } String msgKey = flinkPulsarKeyExtractor.getKey(value); if (null != msgKey) { msgBuilder = msgBuilder.setKey(msgKey); } Message message = msgBuilder .setContent(serializedValue) .build(); if (flushOnCheckpoint) { synchronized (pendingRecordsLock) { pendingRecords++; } } producer.sendAsync(message) .thenApply(successCallback) .exceptionally(failureCallback); }