Java Code Examples for org.apache.kafka.clients.producer.KafkaProducer

The following examples show how to use org.apache.kafka.clients.producer.KafkaProducer. These examples are extracted from open source projects. 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 Project: java-study   Source File: KafkaProducerTest.java    License: Apache License 2.0 7 votes vote down vote up
public KafkaProducerTest(String topicName) {
	Properties props = new Properties();
	props.put("bootstrap.servers", "master:9092,slave1:9092,slave2:9092");
	//acks=0:如果设置为0,生产者不会等待kafka的响应。
	//acks=1:这个配置意味着kafka会把这条消息写到本地日志文件中,但是不会等待集群中其他机器的成功响应。
	//acks=all:这个配置意味着leader会等待所有的follower同步完成。这个确保消息不会丢失,除非kafka集群中所有机器挂掉。这是最强的可用性保证。
	props.put("acks", "all");
	//配置为大于0的值的话,客户端会在消息发送失败时重新发送。
	props.put("retries", 0);
	//当多条消息需要发送到同一个分区时,生产者会尝试合并网络请求。这会提高client和生产者的效率
	props.put("batch.size", 16384);
	props.put("key.serializer", StringSerializer.class.getName());
	props.put("value.serializer", StringSerializer.class.getName());
	this.producer = new KafkaProducer<String, String>(props);
	this.topic = topicName;
}
 
Example 2
Source Project: components   Source File: KafkaDatasetTestIT.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws TimeoutException {
    // there may exists other topics than these build in(configured in pom.xml) topics, but ignore them

    // ----------------- Send sample data to TOPIC_IN start --------------------
    String testID = "sampleTest" + new Random().nextInt();

    List<Person> expectedPersons = Person.genRandomList(testID, 10);

    Properties props = new Properties();
    props.put("bootstrap.servers", BOOTSTRAP_HOST);
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

    Producer<Void, String> producer = new KafkaProducer<>(props);
    for (Person person : expectedPersons) {
        ProducerRecord<Void, String> message = new ProducerRecord<>(TOPIC_IN, person.toCSV(";"));
        producer.send(message);
    }
    producer.close();
    // ----------------- Send sample data to TOPIC_IN end --------------------
}
 
