/** * This is called when client sends message to pulsar broker, before key and value gets serialized. * The method calls {@link ProducerInterceptor#beforeSend(Producer,Message)} method. Message returned from * first interceptor's beforeSend() is passed to the second interceptor beforeSend(), and so on in the * interceptor chain. The message returned from the last interceptor is returned from this method. * * This method does not throw exceptions. Exceptions thrown by any interceptor methods are caught and ignored. * If a interceptor in the middle of the chain, that normally modifies the message, throws an exception, * the next interceptor in the chain will be called with a message returned by the previous interceptor that did * not throw an exception. * * @param producer the producer which contains the interceptor. * @param message the message from client * @return the message to send to topic/partition */ public Message<T> beforeSend(Producer<T> producer, Message<T> message) { Message<T> interceptorMessage = message; for (int i = 0; i < interceptors.size(); i++) { try { interceptorMessage = interceptors.get(i).beforeSend(producer, interceptorMessage); } catch (Exception e) { if (message != null && producer != null) { log.warn("Error executing interceptor beforeSend callback for messageId: {}, topicName:{} ", message.getMessageId(), producer.getTopic(), e); } else { log.warn("Error Error executing interceptor beforeSend callback ", e); } } } return interceptorMessage; }
log.error("Send to dead letter topic exception with topic: {}, messageId: {}", deadLetterProducer.getTopic(), messageId, e);
@Override public void writeFailed(Throwable th) { log.warn("[{}] Failed to send ack {}", producer.getTopic(), th.getMessage(), th); }
@Override public void close() throws IOException { if (producer != null) { if (!this.service.removeProducer(this)) { log.warn("[{}] Failed to remove producer handler", producer.getTopic()); } producer.closeAsync().thenAccept(x -> { if (log.isDebugEnabled()) { log.debug("[{}] Closed producer asynchronously", producer.getTopic()); } }).exceptionally(exception -> { log.warn("[{}] Failed to close producer", producer.getTopic(), exception); return null; }); } }
@Override public void writeSuccess() { if (log.isDebugEnabled()) { log.debug("[{}] Ack was sent successfully to {}", producer.getTopic(), getRemote().getInetSocketAddress().toString()); } } });
private void sendAckResponse(ProducerAck response) { try { String msg = ObjectMapperFactory.getThreadLocal().writeValueAsString(response); getSession().getRemote().sendString(msg, new WriteCallback() { @Override public void writeFailed(Throwable th) { log.warn("[{}] Failed to send ack {}", producer.getTopic(), th.getMessage(), th); } @Override public void writeSuccess() { if (log.isDebugEnabled()) { log.debug("[{}] Ack was sent successfully to {}", producer.getTopic(), getRemote().getInetSocketAddress().toString()); } } }); } catch (JsonProcessingException e) { log.warn("[{}] Failed to generate ack json-response {}", producer.getTopic(), e.getMessage(), e); } catch (Exception e) { log.warn("[{}] Failed to send ack {}", producer.getTopic(), e.getMessage(), e); } }
/** * This is called when client sends message to pulsar broker, before key and value gets serialized. * The method calls {@link ProducerInterceptor#beforeSend(Producer,Message)} method. Message returned from * first interceptor's beforeSend() is passed to the second interceptor beforeSend(), and so on in the * interceptor chain. The message returned from the last interceptor is returned from this method. * * This method does not throw exceptions. Exceptions thrown by any interceptor methods are caught and ignored. * If a interceptor in the middle of the chain, that normally modifies the message, throws an exception, * the next interceptor in the chain will be called with a message returned by the previous interceptor that did * not throw an exception. * * @param producer the producer which contains the interceptor. * @param message the message from client * @return the message to send to topic/partition */ public Message<T> beforeSend(Producer<T> producer, Message<T> message) { Message<T> interceptorMessage = message; for (int i = 0; i < interceptors.size(); i++) { try { interceptorMessage = interceptors.get(i).beforeSend(producer, interceptorMessage); } catch (Exception e) { if (message != null && producer != null) { log.warn("Error executing interceptor beforeSend callback for messageId: {}, topicName:{} ", message.getMessageId(), producer.getTopic(), e); } else { log.warn("Error Error executing interceptor beforeSend callback ", e); } } } return interceptorMessage; }
public boolean removeProducer(ProducerHandler producer) { final String topicName = producer.getProducer().getTopic(); if (topicProducerMap.containsKey(topicName)) { return topicProducerMap.get(topicName).remove(producer); } return false; }
public boolean addProducer(ProducerHandler producer) { return topicProducerMap .computeIfAbsent(producer.getProducer().getTopic(), topic -> new ConcurrentOpenHashSet<>()) .add(producer); }
log.warn("[{}] Error occurred while producer handler was sending msg from {}: {}", producer.getTopic(), getRemote().getInetSocketAddress().toString(), exception.getMessage()); numMsgsFailed.increment();
log.error("Send to dead letter topic exception with topic: {}, messageId: {}", deadLetterProducer.getTopic(), messageId, e);