Search in sources :

Example 1 with NotificationException

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

the class KafkaNotification method sendInternalToProducer.

@VisibleForTesting
void sendInternalToProducer(Producer p, NotificationType type, String[] messages) throws NotificationException {
    String topic = TOPIC_MAP.get(type);
    List<MessageContext> messageContexts = new ArrayList<>();
    for (String message : messages) {
        ProducerRecord record = new ProducerRecord(topic, message);
        LOG.debug("Sending message for topic {}: {}", topic, message);
        Future future = p.send(record);
        messageContexts.add(new MessageContext(future, message));
    }
    List<String> failedMessages = new ArrayList<>();
    Exception lastFailureException = null;
    for (MessageContext context : messageContexts) {
        try {
            RecordMetadata response = context.getFuture().get();
            LOG.debug("Sent message for topic - {}, partition - {}, offset - {}", response.topic(), response.partition(), response.offset());
        } catch (Exception e) {
            lastFailureException = e;
            failedMessages.add(context.getMessage());
        }
    }
    if (lastFailureException != null) {
        throw new NotificationException(lastFailureException, failedMessages);
    }
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ArrayList(java.util.ArrayList) NotificationException(org.apache.atlas.notification.NotificationException) Future(java.util.concurrent.Future) URISyntaxException(java.net.URISyntaxException) NotificationException(org.apache.atlas.notification.NotificationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) AtlasException(org.apache.atlas.AtlasException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with NotificationException

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

the class AtlasHookTest method testFailedMessageIsLoggedIfRequired.

@Test
public void testFailedMessageIsLoggedIfRequired() throws NotificationException {
    List<HookNotification.HookNotificationMessage> hookNotificationMessages = new ArrayList<HookNotification.HookNotificationMessage>() {

        {
            add(new HookNotification.EntityCreateRequest("user"));
        }
    };
    doThrow(new NotificationException(new Exception(), Arrays.asList("test message"))).when(notificationInterface).send(NotificationInterface.NotificationType.HOOK, hookNotificationMessages);
    AtlasHook.notifyEntitiesInternal(hookNotificationMessages, 2, notificationInterface, true, failedMessagesLogger);
    verify(failedMessagesLogger, times(1)).log("test message");
}
Also used : HookNotification(org.apache.atlas.notification.hook.HookNotification) ArrayList(java.util.ArrayList) NotificationException(org.apache.atlas.notification.NotificationException) NotificationException(org.apache.atlas.notification.NotificationException) Test(org.testng.annotations.Test)

Example 3 with NotificationException

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

the class AtlasHookTest method testFailedMessageIsNotLoggedIfNotRequired.

@Test
public void testFailedMessageIsNotLoggedIfNotRequired() throws NotificationException {
    List<HookNotification.HookNotificationMessage> hookNotificationMessages = new ArrayList<>();
    doThrow(new NotificationException(new Exception(), Arrays.asList("test message"))).when(notificationInterface).send(NotificationInterface.NotificationType.HOOK, hookNotificationMessages);
    AtlasHook.notifyEntitiesInternal(hookNotificationMessages, 2, notificationInterface, false, failedMessagesLogger);
    verifyZeroInteractions(failedMessagesLogger);
}
Also used : ArrayList(java.util.ArrayList) NotificationException(org.apache.atlas.notification.NotificationException) NotificationException(org.apache.atlas.notification.NotificationException) Test(org.testng.annotations.Test)

Example 4 with NotificationException

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

the class AtlasHookTest method testNotifyEntitiesRetriesOnException.

@Test
public void testNotifyEntitiesRetriesOnException() throws NotificationException {
    List<HookNotification.HookNotificationMessage> hookNotificationMessages = new ArrayList<HookNotification.HookNotificationMessage>() {

        {
            add(new HookNotification.EntityCreateRequest("user"));
        }
    };
    doThrow(new NotificationException(new Exception())).when(notificationInterface).send(NotificationInterface.NotificationType.HOOK, hookNotificationMessages);
    AtlasHook.notifyEntitiesInternal(hookNotificationMessages, 2, notificationInterface, false, failedMessagesLogger);
    verify(notificationInterface, times(2)).send(NotificationInterface.NotificationType.HOOK, hookNotificationMessages);
}
Also used : HookNotification(org.apache.atlas.notification.hook.HookNotification) ArrayList(java.util.ArrayList) NotificationException(org.apache.atlas.notification.NotificationException) NotificationException(org.apache.atlas.notification.NotificationException) Test(org.testng.annotations.Test)

Example 5 with NotificationException

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

the class KafkaNotificationMockTest method shouldCollectAllFailedMessagesIfProducerFails.

@Test
@SuppressWarnings("unchecked")
public void shouldCollectAllFailedMessagesIfProducerFails() throws NotificationException, ExecutionException, InterruptedException {
    Properties configProperties = mock(Properties.class);
    KafkaNotification kafkaNotification = new KafkaNotification(configProperties);
    Producer producer = mock(Producer.class);
    String topicName = kafkaNotification.getTopicName(NotificationInterface.NotificationType.HOOK);
    String message1 = "This is a test message1";
    String message2 = "This is a test message2";
    Future returnValue1 = mock(Future.class);
    when(returnValue1.get()).thenThrow(new RuntimeException("Simulating exception"));
    Future returnValue2 = mock(Future.class);
    when(returnValue2.get()).thenThrow(new RuntimeException("Simulating exception"));
    ProducerRecord expectedRecord1 = new ProducerRecord(topicName, message1);
    when(producer.send(expectedRecord1)).thenReturn(returnValue1);
    ProducerRecord expectedRecord2 = new ProducerRecord(topicName, message2);
    when(producer.send(expectedRecord2)).thenReturn(returnValue1);
    try {
        kafkaNotification.sendInternalToProducer(producer, NotificationInterface.NotificationType.HOOK, new String[] { message1, message2 });
        fail("Should have thrown NotificationException");
    } catch (NotificationException e) {
        assertEquals(e.getFailedMessages().size(), 2);
        assertEquals(e.getFailedMessages().get(0), "This is a test message1");
        assertEquals(e.getFailedMessages().get(1), "This is a test message2");
    }
}
Also used : Producer(org.apache.kafka.clients.producer.Producer) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) NotificationException(org.apache.atlas.notification.NotificationException) Future(java.util.concurrent.Future) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Aggregations

NotificationException (org.apache.atlas.notification.NotificationException)8 Test (org.testng.annotations.Test)7 ArrayList (java.util.ArrayList)6 Future (java.util.concurrent.Future)3 HookNotification (org.apache.atlas.notification.hook.HookNotification)3 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)3 Properties (java.util.Properties)2 Producer (org.apache.kafka.clients.producer.Producer)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 AtlasException (org.apache.atlas.AtlasException)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1