org.apache.kafka.clients.producer.ProducerRecord Java Examples

The following examples show how to use org.apache.kafka.clients.producer.ProducerRecord. 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: KafkaProducerRecordImpl.java    From vertx-kafka-client with Apache License 2.0 7 votes vote down vote up
@Override
public ProducerRecord<K, V> record() {
  if (headers.isEmpty()) {
    return new ProducerRecord<>(topic, partition, timestamp, key, value);
  } else {
    return new ProducerRecord<>(
      topic,
      partition,
      timestamp,
      key,
      value,
      headers.stream()
        .map(header -> new RecordHeader(header.key(), header.value().getBytes()))
        .collect(Collectors.toList()));
  }
}
 
Example #2
Source File: KafkaRangerAuthorizerTest.java    From ranger with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthorizedWrite() throws Exception {
    // Create the Producer
    Properties producerProps = new Properties();
    producerProps.put("bootstrap.servers", "localhost:" + port);
    producerProps.put("acks", "all");
    producerProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    producerProps.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SSL");
    producerProps.put(SslConfigs.SSL_KEYSTORE_TYPE_CONFIG, "JKS");
    producerProps.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, serviceKeystorePath);
    producerProps.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "sspass");
    producerProps.put(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "skpass");
    producerProps.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, truststorePath);
    producerProps.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "security");
    
    final Producer<String, String> producer = new KafkaProducer<>(producerProps);
    // Send a message
    Future<RecordMetadata> record = 
        producer.send(new ProducerRecord<String, String>("dev", "somekey", "somevalue"));
    producer.flush();
    record.get();

    producer.close();
}
 
Example #3
Source File: KafkaUsage.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
/**
 * Use the supplied function to asynchronously produce messages and write them to the cluster.
 *
 * @param producerName the name of the producer; may not be null
 * @param messageCount the number of messages to produce; must be positive
 * @param keySerializer the serializer for the keys; may not be null
 * @param valueSerializer the serializer for the values; may not be null
 * @param completionCallback the function to be called when the producer is completed; may be null
 * @param messageSupplier the function to produce messages; may not be null
 */
public <K, V> void produce(String producerName, int messageCount,
        Serializer<K> keySerializer, Serializer<V> valueSerializer,
        Runnable completionCallback,
        Supplier<ProducerRecord<K, V>> messageSupplier) {
    Properties props = getProducerProperties(producerName);
    Thread t = new Thread(() -> {
        LOGGER.infof("Starting producer %s to write %s messages", producerName, messageCount);
        try (KafkaProducer<K, V> producer = new KafkaProducer<>(props, keySerializer, valueSerializer)) {
            for (int i = 0; i != messageCount; ++i) {
                ProducerRecord<K, V> record = messageSupplier.get();
                producer.send(record);
                producer.flush();
                LOGGER.infof("Producer %s: sent message %s", producerName, record);
            }
        } finally {
            if (completionCallback != null) {
                completionCallback.run();
            }
            LOGGER.infof("Stopping producer %s", producerName);
        }
    });
    t.setName(producerName + "-thread");
    t.start();
}
 
Example #4
Source File: ESErrorRetryWriter.java    From SkaETL with Apache License 2.0 6 votes vote down vote up
public void sendToErrorTopic(String applicationId, ErrorData errorData) {
    JsonNode value = JSONUtils.getInstance().parse(errorData.message);
    String type = value.has("type") ?  value.get("type").asText() : "unknown";
    Metrics.counter("skaetl_nb_produce_message_kafka_count",
            Lists.newArrayList(
                    Tag.of("processConsumerName", applicationId),
                    Tag.of("topic", kafkaConfiguration.getErrorTopic()),
                    Tag.of("type", type)
            )
    ).increment();
    Metrics.counter("skaetl_nb_produce_error_kafka_count",
            Lists.newArrayList(
                    Tag.of("processConsumerName", applicationId),
                    Tag.of("type", type),
                    Tag.of("reason", errorData.getErrorReason())
            )
    ).increment();
    errorProducer.send(new ProducerRecord<>(kafkaConfiguration.getErrorTopic(), errorData));
}
 