Example 3
@Override
public void run() {
    PropertyReader propertyReader = new PropertyReader();

    Properties producerProps = new Properties();
    producerProps.put("bootstrap.servers", propertyReader.getPropertyValue("broker.list"));
    producerProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    producerProps.put("auto.create.topics.enable", "true");

    KafkaProducer<String, String> ipProducer = new KafkaProducer<String, String>(producerProps);

    BufferedReader br = readFile();
    String oldLine = "";
    try {
        while ((oldLine = br.readLine()) != null) {
            String line = getNewRecordWithRandomIP(oldLine).replace("[", "").replace("]", "");
            ProducerRecord ipData = new ProducerRecord<String, String>(propertyReader.getPropertyValue("topic"), line);
            Future<RecordMetadata> recordMetadata = ipProducer.send(ipData);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    ipProducer.close();
}
 
Example 4
Source Project: components   Source File: KafkaDatasetOtherDelimTestIT.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws TimeoutException {
    // there may exists other topics than these build in(configured in pom.xml) topics, but ignore them

    // ----------------- Send sample data to TOPIC_IN start --------------------
    String testID = "sampleTest" + new Random().nextInt();

    List<Person> expectedPersons = Person.genRandomList(testID, 10);

    Properties props = new Properties();
    props.put("bootstrap.servers", BOOTSTRAP_HOST);
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

    Producer<Void, String> producer = new KafkaProducer<>(props);
    for (Person person : expectedPersons) {
        ProducerRecord<Void, String> message = new ProducerRecord<>(TOPIC_IN, person.toCSV(fieldDelimiter));
        producer.send(message);
    }
    producer.close();
    // ----------------- Send sample data to TOPIC_IN end --------------------
}
 
Example 5
Source Project: javabase   Source File: KafKaProducerAPITest.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws ExecutionException, InterruptedException {
    KafkaProducer<Integer, String> producer = getProducer();
    //构造producerRecord
    ProducerRecord producerRecord=getProducerRecord();
    //普通send
    while (true) {
        Thread.sleep(500);
        //producer.send(producerRecord);
        //带callback的send
          producer.send(producerRecord,new CallBackAPI("send message"));
          log.info("----");
    }

    //send发送时候加锁,一个一个发送
    //producer.send(producerRecord).get();

   // producer.close();
}
 
Example 6
Source Project: onos   Source File: PublishManager.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void start(KafkaServerConfig config) {

    if (kafkaProducer != null) {
        log.info("Producer has already started");
        return;
    }

    String bootstrapServer =
            new StringBuilder().append(config.getIpAddress()).append(":")
                    .append(config.getPort()).toString();

    // Set Server Properties
    Properties prop = new Properties();
    prop.put("bootstrap.servers", bootstrapServer);
    prop.put("retries", config.getNumOfRetries());
    prop.put("max.in.flight.requests.per.connection",
             config.getMaxInFlightRequestsPerConnection());
    prop.put("request.required.acks", config.getAcksRequired());
    prop.put("key.serializer", config.getKeySerializer());
    prop.put("value.serializer", config.getValueSerializer());

    kafkaProducer = new KafkaProducer<>(prop);
    log.info("Kafka Producer has started.");
}
 
Example 7
Source Project: java-study   Source File: KafkaProducerUtil.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 向kafka发送批量消息
 * @param listMsg 发送的消息
 * @param url 发送的地址
 * @param topicName 消息名称
 * @param num 每次发送的条数
 * @return
 * @throws Exception 
 */
public static boolean sendMessage(List<String> listMsg,String url,String topicName,int num) throws Exception{
	KafkaProducer<String, String> producer=null;
	boolean falg=false;
	try{
		Properties props=init(url);
		producer= new KafkaProducer<String, String>(props);
		List<String> listMsg2 =new ArrayList<String>();
		for(int i = 1,j = listMsg.size();i<=j;i++){
			listMsg2.add(listMsg.get(i-1));
			if(i%num==0 || i == j){
				producer.send(new ProducerRecord<String, String>(topicName,listMsg2.toString()));
				listMsg2.clear();
			}
		}
		falg=true;
	}catch(Exception e){
		e.printStackTrace();
	}finally{
		if(producer!=null){
			producer.close();
		}
	}
	return falg;
}
 
Example 8
Source Project: kafka-webview   Source File: WebKafkaConsumerTest.java    License: MIT License 6 votes vote down vote up
public void publishDummyDataNumbers() {
    final String topic = "NumbersTopic";

    // Create publisher
    final Map<String, Object> config = new HashMap<>();
    config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class);
    config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class);
    config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");

    final KafkaProducer<Integer, Integer> producer = new KafkaProducer<>(config);
    for (int value = 0; value < 10000; value++) {
        producer.send(new ProducerRecord<>(topic, value, value));
    }
    producer.flush();
    producer.close();
}
 
Example 9
Source Project: kafka-junit   Source File: KafkaJunitRuleTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testKafkaServerIsUp() {
    try (KafkaProducer<String, String> producer = kafkaRule.helper().createStringProducer()) {
        producer.send(new ProducerRecord<>(TOPIC, "keyA", "valueA"));
    }

    try (KafkaConsumer<String, String> consumer = kafkaRule.helper().createStringConsumer()) {
        consumer.subscribe(Lists.newArrayList(TOPIC));
        ConsumerRecords<String, String> records = consumer.poll(10000);
        assertThat(records).isNotNull();
        assertThat(records.isEmpty()).isFalse();

        ConsumerRecord<String, String> msg = records.iterator().next();
        assertThat(msg).isNotNull();
        assertThat(msg.key()).isEqualTo("keyA");
        assertThat(msg.value()).isEqualTo("valueA");
    }
}
 
