Search in sources :

Example 1 with NotificationConsumer

use of org.apache.atlas.notification.NotificationConsumer in project incubator-atlas by apache.

the class KafkaNotification method createConsumers.

@VisibleForTesting
public <T> List<NotificationConsumer<T>> createConsumers(NotificationType notificationType, int numConsumers, boolean autoCommitEnabled) {
    String topic = TOPIC_MAP.get(notificationType);
    Properties consumerProperties = getConsumerProperties(notificationType);
    List<NotificationConsumer<T>> consumers = new ArrayList<>(numConsumers);
    for (int i = 0; i < numConsumers; i++) {
        ConsumerConnector consumerConnector = createConsumerConnector(consumerProperties);
        Map<String, Integer> topicCountMap = new HashMap<>();
        topicCountMap.put(topic, 1);
        StringDecoder decoder = new StringDecoder(null);
        Map<String, List<KafkaStream<String, String>>> streamsMap = consumerConnector.createMessageStreams(topicCountMap, decoder, decoder);
        List<KafkaStream<String, String>> kafkaConsumers = streamsMap.get(topic);
        for (KafkaStream stream : kafkaConsumers) {
            KafkaConsumer<T> kafkaConsumer = createKafkaConsumer(notificationType.getClassType(), notificationType.getDeserializer(), stream, i, consumerConnector, autoCommitEnabled);
            consumers.add(kafkaConsumer);
        }
        consumerConnectors.add(consumerConnector);
    }
    return consumers;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringDecoder(kafka.serializer.StringDecoder) ConsumerConnector(kafka.javaapi.consumer.ConsumerConnector) KafkaStream(kafka.consumer.KafkaStream) Properties(java.util.Properties) ApplicationProperties(org.apache.atlas.ApplicationProperties) NotificationConsumer(org.apache.atlas.notification.NotificationConsumer) ArrayList(java.util.ArrayList) List(java.util.List) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with NotificationConsumer

use of org.apache.atlas.notification.NotificationConsumer in project incubator-atlas by apache.

the class KafkaNotificationMockTest method testCreateConsumers.

@Test
@SuppressWarnings("unchecked")
public void testCreateConsumers() throws Exception {
    Properties properties = mock(Properties.class);
    when(properties.getProperty("entities.group.id")).thenReturn("atlas");
    final ConsumerConnector consumerConnector = mock(ConsumerConnector.class);
    Map<String, Integer> topicCountMap = new HashMap<>();
    topicCountMap.put(KafkaNotification.ATLAS_ENTITIES_TOPIC, 1);
    Map<String, List<KafkaStream<String, String>>> kafkaStreamsMap = new HashMap<>();
    List<KafkaStream<String, String>> kafkaStreams = new ArrayList<>();
    KafkaStream kafkaStream = mock(KafkaStream.class);
    kafkaStreams.add(kafkaStream);
    kafkaStreamsMap.put(KafkaNotification.ATLAS_ENTITIES_TOPIC, kafkaStreams);
    when(consumerConnector.createMessageStreams(eq(topicCountMap), any(StringDecoder.class), any(StringDecoder.class))).thenReturn(kafkaStreamsMap);
    final KafkaConsumer consumer1 = mock(KafkaConsumer.class);
    final KafkaConsumer consumer2 = mock(KafkaConsumer.class);
    KafkaNotification kafkaNotification = new TestKafkaNotification(properties, consumerConnector, consumer1, consumer2);
    List<NotificationConsumer<String>> consumers = kafkaNotification.createConsumers(NotificationInterface.NotificationType.ENTITIES, 2);
    verify(consumerConnector, times(2)).createMessageStreams(eq(topicCountMap), any(StringDecoder.class), any(StringDecoder.class));
    assertEquals(consumers.size(), 2);
    assertTrue(consumers.contains(consumer1));
    assertTrue(consumers.contains(consumer2));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringDecoder(kafka.serializer.StringDecoder) ConsumerConnector(kafka.javaapi.consumer.ConsumerConnector) KafkaStream(kafka.consumer.KafkaStream) Properties(java.util.Properties) NotificationConsumer(org.apache.atlas.notification.NotificationConsumer) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Properties (java.util.Properties)2 KafkaStream (kafka.consumer.KafkaStream)2 ConsumerConnector (kafka.javaapi.consumer.ConsumerConnector)2 StringDecoder (kafka.serializer.StringDecoder)2 NotificationConsumer (org.apache.atlas.notification.NotificationConsumer)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ApplicationProperties (org.apache.atlas.ApplicationProperties)1 Test (org.testng.annotations.Test)1