|
| 1 | +package Client; |
| 2 | + |
| 3 | +import MySerdes.ValueSerde; |
| 4 | +import Structure.EventVal; |
| 5 | +import Structure.SubscribeVal; |
| 6 | +import org.apache.kafka.clients.consumer.ConsumerRecord; |
| 7 | +import org.apache.kafka.clients.consumer.ConsumerRecords; |
| 8 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 9 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 10 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 11 | +import org.apache.kafka.common.errors.WakeupException; |
| 12 | +import utils.InfluxdbUtil; |
| 13 | + |
| 14 | +import java.io.*; |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.Properties; |
| 17 | + |
| 18 | +public class Consumer { |
| 19 | + |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + if(args.length < 2){ |
| 23 | + System.out.println("Usage: Consumer -matchconfigfile -influxdbconfigfile "); |
| 24 | + System.exit(1); |
| 25 | + } |
| 26 | + |
| 27 | + String config_filename = ""; |
| 28 | + String influx_filename = ""; |
| 29 | + try{ |
| 30 | + config_filename = args[0]; |
| 31 | + influx_filename = args[1]; |
| 32 | + } catch (Throwable e){ |
| 33 | + System.out.println("Usage: Consumer -matchconfigfile -influxdbconfigfile "); |
| 34 | + e.printStackTrace(); |
| 35 | + System.exit(1); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + // read consumer config file |
| 40 | + Properties properties = new Properties(); |
| 41 | + try { |
| 42 | + InputStream inputStream = new FileInputStream(new File(config_filename)); |
| 43 | + properties.load(inputStream); |
| 44 | + } catch (FileNotFoundException e) { |
| 45 | + System.err.println("properties file open failed!"); |
| 46 | + e.printStackTrace(); |
| 47 | + } catch (IOException e) { |
| 48 | + System.err.println("properties file read failed"); |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + String KafkaServer = properties.getProperty("KafkaServer"); |
| 52 | + String clientId = properties.getProperty("clientId"); |
| 53 | + String securityProto = properties.getProperty("security.protocol"); |
| 54 | + String saslName = properties.getProperty("sasl.kerberos.service.name"); |
| 55 | + |
| 56 | + // define the variable of kafka connection prop |
| 57 | + Properties props = new Properties(); |
| 58 | + props.put("bootstrap.servers", KafkaServer); |
| 59 | + props.put("group.id", "Consumer"); |
| 60 | + props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); |
| 61 | + props.put("value.deserializer", ValueSerde.EventValDeserde.class.getName()); |
| 62 | + if(securityProto != null && saslName != null){ |
| 63 | + props.put("security.protocol", securityProto); |
| 64 | + props.put("sasl.kerberos.service.name", saslName); |
| 65 | + } else { |
| 66 | + System.out.println("WARNING: no authentication configuration"); |
| 67 | +// System.exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + // create consumer |
| 71 | + KafkaConsumer<String, EventVal> consumer = new KafkaConsumer<>(props); |
| 72 | + // consumer subscribe |
| 73 | + consumer.subscribe(Arrays.asList(clientId)); |
| 74 | + try{ |
| 75 | + sendSubscribe(properties); |
| 76 | + } catch (Throwable e) { |
| 77 | + System.out.println("Sent subscribe error\n"); |
| 78 | + System.exit(1); |
| 79 | + } |
| 80 | + InfluxdbUtil influx = InfluxdbUtil.setUp(influx_filename,"client-test"); |
| 81 | + |
| 82 | + //TopicPartition topicPartition = new TopicPartition("", 0); |
| 83 | + |
| 84 | + // attach shutdown handler to catch control-c |
| 85 | + Runtime.getRuntime().addShutdownHook(new Thread("consumer-shutdown-hook") { |
| 86 | + @Override |
| 87 | + public void run() { |
| 88 | + consumer.wakeup(); |
| 89 | + //latch.countDown(); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | +// BufferedWriter bw = null; |
| 94 | + try { |
| 95 | +// File file = new File("src/main/resources/Stream-data/rcv-time.txt"); |
| 96 | +// FileWriter fw = new FileWriter(file, true); |
| 97 | +// bw = new BufferedWriter(fw); |
| 98 | + |
| 99 | + long tmpArriveTime = 0; |
| 100 | + double aver_delay_time = 0; |
| 101 | + int x = 0; |
| 102 | + // loop to poll events |
| 103 | + while(true) { |
| 104 | + ConsumerRecords<String, EventVal> records = consumer.poll(70); |
| 105 | + |
| 106 | + /** ATTENTION: not accurate rcv time, for accurate, JMX shoudl be used**/ |
| 107 | + tmpArriveTime = System.currentTimeMillis(); |
| 108 | + // traverse all records |
| 109 | + for(ConsumerRecord<String, EventVal> record : records) { |
| 110 | + // decode to get EventVal object |
| 111 | + EventVal eVal = record.value(); |
| 112 | + eVal.EventGetTime = tmpArriveTime; |
| 113 | + long tmpDelayTime = tmpArriveTime - eVal.EventProduceTime; |
| 114 | + |
| 115 | + //save rcv_time |
| 116 | + String s = eVal.StockId + " " |
| 117 | + + eVal.EventArriveTime + " " |
| 118 | + + eVal.EventMatchTime + " " |
| 119 | + + eVal.EventGetTime + " " |
| 120 | + + tmpDelayTime; |
| 121 | + |
| 122 | +// bw.write(s + "\n"); |
| 123 | + |
| 124 | + influx.consumerInsert(eVal); |
| 125 | + |
| 126 | + } |
| 127 | + } |
| 128 | + } catch (WakeupException e) { |
| 129 | + |
| 130 | + } catch (Throwable e) { |
| 131 | + System.exit(1); |
| 132 | + } finally { |
| 133 | +// try { |
| 134 | +// bw.close(); |
| 135 | +// }catch (Exception e){ |
| 136 | +// e.printStackTrace(); |
| 137 | +// } |
| 138 | + consumer.close(); |
| 139 | + } |
| 140 | + System.exit(0); |
| 141 | + } |
| 142 | + |
| 143 | + //client's send subscribe func |
| 144 | + public static int sendSubscribe(Properties properties) { |
| 145 | + String securityProto = properties.getProperty("security.protocol"); |
| 146 | + String saslName = properties.getProperty("sasl.kerberos.service.name"); |
| 147 | + |
| 148 | + Properties Props = new Properties(); |
| 149 | + Props.put("bootstrap.servers", properties.getProperty("KafkaServer")); |
| 150 | + Props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); |
| 151 | + Props.put("value.serializer", ValueSerde.SubValSerde.class.getName()); |
| 152 | + if(securityProto != null && saslName != null){ |
| 153 | + Props.put("security.protocol", securityProto); |
| 154 | + Props.put("sasl.kerberos.service.name", saslName); |
| 155 | + } else { |
| 156 | + System.out.println("WARNING: no authentication configuration"); |
| 157 | + } |
| 158 | + |
| 159 | + KafkaProducer<String, SubscribeVal> producer = new KafkaProducer<>(Props); |
| 160 | + |
| 161 | + String attrs[] = properties.getProperty("attributes").split(","); |
| 162 | + System.out.println(attrs[0]); |
| 163 | + System.out.println(attrs[1]); |
| 164 | + System.out.println(attrs[2]); |
| 165 | + |
| 166 | + |
| 167 | + SubscribeVal sVal = new SubscribeVal(attrs.length); |
| 168 | + sVal.SubId = properties.getProperty("clientId"); |
| 169 | +// sVal.sub_num_id = properties.getProperty("Client" + "clientId"); |
| 170 | + sVal.StockId = Integer.parseInt(properties.getProperty("stockId")); |
| 171 | + for(int j = 0; j < sVal.AttributeNum; j++){ |
| 172 | + String nums_str[] = attrs[j].split(" "); |
| 173 | + sVal.subVals.get(j).attributeId = Integer.parseInt(nums_str[0]); |
| 174 | + sVal.subVals.get(j).min_val = Double.parseDouble(nums_str[1]); |
| 175 | + sVal.subVals.get(j).max_val = Double.parseDouble(nums_str[2]); |
| 176 | + } |
| 177 | + //Record |
| 178 | + ProducerRecord<String, SubscribeVal> record = new ProducerRecord<>("Sub", sVal); |
| 179 | + //send |
| 180 | + try { |
| 181 | + System.out.println(record); |
| 182 | + producer.send(record).get(); |
| 183 | + //System.err.println("Producer Send " + i + " Success!"); |
| 184 | + //Thread.sleep(stime[st]); |
| 185 | + } catch (Exception e) { |
| 186 | + e.printStackTrace(); |
| 187 | + return 1; |
| 188 | + } |
| 189 | + |
| 190 | + producer.close(); |
| 191 | + |
| 192 | + return 0; |
| 193 | + } |
| 194 | +} |
0 commit comments