Example #5
Source File: KafkaTest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
@Test
public void clients(final MockTracer tracer) throws Exception {
  try (final Producer<Integer,String> producer = createProducer()) {
    // Send 1
    producer.send(new ProducerRecord<>("messages", 1, "test"));

    // Send 2
    producer.send(new ProducerRecord<>("messages", 1, "test"));

    final CountDownLatch latch = new CountDownLatch(2);
    createConsumer(latch, 1, tracer);
  }

  final List<MockSpan> mockSpans = tracer.finishedSpans();
  assertEquals(4, mockSpans.size());
  assertNull(tracer.activeSpan());
}
 
Example #6
Source File: KafkaRangerAuthorizerGSSTest.java    From ranger with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnauthorizedWrite() throws Exception {
    // Create the Producer
    Properties producerProps = new Properties();
    producerProps.put("bootstrap.servers", "localhost:" + port);
    producerProps.put("acks", "all");
    producerProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    producerProps.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
    producerProps.put("sasl.mechanism", "GSSAPI");
    producerProps.put("sasl.kerberos.service.name", "kafka");

    final Producer<String, String> producer = new KafkaProducer<>(producerProps);

    // Send a message
    try {
        Future<RecordMetadata> record =
            producer.send(new ProducerRecord<String, String>("dev", "somekey", "somevalue"));
        producer.flush();
        record.get();
    } catch (Exception ex) {
        Assert.assertTrue(ex.getMessage().contains("Not authorized to access topics"));
    }

    producer.close();
}
 
Example #7
Source File: MyProducer.java    From ad with Apache License 2.0 6 votes vote down vote up
/** 适中*/
private static void sendMessageAsync() {
    ProducerRecord<String, String> record = new ProducerRecord<>(
            "kafka-topic",
            "key",
            "async"
    );
    producer.send(record, ((metadata, e) -> {
        log.info("coming into callback");
        if (e != null) {
            log.error("kafka exception: {}", e.getMessage());
            return;
        }
        log.debug("topic: {}", metadata.topic());
        log.debug("partition: {}", metadata.partition());
        log.debug("offset: {}", metadata.offset());
    }));
}
 
Example #8
Source File: EventProducer.java    From mercury with Apache License 2.0 6 votes vote down vote up
private void initializeConsumer() {
    startProducer();
    try {
        String origin = Platform.getInstance().getOrigin();
        String uuid = Utility.getInstance().getUuid();
        EventEnvelope direct = new EventEnvelope().setTo(ServiceDiscovery.SERVICE_REGISTRY).setHeader(TYPE, INIT);
        producer.send(new ProducerRecord<>(origin, uuid, direct.toBytes())).get(20, TimeUnit.SECONDS);
        totalEvents++;
        lastActive = System.currentTimeMillis();
        log.info("Tell event consumer to start with {}", origin);
    } catch (Exception e) {
        /*
         * Unrecoverable error. Shutdown and let infrastructure to restart this app instance.
         */
        log.error("Unable to initialize consumer - {}", e.getMessage());
        System.exit(20);
    }
}
 
Example #9
Source File: CounterProviderString.java    From jMetalSP with MIT License 6 votes vote down vote up
public void run(){
    int count = 0;
    long startTime = System.currentTimeMillis();
    while (true){
        String auxCount = count+"";
        Future<RecordMetadata> send =
                producer.send(new ProducerRecord<String, String>
                        (topic,auxCount,auxCount), new ProducerCallBack(startTime, count, "Count ->" + count) );

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        count++;
    }
}
 
Example #10
Source File: BoltCommandHandlerHelper.java    From DBus with Apache License 2.0 6 votes vote down vote up
public static void writeEmailMessage(String subject, String contents, String dataSchema, Producer<String, String> producer) {
    try {
        // 发邮件
        ControlMessage gm = new ControlMessage(System.currentTimeMillis(), ControlType.COMMON_EMAIL_MESSAGE.toString(), BoltCommandHandlerHelper.class.getName());

        gm.addPayload("subject", subject);
        gm.addPayload("contents", contents);
        gm.addPayload("datasource_schema", Utils.getDatasource().getDsName() + "/" + dataSchema);

        String topic = PropertiesHolder.getProperties(Constants.Properties.CONFIGURE, Constants.ConfigureKey.GLOBAL_EVENT_TOPIC);
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, gm.getType(), gm.toJSONString());
        producer.send(record, (metadata, exception) -> {
            if (exception != null) {
                logger.error("Send global event error.{}", exception.getMessage());
            }
        });
    } catch (Exception e) {
        logger.error("send email error. schema:{}, subject:{}, content:{}", dataSchema, subject, contents, e);
    } finally {
        if (producer != null) producer.close();
    }
}
 
Example #11
Source File: TestKafkaWriter.java    From singer with Apache License 2.0 6 votes vote down vote up
public KafkaWritingTaskResult createResult(KafkaWritingTask worker, List<PartitionInfo> sortedPartitions){
    long start = System.currentTimeMillis();
    List<ProducerRecord<byte[], byte[]>> messages = worker.getMessages();
    List<RecordMetadata> recordMetadataList = new ArrayList<>();
    int bytes = 0;
    for(int i = 0; i < messages.size(); i++){
      int keySize = messages.get(i).key().length;
      int valSize = messages.get(i).value().length;
      bytes += keySize + valSize;
      int partition = messages.get(i).partition();
      String topic = sortedPartitions.get(partition).topic();
      TopicPartition topicPartition = new TopicPartition(topic,partition);
      recordMetadataList.add(new RecordMetadata(topicPartition, 0,0,0,0L, keySize, valSize));
    }
    long end = System.currentTimeMillis();
    KafkaWritingTaskResult  kafkaWritingTaskResult = new KafkaWritingTaskResult(true, bytes, (int)(end - start));
    kafkaWritingTaskResult.setPartition(messages.get(0).partition());
    kafkaWritingTaskResult.setRecordMetadataList(recordMetadataList);
    return kafkaWritingTaskResult;
}
 
Example #12
Source File: KafkaNotificationMockTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldSendMessagesSuccessfully() throws NotificationException,
        ExecutionException, InterruptedException {
    Properties configProperties = mock(Properties.class);
    KafkaNotification kafkaNotification = new KafkaNotification(configProperties);

    Producer producer = mock(Producer.class);
    String topicName = kafkaNotification.getTopicName(NotificationInterface.NotificationType.HOOK);
    String message = "This is a test message";
    Future returnValue = mock(Future.class);
    when(returnValue.get()).thenReturn(new RecordMetadata(new TopicPartition(topicName, 0), 0, 0));
    ProducerRecord expectedRecord = new ProducerRecord(topicName, message);
    when(producer.send(expectedRecord)).thenReturn(returnValue);

    kafkaNotification.sendInternalToProducer(producer,
            NotificationInterface.NotificationType.HOOK, new String[]{message});

    verify(producer).send(expectedRecord);
}
 
Example #13
Source File: TracingKafkaProducer.java    From java-kafka-client with Apache License 2.0 6 votes vote down vote up
public Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback callback,
    SpanContext parent) {
  /*
  // Create wrappedRecord because headers can be read only in record (if record is sent second time)
  ProducerRecord<K, V> wrappedRecord = new ProducerRecord<>(record.topic(),
      record.partition(),
      record.timestamp(),
      record.key(),
      record.value(),
      record.headers());
  */

  Span span = TracingKafkaUtils
      .buildAndInjectSpan(record, tracer, producerSpanNameProvider, parent, spanDecorators);
  try (Scope ignored = tracer.activateSpan(span)) {
    Callback wrappedCallback = new TracingCallback(callback, span, tracer, spanDecorators);
    return producer.send(record, wrappedCallback);
  }
}
 
Example #14
Source File: KafkaNotificationMockTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldSendMessagesSuccessfully() throws NotificationException,
        ExecutionException, InterruptedException {
    Properties configProperties = mock(Properties.class);
    KafkaNotification kafkaNotification = new KafkaNotification(configProperties);

    Producer producer = mock(Producer.class);
    String topicName = kafkaNotification.getProducerTopicName(NotificationInterface.NotificationType.HOOK);
    String message = "This is a test message";
    Future returnValue = mock(Future.class);
    TopicPartition topicPartition = new TopicPartition(topicName, 0);
    when(returnValue.get()).thenReturn(new RecordMetadata(topicPartition, 0, 0, 0, Long.valueOf(0), 0, 0));
    ProducerRecord expectedRecord = new ProducerRecord(topicName, message);
    when(producer.send(expectedRecord)).thenReturn(returnValue);

    kafkaNotification.sendInternalToProducer(producer,
            NotificationInterface.NotificationType.HOOK, Arrays.asList(new String[]{message}));

    verify(producer).send(expectedRecord);
}
 
Example #15
Source File: KafkaMessageWriter.java    From java-course-ee with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("acks", "all");
    props.put("delivery.timeout.ms", 30000);
    props.put("request.timeout.ms", 20000);
    props.put("batch.size", 16384);
    props.put("linger.ms", 1);
    props.put("buffer.memory", 33554432);
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");


    Producer<String, String> producer = new KafkaProducer<>(props);
    for (int i = 0; i < 100; i++) {
        producer.send(new ProducerRecord<String, String>("test.topic", Integer.toString(i), Integer.toString(i)));
    }

    producer.close();
}
 
Example #16
Source File: KafkaNotificationMockTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void shouldThrowExceptionIfProducerFails() throws NotificationException,
        ExecutionException, InterruptedException {
    Properties configProperties = mock(Properties.class);
    KafkaNotification kafkaNotification = new KafkaNotification(configProperties);

    Producer producer = mock(Producer.class);
    String topicName = kafkaNotification.getProducerTopicName(NotificationInterface.NotificationType.HOOK);
    String message = "This is a test message";
    Future returnValue = mock(Future.class);
    when(returnValue.get()).thenThrow(new RuntimeException("Simulating exception"));
    ProducerRecord expectedRecord = new ProducerRecord(topicName, message);
    when(producer.send(expectedRecord)).thenReturn(returnValue);

    try {
        kafkaNotification.sendInternalToProducer(producer,
            NotificationInterface.NotificationType.HOOK, Arrays.asList(new String[]{message}));
        fail("Should have thrown NotificationException");
    } catch (NotificationException e) {
        assertEquals(e.getFailedMessages().size(), 1);
        assertEquals(e.getFailedMessages().get(0), "This is a test message");
    }
}
 
Example #17
Source File: KafkaSink.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
private void handleWriteResult(AsyncResult<?> ar, Message<?> message, ProducerRecord<?, ?> record,
        UniEmitter<? super Void> emitter) {
    String actualTopic = record.topic();
    if (ar.succeeded()) {
        log.successfullyToTopic(message, actualTopic);
        message.ack().whenComplete((x, f) -> {
            if (f != null) {
                emitter.fail(f);
            } else {
                emitter.complete(null);
            }
        });
    } else {
        // Fail, there will be retry.
        emitter.fail(ar.cause());
    }
}
 
Example #18
Source File: MetadataPropagationTest.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromKafkaToAppWithMetadata() {
    KafkaUsage usage = new KafkaUsage();
    deploy(getKafkaSinkConfigForMyAppWithKafkaMetadata(), MyAppWithKafkaMetadata.class);

    AtomicInteger value = new AtomicInteger();
    usage.produceIntegers(100, null,
            () -> new ProducerRecord<>("metadata-topic", "a-key", value.getAndIncrement()));

    MyAppWithKafkaMetadata bean = container.getBeanManager().createInstance().select(MyAppWithKafkaMetadata.class).get();
    await().atMost(2, TimeUnit.MINUTES).until(() -> bean.list().size() >= 10);
    assertThat(bean.list()).contains(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

    assertThat(bean.getMetadata()).isNotNull();
    assertThat(bean.getMetadata()).contains(bean.getOriginal());
    AtomicBoolean foundMetadata = new AtomicBoolean(false);
    for (Object object : bean.getMetadata()) {
        if (object instanceof IncomingKafkaRecordMetadata) {
            IncomingKafkaRecordMetadata incomingMetadata = (IncomingKafkaRecordMetadata) object;
            assertThat(incomingMetadata.getKey()).isEqualTo("a-key");
            assertThat(incomingMetadata.getTopic()).isEqualTo("metadata-topic");
            foundMetadata.compareAndSet(false, true);
        }
    }
    assertThat(foundMetadata.get()).isTrue();
}
 
Example #19
Source File: KafkaLoader.java    From metron with Apache License 2.0 6 votes vote down vote up
public void start() {
  Map<String, Object> producerConfig = new HashMap<>();
  producerConfig.put("bootstrap.servers", brokerUrl);
  producerConfig.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  producerConfig.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  kafkaProducer = new KafkaProducer<>(producerConfig);
  try {
    while (iterations == -1 || iterations-- > 0) {
      BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(samplePath), StandardCharsets.UTF_8));
      String line;
      while((line = reader.readLine()) != null) {
        kafkaProducer.send(new ProducerRecord<String, String>(topic, line));
        Thread.sleep(delay);
      }
      reader.close();
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example #20
Source File: MetricsVerticle.java    From redpipe with Apache License 2.0 6 votes vote down vote up
@Override
public void start() throws Exception {
  systemMBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);

  // A random identifier
  String pid = UUID.randomUUID().toString();

  // Get the kafka producer config
  JsonObject config = config();

  // Create the producer
  producer = KafkaWriteStream.create(vertx.getDelegate(), config.getMap(), String.class, JsonObject.class);

  // Publish the metircs in Kafka
  vertx.setPeriodic(1000, id -> {
    JsonObject metrics = new JsonObject();
    metrics.put("CPU", systemMBean.getProcessCpuLoad());
    metrics.put("Mem", systemMBean.getTotalPhysicalMemorySize() - systemMBean.getFreePhysicalMemorySize());
    producer.write(new ProducerRecord<>("the_topic", new JsonObject().put(pid, metrics)));
  });
}
 
Example #21
Source File: GeneratorRetryService.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
private void sendToKafka(RawDataGen rdg) {
    try {
        String value = mapper.writeValueAsString(rdg);
        log.info("Sending {}", value);
        producer.send(new ProducerRecord(topic, value));
    } catch (Exception e) {
        log.error("Error during converter", e);
    }
}
 
Example #22
Source File: KafkaNotification.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void sendInternalToProducer(Producer p, NotificationType type, String[] messages) throws NotificationException {
    String topic = TOPIC_MAP.get(type);
    List<MessageContext> messageContexts = new ArrayList<>();
    for (String message : messages) {
        ProducerRecord record = new ProducerRecord(topic, message);
        LOG.debug("Sending message for topic {}: {}", topic, message);
        Future future = p.send(record);
        messageContexts.add(new MessageContext(future, message));
    }

    List<String> failedMessages = new ArrayList<>();
    Exception lastFailureException = null;
    for (MessageContext context : messageContexts) {
        try {
            RecordMetadata response = context.getFuture().get();
            LOG.debug("Sent message for topic - {}, partition - {}, offset - {}", response.topic(),
                response.partition(), response.offset());
        } catch (Exception e) {
            lastFailureException = e;
            failedMessages.add(context.getMessage());
        }
    }
    if (lastFailureException != null) {
        throw new NotificationException(lastFailureException, failedMessages);
    }
}
 
Example #23
Source File: ProducerTest.java    From vertx-kafka-client with Apache License 2.0 5 votes vote down vote up
@Test
// Should fail because it cannot reach the broker
public void testBrokerConnectionError(TestContext ctx) throws Exception {
  Properties props = new Properties();
  // use a wrong port on purpose, because Broker IS running
  props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9091");
  props.setProperty(ProducerConfig.ACKS_CONFIG, "1");
  props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
  props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
  props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 2000);

  producer = producer(Vertx.vertx(), props);
  producer.write(new ProducerRecord<>("testBrokerConnectionError", 0, "key", "value"), ctx.asyncAssertFailure());
}
 
