@Override public void shutdown() { log.debug("Starting shutdown of KafkaStoreReaderThread."); super.initiateShutdown(); if (consumer != null) { consumer.wakeup(); } if (localStore != null) { localStore.close(); } super.awaitShutdown(); consumer.close(); log.info("KafkaStoreReaderThread shutdown complete."); }
@Override public void doWork() { try { ConsumerRecords<byte[], byte[]> records = consumer.poll(Long.MAX_VALUE); for (ConsumerRecord<byte[], byte[]> record : records) { K messageKey = null; try { messageKey = this.serializer.deserializeKey(record.key()); } catch (SerializationException e) { log.error("Failed to deserialize the schema or config key", e); continue; } if (messageKey.equals(noopKey)) { // If it's a noop, update local offset counter and do nothing else try { offsetUpdateLock.lock(); offsetInSchemasTopic = record.offset(); offsetReachedThreshold.signalAll(); } finally { offsetUpdateLock.unlock(); } } else { V message = null; try { message = record.value() == null ? null : serializer.deserializeValue(messageKey, record.value()); } catch (SerializationException e) { log.error("Failed to deserialize a schema or config update", e); continue; } try { log.trace( "Applying update (" + messageKey + "," + message + ") to the local " + "store"); if (message == null) { localStore.delete(messageKey); } else { localStore.put(messageKey, message); } this.storeUpdateHandler.handleUpdate(messageKey, message); try { offsetUpdateLock.lock(); offsetInSchemasTopic = record.offset(); offsetReachedThreshold.signalAll(); } finally { offsetUpdateLock.unlock(); } } catch (StoreException se) { log.error("Failed to add record from the Kafka topic" + topic + " the local store"); } } } } catch (WakeupException we) { // do nothing because the thread is closing -- see shutdown() } catch (RecordTooLargeException rtle) { throw new IllegalStateException( "Consumer threw RecordTooLargeException. A schema has been written that " + "exceeds the default maximum fetch size.", rtle); } catch (RuntimeException e) { log.error("KafkaStoreReader thread has died for an unknown reason."); throw new RuntimeException(e); } }