Search in sources :

Example 1 with NoOpConsumerRebalanceListener

use of org.apache.kafka.clients.consumer.internals.NoOpConsumerRebalanceListener in project open-kilda by telstra.

the class KafkaUtils method getStateDumpsFromBolts.

public DumpStateManager getStateDumpsFromBolts() {
    long timestamp = System.currentTimeMillis();
    String correlationId = String.format("atdd-%d", timestamp);
    CtrlRequest dumpRequest = new CtrlRequest("*", new RequestData("dump"), timestamp, correlationId, WFM_CTRL);
    try {
        RecordMetadata postedMessage = postMessage(settings.getControlTopic(), dumpRequest);
        KafkaConsumer<String, String> consumer = createConsumer();
        try {
            consumer.subscribe(Collections.singletonList(settings.getControlTopic()), new NoOpConsumerRebalanceListener() {

                @Override
                public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
                    System.out.println("Seek to offset: " + postedMessage.offset());
                    for (TopicPartition topicPartition : partitions) {
                        consumer.seek(topicPartition, postedMessage.offset());
                    }
                }
            });
            List<CtrlResponse> buffer = new ArrayList<>();
            final int BOLT_COUNT = 4;
            final int NUMBER_OF_ATTEMPTS = 5;
            int attempt = 0;
            while (buffer.size() < BOLT_COUNT && attempt++ < NUMBER_OF_ATTEMPTS) {
                for (ConsumerRecord<String, String> record : consumer.poll(1000)) {
                    System.out.println("Received message: (" + record.key() + ", " + record.value() + ") at offset " + record.offset());
                    Message message = MAPPER.readValue(record.value(), Message.class);
                    if (message.getDestination() == CTRL_CLIENT && message.getCorrelationId().equals(correlationId)) {
                        buffer.add((CtrlResponse) message);
                    }
                }
            }
            return DumpStateManager.fromResponsesList(buffer);
        } finally {
            consumer.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : Message(org.openkilda.messaging.Message) CtrlRequest(org.openkilda.messaging.ctrl.CtrlRequest) ArrayList(java.util.ArrayList) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CtrlResponse(org.openkilda.messaging.ctrl.CtrlResponse) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) RequestData(org.openkilda.messaging.ctrl.RequestData) TopicPartition(org.apache.kafka.common.TopicPartition) NoOpConsumerRebalanceListener(org.apache.kafka.clients.consumer.internals.NoOpConsumerRebalanceListener)

Example 2 with NoOpConsumerRebalanceListener

use of org.apache.kafka.clients.consumer.internals.NoOpConsumerRebalanceListener in project kafka by apache.

the class KafkaConsumerTest method testSubscriptionOnNullPattern.

@Test(expected = IllegalArgumentException.class)
public void testSubscriptionOnNullPattern() {
    KafkaConsumer<byte[], byte[]> consumer = newConsumer();
    Pattern pattern = null;
    try {
        consumer.subscribe(pattern, new NoOpConsumerRebalanceListener());
    } finally {
        consumer.close();
    }
}
Also used : Pattern(java.util.regex.Pattern) NoOpConsumerRebalanceListener(org.apache.kafka.clients.consumer.internals.NoOpConsumerRebalanceListener) Test(org.junit.Test)

Aggregations

NoOpConsumerRebalanceListener (org.apache.kafka.clients.consumer.internals.NoOpConsumerRebalanceListener)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Pattern (java.util.regex.Pattern)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 Test (org.junit.Test)1 Message (org.openkilda.messaging.Message)1 CtrlRequest (org.openkilda.messaging.ctrl.CtrlRequest)1 CtrlResponse (org.openkilda.messaging.ctrl.CtrlResponse)1 RequestData (org.openkilda.messaging.ctrl.RequestData)1