All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.azure.eventhubs.impl.EventDataUtil Maven / Gradle / Ivy

Go to download

Please note, a newer package azure-messaging-eventhubs for Azure Event Hubs is available at https://search.maven.org/artifact/com.azure/azure-messaging-eventhubs as of February 2020. While this package will continue to receive critical bug fixes, we strongly encourage you to upgrade. Read the migration guide at https://aka.ms/azsdk/java/migrate/eh for more details.

There is a newer version: 3.3.0
Show newest version
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.microsoft.azure.eventhubs.impl;

import com.microsoft.azure.eventhubs.EventData;
import com.microsoft.azure.eventhubs.EventPosition;
import org.apache.qpid.proton.message.Message;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.function.Consumer;

final class EventDataUtil {

    @SuppressWarnings("serial")
    static final Set RESERVED_SYSTEM_PROPERTIES = Collections.unmodifiableSet(new HashSet() {{
            add(AmqpConstants.OFFSET_ANNOTATION_NAME);
            add(AmqpConstants.PARTITION_KEY_ANNOTATION_NAME);
            add(AmqpConstants.SEQUENCE_NUMBER_ANNOTATION_NAME);
            add(AmqpConstants.ENQUEUED_TIME_UTC_ANNOTATION_NAME);
            add(AmqpConstants.PUBLISHER_ANNOTATION_NAME);
            }});

    private EventDataUtil() {
    }

    static LinkedList toEventDataCollection(final Collection messages, final PassByRef lastMessageRef) {

        if (messages == null) {
            return null;
        }

        LinkedList events = new LinkedList<>();
        for (Message message : messages) {
            EventData eventData = new EventDataImpl(message);
            events.add(eventData);

            if (lastMessageRef != null) {
                lastMessageRef.set(new MessageWrapper(message,
                        EventPosition.fromSequenceNumber(eventData.getSystemProperties().getSequenceNumber(), true)));
            }
        }

        return events;
    }

    static Iterable toAmqpMessages(final Iterable eventDatas, final String partitionKey) {

        final LinkedList messages = new LinkedList<>();
        eventDatas.forEach(new Consumer() {
            @Override
            public void accept(EventData eventData) {
                EventDataImpl eventDataImpl = (EventDataImpl) eventData;
                Message amqpMessage = partitionKey == null ? eventDataImpl.toAmqpMessage() : eventDataImpl.toAmqpMessage(partitionKey);
                messages.add(amqpMessage);
            }
        });

        return messages;
    }

    static Iterable toAmqpMessages(final Iterable eventDatas) {

        return EventDataUtil.toAmqpMessages(eventDatas, null);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy