org.springframework.kafka.support.SendResult Java Examples

The following examples show how to use org.springframework.kafka.support.SendResult. 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: KafkaProducer.java    From cubeai with Apache License 2.0 12 votes vote down vote up
public void send(String topic, String message) {
    // the KafkaTemplate provides asynchronous send methods returning a Future
    ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topic, message);

    // register a callback with the listener to receive the result of the send asynchronously
    future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {

        @Override
        public void onSuccess(SendResult<String, String> result) {
            log.info("Kafka sent message='{}' with offset={}", message,
                result.getRecordMetadata().offset());
        }

        @Override
        public void onFailure(Throwable ex) {
            log.error("Kafka unable to send message='{}'", message, ex);
        }
    });

    // or, to block the sending thread to await the result, invoke the future's get() method
}
 
Example #2
Source File: KafkaApplication.java    From tutorials with MIT License 6 votes vote down vote up
public void sendMessage(String message) {

            ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topicName, message);

            future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {

                @Override
                public void onSuccess(SendResult<String, String> result) {
                    System.out.println("Sent message=[" + message + "] with offset=[" + result.getRecordMetadata()
                        .offset() + "]");
                }

                @Override
                public void onFailure(Throwable ex) {
                    System.out.println("Unable to send message=[" + message + "] due to : " + ex.getMessage());
                }
            });
        }
 
Example #3
Source File: KafkaDriverPublisher.java    From stateful-functions with Apache License 2.0 6 votes vote down vote up
@Override
public void accept(InboundDriverMessage driver) {
  byte[] keyBytes = driver.getDriverId().getBytes(StandardCharsets.UTF_8);
  ListenableFuture<SendResult<Object, Object>> future =
      kafkaTemplate.send(topic, keyBytes, driver.toByteArray());

  future.addCallback(
      new ListenableFutureCallback<SendResult<Object, Object>>() {
        @Override
        public void onFailure(Throwable throwable) {
          log.warn("Failed sending an event to kafka", throwable);
        }

        @Override
        public void onSuccess(SendResult<Object, Object> objectObjectSendResult) {}
      });
}
 
Example #4
Source File: KafkaPassengerPublisher.java    From stateful-functions with Apache License 2.0 6 votes vote down vote up
@Override
public void accept(InboundPassengerMessage passenger) {
  byte[] bytes = passenger.getPassengerId().getBytes(StandardCharsets.UTF_8);
  kafkaTemplate
      .send(topic, bytes, passenger.toByteArray())
      .addCallback(
          new ListenableFutureCallback<SendResult<Object, Object>>() {
            @Override
            public void onFailure(@NonNull Throwable throwable) {
              log.warn("couldn't send passenger data.", throwable);
            }

            @Override
            public void onSuccess(SendResult<Object, Object> objectObjectSendResult) {
              log.info("Sent passenger data");
            }
          });
}
 
Example #5
Source File: KafkaProducer.java    From cubeai with Apache License 2.0 6 votes vote down vote up
public void send(String topic, String message) {
    // the KafkaTemplate provides asynchronous send methods returning a Future
    ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topic, message);

    // register a callback with the listener to receive the result of the send asynchronously
    future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {

        @Override
        public void onSuccess(SendResult<String, String> result) {
            log.info("Kafka sent message='{}' with offset={}", message,
                result.getRecordMetadata().offset());
        }

        @Override
        public void onFailure(Throwable ex) {
            log.error("Kafka unable to send message='{}'", message, ex);
        }
    });

    // or, to block the sending thread to await the result, invoke the future's get() method
}
 
Example #6
Source File: KafkaProducerTest.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
/**
 * 同步发送 10000 条消息(吞吐量很低,但可靠)
 */
@Test
public void syncSend() {
    long begin = System.currentTimeMillis();
    for (int i = 0; i < BATCH_SIZE; i++) {
        KafkaTestBean<Integer> bean = new KafkaTestBean<>();
        bean.setData(i).setTimestamp(new Date());
        String value = JSON.toJSONString(bean);
        try {
            SendResult<String, String> result = kafkaTemplate.send(stringStringTopic, "no-sequence", value).get();
            log.info("Producer send result [key = {}, value = {}]", result.getProducerRecord().key(),
                result.getProducerRecord().value());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
            log.error("发送 Kafka 消息 [topic = {}, key = {}, value = {}] 失败!Exception: {}",
                stringStringTopic, "no-sequence", value, e.getMessage());
        }
    }
    long end = System.currentTimeMillis();
    AnsiSystem.BLUE.println("耗时:" + (end - begin));
}
 
Example #7
Source File: SendKafkaMessageService.java    From enode with MIT License 6 votes vote down vote up
@Override
public CompletableFuture<Void> sendMessageAsync(QueueMessage queueMessage) {
    CompletableFuture<Void> future = new CompletableFuture<>();
    ProducerRecord<String, String> message = KafkaTool.covertToProducerRecord(queueMessage);
    producer.send(message).addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
        @Override
        public void onFailure(Throwable throwable) {
            logger.error("Enode message async send has exception, message: {}", message, throwable);
            future.completeExceptionally(new IORuntimeException(throwable));
        }

        @Override
        public void onSuccess(SendResult<String, String> result) {
            if (logger.isDebugEnabled()) {
                logger.debug("Enode message async send success, sendResult: {}, message: {}", result, message);
            }
            future.complete(null);
        }
    });
    return future;
}
 
Example #8
Source File: KafkaSender.java    From java-tutorial with MIT License 6 votes vote down vote up
/**
 * kafka 发送消息
 *
 * @param obj 消息对象
 */
public void send(T obj) {
    String jsonObj = JSON.toJSONString(obj);
    logger.info("------------ message = {}", jsonObj);

    String topic = "jwell-opt-log";
    //发送消息
    ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(topic, jsonObj);
    future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
        @Override
        public void onFailure(Throwable throwable) {
            logger.info("Produce: The message failed to be sent:" + throwable.getMessage());
        }

        @Override
        public void onSuccess(SendResult<String, Object> stringObjectSendResult) {
            //TODO 业务处理
            logger.info("Produce: The message was sent successfully:");
            logger.info("Produce: _+_+_+_+_+_+_+ result: " + stringObjectSendResult.toString());
        }
    });
}
 
Example #9
Source File: KafkaChannel.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void doSend(String topic, SyncWrapper<String> wrapper) {
  final SyncData event = wrapper.getEvent();
  // require that messages with the same key (for instance, a unique id) are always seen in the correct order,
  // attaching a key to messages will ensure messages with the same key always go to the same partition in a topic
  ListenableFuture<SendResult<String, Object>> future;
  Long partitionId = event.getPartitionId();
  if (partitionId != null) {
    future = kafkaTemplate.send(topic, partitionId.toString(), event.getResult());
  } else {
    logger.warn("Send {} to {} without key", event, topic);
    future = kafkaTemplate.send(topic, event.getResult());
  }
  ListenableFutureCallback<SendResult<String, Object>> callback = new ListenableFutureCallback<SendResult<String, Object>>() {

    @Override
    public void onSuccess(final SendResult<String, Object> message) {
      ackSuccess(Lists.newArrayList(wrapper));
      logger.info("sent {} with offset {} ", event, message.getRecordMetadata().offset());
    }

    @Override
    public void onFailure(final Throwable throwable) {
      SyncerHealth.consumer(consumerId, identifier, Health.red(throwable.getMessage()));
      retryFailed(Lists.newArrayList(wrapper), throwable);
      logger.error("unable to send {} ", event, throwable);
    }
  };
  future.addCallback(callback);
  // no need to wait future, the order between batch is ensured by kafka client
}
 
Example #10
Source File: KafkaStructuredEventHandlerTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private ListenableFuture<SendResult<String, String>> generateMockFutureWrappers() throws InterruptedException, ExecutionException {
    ListenableFuture futureMock = mock(ListenableFuture.class);
    when(futureMock.get()).thenReturn((SendResult<String, String>) mock(SendResult.class));
    return (ListenableFuture<SendResult<String, String>>) futureMock;
}
 