Example #24
Source File: ConsumerTestBase.java    From vertx-kafka-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testPause(TestContext ctx) throws Exception {
  final String topicName = "testPause";
  final String consumerId = topicName;

  Async batch = ctx.async();
  AtomicInteger index = new AtomicInteger();
  int numMessages = 1000;
  kafkaCluster.useTo().produceStrings(numMessages, batch::complete, () ->
    new ProducerRecord<>(topicName, 0, "key-" + index.get(), "value-" + index.getAndIncrement()));
  batch.awaitSuccess(20000);
  Properties config = kafkaCluster.useTo().getConsumerProperties(consumerId, consumerId, OffsetResetStrategy.EARLIEST);
  config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
  config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
  consumer = createConsumer(vertx, config);
  Async done = ctx.async();
  AtomicInteger count = new AtomicInteger(numMessages);
  consumer.exceptionHandler(ctx::fail);
  AtomicBoolean paused = new AtomicBoolean();
  consumer.handler(rec -> {
    ctx.assertFalse(paused.get());
    int val = count.decrementAndGet();
    if (val == numMessages / 2) {
      paused.set(true);
      consumer.pause();
      vertx.setTimer(500, id -> {
        paused.set(false);
        consumer.resume();
      });
    }
    if (val == 0) {
      done.complete();
    }
  });
  consumer.subscribe(Collections.singleton(topicName));
}
 
Example #25
Source File: ExactlyOnceE2E.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
private static ProducerRecord<String, WrappedMessage> wrappedMessage(String targetInvokeId) {
  final String key = UUID.randomUUID().toString();

  return new ProducerRecord<>(
      KafkaIO.WRAPPED_MESSAGES_TOPIC_NAME,
      key,
      WrappedMessage.newBuilder().setInvokeTargetId(targetInvokeId).setKey(key).build());
}
 
Example #26
Source File: KafkaComponent.java    From metron with Apache License 2.0 5 votes vote down vote up
/**
 * Write a collection of messages to a Kafka topic.
 *
 * @param topic The name of the Kafka topic.
 * @param messages The collection of messages to write.
 */
public void writeMessages(String topic, Collection<byte[]> messages) {
  try(KafkaProducer<String, byte[]> kafkaProducer = createProducer()) {
    for (byte[] message : messages) {
      kafkaProducer.send(new ProducerRecord<>(topic, message));
    }
  }
}
 
Example #27
Source File: KafkaIOTest.java    From beam with Apache License 2.0 5 votes vote down vote up
private static void verifyProducerRecords(
    MockProducer<Integer, Long> mockProducer,
    String topic,
    int numElements,
    boolean keyIsAbsent,
    boolean verifyTimestamp) {

  // verify that appropriate messages are written to kafka
  List<ProducerRecord<Integer, Long>> sent = mockProducer.history();

  // sort by values
  sent.sort(Comparator.comparingLong(ProducerRecord::value));

  for (int i = 0; i < numElements; i++) {
    ProducerRecord<Integer, Long> record = sent.get(i);
    assertEquals(topic, record.topic());
    if (keyIsAbsent) {
      assertNull(record.key());
    } else {
      assertEquals(i, record.key().intValue());
    }
    assertEquals(i, record.value().longValue());
    if (verifyTimestamp) {
      assertEquals(i, record.timestamp().intValue());
    }
  }
}
 
Example #28
Source File: Dispatcher.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
@Override
public void run() {
    int messageCounter = 1;
    String producerName = Thread.currentThread().getName();

    logger.trace("Starting Producer thread" + producerName);
    for (Object data : dataList) {
        producer.send(new ProducerRecord<>(topicName, messageKey, data));
        messageCounter++;
    }
    logger.trace("Finished Producer thread" + producerName + " sent " + messageCounter + " messages");
}
 