Example 10
Source Project: kafka-graphs   Source File: GraphUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static <K, V> void edgesToTopic(
    InputStream inputStream,
    Parser<EdgeWithValue<K, V>> edgeParser,
    Serializer<V> valueSerializer,
    Properties props,
    String topic,
    int numPartitions,
    short replicationFactor
) throws IOException {
    ClientUtils.createTopic(topic, numPartitions, replicationFactor, props);
    try (BufferedReader reader =
             new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
         Producer<Edge<K>, V> producer = new KafkaProducer<>(props, new KryoSerializer<>(), valueSerializer)) {
        String line;
        while ((line = reader.readLine()) != null) {
            EdgeWithValue<K, V> edge = edgeParser.parse(line);
            log.trace("read edge: ({}, {})", edge.source(), edge.target());
            ProducerRecord<Edge<K>, V> producerRecord =
                new ProducerRecord<>(topic, new Edge<>(edge.source(), edge.target()), edge.value());
            producer.send(producerRecord);
        }
        producer.flush();
    }
}
 
Example 11
Source Project: KafkaExample   Source File: ProducerDemo.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	Properties props = new Properties();
	props.put("bootstrap.servers", "kafka0:9092");
	props.put("acks", "all");
	props.put("retries", 3);
	props.put("batch.size", 16384);
	props.put("linger.ms", 1);
	props.put("buffer.memory", 33554432);
	props.put("key.serializer", StringSerializer.class.getName());
	props.put("value.serializer", StringSerializer.class.getName());
	props.put("partitioner.class", HashPartitioner.class.getName());
	props.put("interceptor.classes", EvenProducerInterceptor.class.getName());

	Producer<String, String> producer = new KafkaProducer<String, String>(props);
	for (int i = 0; i < 10; i++)
		producer.send(new ProducerRecord<String, String>("topic1", Integer.toString(i), Integer.toString(i)));
	producer.close();
}
 
Example 12
Source Project: iotplatform   Source File: KafkaMsgProducer.java    License: Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
  properties.put("bootstrap.servers", "127.0.0.1:9092");
  properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  properties.put("acks", "-1");
  properties.put("retries", 0);
  properties.put("batch.size", 16384);
  properties.put("linger.ms", 0);
  properties.put("buffer.memory", 33554432);
  try {
    this.producer = new KafkaProducer<>(properties);
  } catch (Exception e) {
    log.error("Failed to start kafka producer", e);
    throw new RuntimeException(e);
  }
  log.info("Kafka Producer is started....");
}
 
Example 13
Source Project: framework   Source File: KafkaClientFacotry.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @return <br>
 */
public static KafkaProducer<String, byte[]> getProducer() {
    synchronized (lock) {
        if (kafkaProducer == null) {
            Properties props = new Properties();
            props.put("bootstrap.servers", PropertyHolder.getProperty("message.kafka.bootstrap.servers"));
            props.put("acks", PropertyHolder.getProperty("message.kafka.acks", "all"));
            props.put("retries", PropertyHolder.getIntProperty("message.kafka.retries", 0));
            props.put("batch.size", PropertyHolder.getIntProperty("message.kafka.batch.size", BATCH_SIZE));
            props.put("linger.ms", PropertyHolder.getIntProperty("message.kafka.linger.ms", 1));
            props.put("buffer.memory",
                PropertyHolder.getLongProperty("message.kafka.buffer.memory", BUFFER_MEMORY));
            props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
            props.put("value.serializer", "org.apache.kafka.common.serialization.ByteArraySerializer");
            kafkaProducer = new KafkaProducer<String, byte[]>(props);
        }
    }
    return kafkaProducer;
}
 
Example 14
Source Project: kop   Source File: KafkaSSLChannelTest.java    License: Apache License 2.0 6 votes vote down vote up
public SslProducer(String topic, int port) {
    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost" + ":" + port);
    props.put(ProducerConfig.CLIENT_ID_CONFIG, "DemoKafkaOnPulsarProducerSSL");
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName());
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

    // SSL client config
    props.put("security.protocol", "SSL");
    props.put("ssl.truststore.location", "./src/test/resources/ssl/certificate/broker.truststore.jks");
    props.put("ssl.truststore.password", "broker");

    // default is https, here need to set empty.
    props.put("ssl.endpoint.identification.algorithm", "");

    producer = new KafkaProducer<>(props);
    this.topic = topic;
}
 