Example #11
Source File: KafkaStructuredEventHandlerTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@Test
public void checkEventTypeBasedTopicDistribution() throws ExecutionException, InterruptedException {
    StructuredRestCallEvent structuredEvent = createDummyStructuredRestEvent();
    Event<StructuredEvent> event = new Event<>(structuredEvent);
    ListenableFuture<SendResult<String, String>> futures = generateMockFutureWrappers();
    when(kafkaTemplate.send(eq("cbStructuredRestCallEvent"), anyString())).thenReturn(futures);

    classIntest.accept(event);

    verify(kafkaTemplate).send(eq("cbStructuredRestCallEvent"), anyString());
}
 
Example #12
Source File: AProducerHandler.java    From SO with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Called when the {@link ListenableFuture} completes with success.
 * <p>Note that Exceptions raised by this method are ignored.
 *
 * @param result the result
 */
@Override
public void onSuccess(SendResult<String, String> result) {
    log.info("sent message='{}' with offset={}", result.getProducerRecord(),
            result.getRecordMetadata().offset());
    onCompleted(result);
}
 
Example #13
Source File: AProducerHandler.java    From SO with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Called when the {@link ListenableFuture} completes with success.
 * <p>Note that Exceptions raised by this method are ignored.
 *
 * @param result the result
 */
@Override
public void onSuccess(SendResult<String, String> result) {
    log.info("sent message='{}' with offset={}", result.getProducerRecord(),
            result.getRecordMetadata().offset());
    onCompleted(result);
}
 
Example #14
Source File: Application.java    From loc-framework with MIT License 5 votes vote down vote up
@PostMapping(value = "/send")
public Problem send() {
  ListenableFuture<SendResult<byte[], byte[]>> future = kafkaProducer
      .send(TEST_TOPIC, DemoInfo.builder().name("loc").age(123).id(1000).score(100).build());
  future.addCallback(
      (result) -> log.info("send message success"),
      (e) -> log.error(e.getMessage(), e)
  );
  return Problem.builder().with("data", "success").build();
}
 
Example #15
Source File: LocKafkaProducer.java    From loc-framework with MIT License 5 votes vote down vote up
public ListenableFuture<SendResult<byte[], byte[]>> send(String topic, Object message) {
  try {
    log.info("send message, topic is {}, message is {}", topic, objectMapper.writeValueAsString(message));
    ProducerRecord<byte[], byte[]> producerRecord = createRecord(topic, objectMapper.writeValueAsString(message));
    return kafkaTemplate.send(producerRecord);
  } catch (Exception e) {
    throw new LocKafkaProducerException(e.getMessage(), e);
  }
}
 
Example #16
Source File: KafkaRegister.java    From bird-java with MIT License 5 votes vote down vote up
/**
 * 事件注册
 *
 * @param eventArg 事件参数
 */
@Override
public void regist(IEventArg eventArg) {
    ListenableFuture<SendResult<String, IEventArg>> listenableFuture = kafkaTemplate.send(getTopic(eventArg), eventArg);

    EventRegisterResult registerResult = new EventRegisterResult(eventArg);
    //发送成功回调
    SuccessCallback<SendResult<String, IEventArg>> successCallback = result -> {
        if (registerStore == null) return;
        registerResult.setSuccess(true);
        Map<String, Object> map = new HashMap<>(2);
        if (result != null) {
            map.put("producerRecord", result.getProducerRecord());
            map.put("metadata", result.getRecordMetadata());
        }
        registerResult.setExtJson(JSON.toJSONString(map));
        registerStore.register(registerResult);
    };

    //发送失败回调
    FailureCallback failureCallback = ex -> {
        if (registerStore == null) return;
        registerResult.setSuccess(false);
        registerResult.setMessage(ex.getMessage());
        registerStore.register(registerResult);

        log.error(ex.getMessage());
    };
    listenableFuture.addCallback(successCallback, failureCallback);
}
 
Example #17
Source File: StreamBindingEventHandlerForKafka.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
private Future<SendResult<SpecificRecord, SpecificRecord>> sendStreamBindingNotificationEvent(NotificationEvent<StreamBinding> event) {
  return sendEntityNotificationEvent(
      streamBindingToKeyRecord,
      streamBindingToValueRecord,
      kafkaTemplate::send,
      notificationEventsTopic,
      event
  );
}
 
Example #18
Source File: KafkaSendService.java    From spring-boot-study with MIT License 5 votes vote down vote up
/**
 * 异步示例
 * */
public void sendAnsyc(final String topic,final String message){
    ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(topic,message);
    future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
        @Override
        public void onSuccess(SendResult<String, Object> result) {
            System.out.println("发送消息成功:" + result);
        }

        @Override
        public void onFailure(Throwable ex) {
            System.out.println("发送消息失败:"+ ex.getMessage());
        }
    });
}
 
Example #19
Source File: MultiPartitionMessageProducer.java    From kafka-with-springboot with Apache License 2.0 5 votes vote down vote up
public void send(String topic, String key, String payload){
    LOGGER.info("Sending payload='{}' to topic='{}' with key='{}'", payload, topic, key);
    ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topic, key ,payload);
    SuccessCallback<SendResult<String,String>> successCallback = sendResult -> {
        LOGGER.info("Sent payload='{}' with key='{}' to topic-partition@offset='{}'", payload, key, sendResult.getRecordMetadata().toString());
    };
    FailureCallback failureCallback = throwable -> {
        LOGGER.info("Sending payload='{}' to topic='{}' with key='{}' failed!!!", payload, topic, key);
    };
    future.addCallback(successCallback, failureCallback);
}
 
Example #20
Source File: ConsumerEventHandlerForKafka.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
private Future<SendResult<SpecificRecord, SpecificRecord>> sendConsumerNotificationEvent(NotificationEvent<Consumer> event) {
  return sendEntityNotificationEvent(
      consumerToKeyRecord,
      consumerToValueRecord,
      kafkaTemplate::send,
      notificationEventsTopic,
      event
  );
}
 
Example #21
Source File: StreamEventHandlerForKafka.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
private Future<SendResult<SpecificRecord, SpecificRecord>> sendStreamNotificationEvent(NotificationEvent<Stream> event) {
  return sendEntityNotificationEvent(
      streamToKeyRecord,
      streamToValueRecord,
      kafkaTemplate::send,
      notificationEventsTopic,
      event
  );
}
 
Example #22
Source File: SchemaEventHandlerForKafka.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
private Future<SendResult<SpecificRecord, SpecificRecord>> sendSchemaNotificationEvent(NotificationEvent<Schema> event) {
  return sendEntityNotificationEvent(
      schemaToKeyRecord,
      schemaToValueRecord,
      kafkaTemplate::send,
      notificationEventsTopic,
      event
  );
}
 
Example #23
Source File: ProducerEventHandlerForKafka.java    From stream-registry with Apache License 2.0 5 votes vote down vote up
private Future<SendResult<SpecificRecord, SpecificRecord>> sendProducerNotificationEvent(NotificationEvent<Producer> event) {
  return sendEntityNotificationEvent(
      producerToKeyRecord,
      producerToValueRecord,
      kafkaTemplate::send,
      notificationEventsTopic,
      event
  );
}
 
Example #24
Source File: BinLogDistributorAppApplicationTests.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
@Test
public void prod() throws Exception {
	UpdateRowsDTO dto = new UpdateRowsDTO();
	dto.setDatabase("abc");
	dto.setTable("a");
	dto.setUuid("123");

	org.springframework.util.concurrent.ListenableFuture<SendResult> future = template.send(TOPICS,6, dto);
	future.addCallback((success) -> {
		logger.info("success:" + success.getRecordMetadata().offset());
	}, (failure) -> {
		logger.info("failure:", failure.getCause());
	});
	System.in.read();
}
 
Example #25
Source File: KafkaRepository.java    From kafka-service-broker with Apache License 2.0 4 votes vote down vote up
public  ListenableFuture<SendResult<Integer, String>> sendMessage(String message) throws ExecutionException, InterruptedException {
    KafkaTemplate<Integer, String> template = getTemplate();
    ListenableFuture<SendResult<Integer, String>> future = template.send(info.getTopicName(), message);
    template.flush();
    return future;
}
 
Example #26
Source File: DataPublisherKafkaImpl.java    From kkbinlog with Apache License 2.0 4 votes vote down vote up
@Override
public void doPublish(String topic, Object data) {
    ListenableFuture<SendResult<String, Object>> reuslt = kafkaTemplate.send(topic, data);
    reuslt.addCallback(success -> log.info("推送消息到Kafka:{}", data)
    , failure -> log.error("推送消息到Kafka失败:" + data.toString(), failure.getCause()));
}
 
Example #27
Source File: KafkaStreamsBinderHealthIndicatorTests.java    From spring-cloud-stream-binder-kafka with Apache License 2.0 4 votes vote down vote up
private void receive(ConfigurableApplicationContext context,
		List<ProducerRecord<Integer, String>> records, Status expected,
		String... topics) throws Exception {
	Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("group-id0",
			"false", embeddedKafka);
	consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
	DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>(
			consumerProps);

	Map<String, Object> senderProps = KafkaTestUtils.producerProps(embeddedKafka);
	DefaultKafkaProducerFactory<Integer, String> pf = new DefaultKafkaProducerFactory<>(
			senderProps);
	try (Consumer<String, String> consumer = cf.createConsumer()) {
		KafkaTemplate<Integer, String> template = new KafkaTemplate<>(pf, true);
		CountDownLatch latch = new CountDownLatch(records.size());
		for (ProducerRecord<Integer, String> record : records) {
			ListenableFuture<SendResult<Integer, String>> future = template
					.send(record);
			future.addCallback(
					new ListenableFutureCallback<SendResult<Integer, String>>() {
						@Override
						public void onFailure(Throwable ex) {
							Assert.fail();
						}

						@Override
						public void onSuccess(SendResult<Integer, String> result) {
							latch.countDown();
						}
					});
		}

		latch.await(5, TimeUnit.SECONDS);

		embeddedKafka.consumeFromEmbeddedTopics(consumer, topics);
		KafkaTestUtils.getRecords(consumer, 1000);

		TimeUnit.SECONDS.sleep(2);
		checkHealth(context, expected);
	}
	finally {
		pf.destroy();
	}
}
 
Example #28
Source File: DomainEventPublisher.java    From integration-patterns with MIT License 4 votes vote down vote up
private void publish(final DomainEvent event) throws InterruptedException, ExecutionException, TimeoutException {
    // need to block here so that following statements are executed inside transaction
    SendResult<String, String> sendResult = eventPublisher.publish(event).get(1, TimeUnit.SECONDS);
    LOG.debug("Published event to topic:partition {}:{} at {}", sendResult.getProducerRecord().topic(),
        sendResult.getProducerRecord().partition(), sendResult.getProducerRecord().timestamp());
}
 
Example #29
Source File: KafkaGateway.java    From integration-patterns with MIT License 4 votes vote down vote up
public ListenableFuture<SendResult<String, String>> publish(final DomainEvent event) {
    LOGGER.info("publishing event {} to topic {}", event.getId(), topic);
    return kafkaTemplate.send(topic, event.getKey(), toEventMessage(event));
}
 
Example #30
Source File: AProducerHandler.java    From SO with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * send message to kafka.<BR/>
 *
 * @param topic   topic
 * @param key     key
 * @param message value
 */
public void send(String topic, K key, V message, ListenableFutureCallback<SendResult<K, V>> callback) {
    ListenableFuture<SendResult<K, V>> future = kafkaTemplate.send(topic, key, message);
    if (callback != null)
        future.addCallback(callback);
}