Open Kilda Java Documentation
WhateverTest.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm;
17 
18 import org.apache.kafka.clients.consumer.ConsumerRecord;
19 import org.apache.kafka.clients.consumer.ConsumerRecords;
20 import org.apache.kafka.clients.consumer.KafkaConsumer;
21 import org.apache.kafka.common.utils.Utils;
22 
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Properties;
27 
31 public class WhateverTest {
32 
33  public static void main(String[] args) {
34  List<String> results = new ArrayList<>();
35 
36  Properties props = new Properties();
37  props.put("bootstrap.servers", "localhost:9092");
38  props.put("group.id", "test");
39  props.put("enable.auto.commit", "true");
40  props.put("auto.commit.interval.ms", "1000");
41  props.put("session.timeout.ms", "15000");
42  props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
43  props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
44  KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
45  String topic = "speaker.info.switch.updown";
46  consumer.subscribe(Arrays.asList(topic));
47 // System.out.println("partitions = " + consumer.partitionsFor(topic).size());
48 // PartitionInfo info = consumer.partitionsFor(topic).get(0);
49 // TopicPartition part = new TopicPartition(topic,info.partition());
50 // System.out.println("looking at " + info.topic() + " part " + info.partition());
51 // consumer.seekToBeginning(Arrays.asList(part));
52  System.out.println("consumer.listTopics() = " + consumer.listTopics());
53 
54  System.out.println("");
55  for (int i = 0; i < 10; i++) {
56  ConsumerRecords<String, String> records = consumer.poll(500);
57  System.out.println(".");
58  for (ConsumerRecord<String, String> record : records) {
59  System.out.printf("offset = %d, key = %s, value = %s", record.offset(), record.key(), record.value());
60  System.out.print("$");
61  results.add(record.value());
62  }
63  Utils.sleep(1000);
64  }
65  System.out.println("");
66  consumer.close();
67 
68  }
69 }
static void main(String[] args)