Example 15
Source Project: micrometer   Source File: KafkaMetricsSample.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    EphemeralKafkaBroker broker = EphemeralKafkaBroker.create();
    broker.start();
    KafkaHelper kafkaHelper = KafkaHelper.createFor(broker);

    KafkaConsumer<String, String> consumer = kafkaHelper.createStringConsumer();
    KafkaProducer<String, String> producer = kafkaHelper.createStringProducer();

    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    new KafkaClientMetrics(consumer).bindTo(registry);
    new KafkaClientMetrics(producer).bindTo(registry);

    consumer.subscribe(singletonList(TOPIC));

    Flux.interval(Duration.ofMillis(10))
            .doOnEach(n -> producer.send(new ProducerRecord<>(TOPIC, "hello", "world")))
            .subscribe();

    for (; ; ) {
        consumer.poll(Duration.ofMillis(100));
        consumer.commitAsync();
    }
}
 
Example 16
Source Project: xxhadoop   Source File: TestProducer.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    Properties properties = new Properties();
    // bin/kafka-topics.sh
    properties.put("zookeeper.connect", "node-01:2181,node-02:2181,node-03:2181");
    // kafka-console-producer.sh
    properties.put("metadata.broker.list", "node-02:9092,node-03:9092,node-04:9092");
    properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    // kafka-console-consumer.sh
    properties.put("bootstrap.servers", "node-02:9092,node-03:9092,node-04:9092");
    
    
    Producer<String, String> producer = new KafkaProducer<String, String>(properties);
    
    LOGGER.info("roduce start...");
    for (int i = 0; i < 100; i++) {
        ProducerRecord<String, String> msg = new ProducerRecord<String, String>("order-r", "name", "Hello_XXX_" + i);
        producer.send(msg);
    }
    producer.close();
    LOGGER.info("produce end...");
}
 
Example 17
Source Project: pulsar   Source File: KafkaApiTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleProducer() throws Exception {
    String topic = "testSimpleProducer";

    @Cleanup
    PulsarClient pulsarClient = PulsarClient.builder().serviceUrl(getPlainTextServiceUrl()).build();
    org.apache.pulsar.client.api.Consumer<byte[]> pulsarConsumer = pulsarClient.newConsumer().topic(topic)
            .subscriptionName("my-subscription")
            .subscribe();

    Properties props = new Properties();
    props.put("bootstrap.servers", getPlainTextServiceUrl());

    props.put("key.serializer", IntegerSerializer.class.getName());
    props.put("value.serializer", StringSerializer.class.getName());

    Producer<Integer, String> producer = new KafkaProducer<>(props);

    for (int i = 0; i < 10; i++) {
        producer.send(new ProducerRecord<Integer, String>(topic, i, "hello-" + i));
    }

    producer.flush();
    producer.close();

    for (int i = 0; i < 10; i++) {
        Message<byte[]> msg = pulsarConsumer.receive(1, TimeUnit.SECONDS);
        assertEquals(new String(msg.getData()), "hello-" + i);
        pulsarConsumer.acknowledge(msg);
    }
}
 
Example 18
Source Project: beam   Source File: KafkaCSVTableIT.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("FutureReturnValueIgnored")
private void produceSomeRecords(int num) {
  Producer<String, String> producer = new KafkaProducer<String, String>(producerProps());
  String topicName = pipeline.getOptions().as(KafkaOptions.class).getKafkaTopic();
  for (int i = 0; i < num; i++) {
    producer.send(
        new ProducerRecord<String, String>(
            topicName, "k" + i, i + "," + ((i % 3) + 1) + "," + i));
  }
  producer.flush();
  producer.close();
}
 
Example 19
Source Project: datacollector   Source File: MapRStreamsValidationUtil09.java    License: Apache License 2.0 5 votes vote down vote up
protected Producer<String, String> createProducerTopicMetadataClient(Map<String, Object> kafkaClientConfigs) {
  Properties props = new Properties();
  props.put(ProducerConfig.CLIENT_ID_CONFIG, "topicMetadataClient");
  props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, ORG_APACHE_KAFKA_COMMON_SERIALIZATION_STRING_SERIALIZER);
  props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ORG_APACHE_KAFKA_COMMON_SERIALIZATION_STRING_SERIALIZER);

  // Check if user has configured 'max.block.ms' option, otherwise wait for 60 seconds to fetch metadata
  if (kafkaClientConfigs != null && kafkaClientConfigs.containsKey(STREAMS_RPC_TIMEOUT_MS)) {
    props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, kafkaClientConfigs.get(STREAMS_RPC_TIMEOUT_MS));
  } else {
    props.put(STREAMS_RPC_TIMEOUT_MS, 60000);
  }
  return new KafkaProducer<>(props);
}
 
Example 20
Source Project: DBus   Source File: DbusHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static Producer getProducer(Properties props) throws Exception {
    if (producerMap.containsKey(props)) {
        return producerMap.get(props);
    } else {
        Producer producer = new KafkaProducer<>(props);
        producerMap.put(props, producer);
        return producer;
    }
}
 
Example 21
Source Project: DDMQ   Source File: KafkaClusterProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void shutdown() {
    LOGGER.info("start closing kafka producer, broker cluster:{}", brokerCluster);
    for (KafkaProducer<String, byte[]> kafkaProducer : kafkaProducers) {
        kafkaProducer.close();
    }
}
 
Example 22
Source Project: canal-1.1.3   Source File: CanalKafkaProducer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void init(MQProperties kafkaProperties) {
    this.kafkaProperties = kafkaProperties;
    Properties properties = new Properties();
    properties.put("bootstrap.servers", kafkaProperties.getServers());
    properties.put("acks", kafkaProperties.getAcks());
    properties.put("compression.type", kafkaProperties.getCompressionType());
    properties.put("batch.size", kafkaProperties.getBatchSize());
    properties.put("linger.ms", kafkaProperties.getLingerMs());
    properties.put("max.request.size", kafkaProperties.getMaxRequestSize());
    properties.put("buffer.memory", kafkaProperties.getBufferMemory());
    properties.put("key.serializer", StringSerializer.class.getName());
    properties.put("max.in.flight.requests.per.connection", 1);

    if (!kafkaProperties.getProperties().isEmpty()) {
        properties.putAll(kafkaProperties.getProperties());
    }

    if (kafkaProperties.getTransaction()) {
        properties.put("transactional.id", "canal-transactional-id");
    } else {
        properties.put("retries", kafkaProperties.getRetries());
    }
    if (!kafkaProperties.getFlatMessage()) {
        properties.put("value.serializer", MessageSerializer.class.getName());
        producer = new KafkaProducer<String, Message>(properties);
    } else {
        properties.put("value.serializer", StringSerializer.class.getName());
        producer2 = new KafkaProducer<String, String>(properties);
    }
    if (kafkaProperties.getTransaction()) {
        if (!kafkaProperties.getFlatMessage()) {
            producer.initTransactions();
        } else {
            producer2.initTransactions();
        }
    }
}
 
Example 23
Source Project: jstarcraft-core   Source File: KafkaEventChannel.java    License: Apache License 2.0 5 votes vote down vote up
public KafkaEventChannel(EventMode mode, String name, String connections, ContentCodec codec) {
    super(mode, name);
    this.connections = connections;
    this.codec = codec;
    Properties properties = new Properties();
    properties.put("bootstrap.servers", connections);
    properties.put("key.serializer", keySerializer);
    properties.put("value.serializer", valueSerializer);
    KafkaProducer producer = new KafkaProducer(properties);
    this.producer = producer;
    this.consumers = new ConcurrentHashMap<>();
    this.threads = new ConcurrentHashMap<>();
}
 
Example 24
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void wrapIterator(@Nullable @Advice.Return(readOnly = false) Iterator<ConsumerRecord> iterator) {
    if (tracer == null || !tracer.isRunning() || tracer.currentTransaction() != null) {
        return;
    }

    //noinspection ConstantConditions,rawtypes
    KafkaInstrumentationHeadersHelper<ConsumerRecord, ProducerRecord> kafkaInstrumentationHelper =
        kafkaInstrHeadersHelperManager.getForClassLoaderOfClass(KafkaProducer.class);
    if (iterator != null && kafkaInstrumentationHelper != null) {
        iterator = kafkaInstrumentationHelper.wrapConsumerRecordIterator(iterator);
    }
}
 