Example #29
Source File: KeycloakRefreshTokenWithJwtValidationTest.java    From strimzi-kafka-oauth with Apache License 2.0 5 votes vote down vote up
@Test
public void doTest() throws Exception {
    System.out.println("==== KeycloakRefreshTokenWithJwtValidationTest ====");

    final String topic = "KeycloakRefreshTokenWithJwtValidationTest";
    final String tokenEndpointUri = "http://" + HOST + ":8080/auth/realms/" + REALM + "/protocol/openid-connect/token";

    String refreshToken = loginWithClientSecretForRefreshToken(URI.create(tokenEndpointUri), CLIENT_ID, CLIENT_SECRET);

    Properties defaults = new Properties();
    defaults.setProperty(ClientConfig.OAUTH_TOKEN_ENDPOINT_URI, tokenEndpointUri);
    defaults.setProperty(ClientConfig.OAUTH_CLIENT_ID, CLIENT_ID);
    defaults.setProperty(ClientConfig.OAUTH_CLIENT_SECRET, CLIENT_SECRET);
    defaults.setProperty(ClientConfig.OAUTH_REFRESH_TOKEN, refreshToken);
    defaults.setProperty(ClientConfig.OAUTH_USERNAME_CLAIM, "preferred_username");

    ConfigProperties.resolveAndExportToSystemProperties(defaults);

    Properties producerProps = buildProducerConfig();
    Producer<String, String> producer = new KafkaProducer<>(producerProps);

    producer.send(new ProducerRecord<>(topic, "The Message")).get();
    System.out.println("Produced The Message");

    Properties consumerProps = buildConsumerConfig();
    Consumer<String, String> consumer = new KafkaConsumer<>(consumerProps);

    TopicPartition partition = new TopicPartition(topic, 0);
    consumer.assign(Arrays.asList(partition));

    while (consumer.partitionsFor(topic, Duration.ofSeconds(1)).size() == 0) {
        System.out.println("No assignment yet for consumer");
    }
    consumer.seekToBeginning(Arrays.asList(partition));

    ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1));

    Assert.assertEquals("Got message", 1, records.count());
    Assert.assertEquals("Is message text: 'The Message'", "The Message", records.iterator().next().value());
}
 
Example #30
Source File: BuildMachineMetricDataUtil.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
public static void writeDataToKafka() throws InterruptedException {
        Properties props = new Properties();
        props.put("bootstrap.servers", BROKER_LIST);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        KafkaProducer producer = new KafkaProducer<String, String>(props);

        while (true) {
            long timestamp = System.currentTimeMillis();
            for (int i = 0; i < hostIps.size(); i++) {
                MetricEvent cpuData = buildCpuData(hostIps.get(i), timestamp);
                MetricEvent loadData = buildLoadData(hostIps.get(i), timestamp);
                MetricEvent memData = buildMemData(hostIps.get(i), timestamp);
                MetricEvent swapData = buildSwapData(hostIps.get(i), timestamp);
                ProducerRecord cpuRecord = new ProducerRecord<String, String>(METRICS_TOPIC, null, null, GsonUtil.toJson(cpuData));
                ProducerRecord loadRecord = new ProducerRecord<String, String>(METRICS_TOPIC, null, null, GsonUtil.toJson(loadData));
                ProducerRecord memRecord = new ProducerRecord<String, String>(METRICS_TOPIC, null, null, GsonUtil.toJson(memData));
                ProducerRecord swapRecord = new ProducerRecord<String, String>(METRICS_TOPIC, null, null, GsonUtil.toJson(swapData));

                producer.send(cpuRecord);
                producer.send(loadRecord);
//                System.out.println(cpuData);
//                System.out.println(loadData);
                producer.send(memRecord);
                producer.send(swapRecord);
            }
            producer.flush();
            Thread.sleep(1000);
        }
    }