Example 25
Source Project: ranger   Source File: KafkaRangerAuthorizerSASLSSLTest.java    License: Apache License 2.0 5 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, "SASL_SSL");
    producerProps.put("sasl.mechanism", "PLAIN");
    
    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 26
Source Project: stream-registry   Source File: KafkaEventSender.java    License: Apache License 2.0 5 votes vote down vote up
public KafkaEventSender(Config config, EventCorrelator correlator) {
  this(
      config,
      correlator == null ? new NullCorrelationStrategy() : new CorrelationStrategyImpl(correlator),
      new AvroConverter(),
      new KafkaProducer<>(producerConfig(config))
  );
}
 
Example 27
Source Project: java-kafka-client   Source File: TracingKafkaTest.java    License: Apache License 2.0 5 votes vote down vote up
private Producer<Integer, String> createProducerWithDecorators(
    Collection<SpanDecorator> spanDecorators) {
  Map<String, Object> senderProps = KafkaTestUtils
      .producerProps(embeddedKafka.getEmbeddedKafka());
  KafkaProducer kafkaProducer = new KafkaProducer<>(senderProps);
  TracingKafkaProducerBuilder tracingKafkaProducerBuilder =
      new TracingKafkaProducerBuilder<>(kafkaProducer, mockTracer);
  if (spanDecorators != null) {
    tracingKafkaProducerBuilder = tracingKafkaProducerBuilder.withDecorators(spanDecorators);
  }

  return tracingKafkaProducerBuilder.build();
}
 
Example 28
Source Project: kafka-streams   Source File: TwitterDataSource.java    License: Apache License 2.0 5 votes vote down vote up
private  Producer<String, String> getKafkaProducer() {
    Properties props = new Properties();
    props.put("bootstrap.servers", "localhost:9092");
    props.put("acks", "all");
    props.put("retries", 0);
    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");

    return new KafkaProducer<>(props);
}
 
Example 29
Source Project: KafkaExample   Source File: ProducerDemoCallback.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
		Properties props = new Properties();
		props.put("bootstrap.servers", "kafka0:9092");
		props.put("acks", "all");
		props.put("retries", 3);
		props.put("batch.size", 16384);
		props.put("linger.ms", 1);
		props.put("buffer.memory", 33554432);
		props.put("key.serializer", StringSerializer.class.getName());
		props.put("value.serializer", StringSerializer.class.getName());
		props.put("partitioner.class", HashPartitioner.class.getName());

		Producer<String, String> producer = new KafkaProducer<String, String>(props);
		for (int i = 0; i < 10; i++) {
			ProducerRecord record = new ProducerRecord<String, String>("topic1", Integer.toString(i),
					Integer.toString(i));
//			producer.send(record);
//			producer.send(record, new Callback() {
//
//				@Override
//				public void onCompletion(RecordMetadata metadata, Exception exception) {
//					System.out.printf("Send record partition:%d, offset:%d, keysize:%d, valuesize:%d %n",
//							metadata.partition(), metadata.offset(), metadata.serializedKeySize(),
//							metadata.serializedValueSize());
//				}
//
//			});
			 producer.send(record, (metadata, exception) -> {
				 if(metadata != null) {
					 System.out.printf("Send record partition:%d, offset:%d, keysize:%d, valuesize:%d %n",
								metadata.partition(), metadata.offset(), metadata.serializedKeySize(),
								metadata.serializedValueSize());
				 }
				 if(exception != null) {
					 exception.printStackTrace();
				 }
			 });
		}
		producer.close();
	}
 
Example 30
Source Project: DBus   Source File: KafkaContainer.java    License: Apache License 2.0 5 votes vote down vote up
public Producer getProducer(Properties props) {
    if (producerMap.containsKey(props)) {
        return producerMap.get(props);
    } else {
        Producer producer = new KafkaProducer<>(props);
        producerMap.put(props, producer);
        return producer;
    }
}