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

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

There is a newer version: 3.3.0
Show newest version
package com.microsoft.azure.eventhubs.impl;

import org.apache.qpid.proton.amqp.transport.ErrorCondition;
import org.apache.qpid.proton.engine.Session;

import java.util.function.BiConsumer;

public class RequestResponseOpener implements Operation {
    private final SessionProvider sessionProvider;
    private final String sessionName;
    private final String linkName;
    private final String endpointAddress;
    private final AmqpConnection eventDispatcher;

    public RequestResponseOpener(final SessionProvider sessionProvider, final String sessionName, final String linkName,
                                 final String endpointAddress, final AmqpConnection eventDispatcher) {
        this.sessionProvider = sessionProvider;
        this.sessionName = sessionName;
        this.linkName = linkName;
        this.endpointAddress = endpointAddress;
        this.eventDispatcher = eventDispatcher;
    }

    @Override
    public void run(OperationResult operationCallback) {

        final Session session = this.sessionProvider.getSession(
                this.sessionName,
                null,
                new BiConsumer() {
                    @Override
                    public void accept(ErrorCondition error, Exception exception) {
                        if (error != null)
                            operationCallback.onError(ExceptionUtil.toException(error));
                        else if (exception != null)
                            operationCallback.onError(exception);
                    }
                });

        if (session == null)
            return;

        final RequestResponseChannel requestResponseChannel = new RequestResponseChannel(
                this.linkName,
                this.endpointAddress,
                session);

        requestResponseChannel.open(
                new OperationResult() {
                    @Override
                    public void onComplete(Void result) {
                        eventDispatcher.registerForConnectionError(requestResponseChannel.getSendLink());
                        eventDispatcher.registerForConnectionError(requestResponseChannel.getReceiveLink());

                        operationCallback.onComplete(requestResponseChannel);
                    }

                    @Override
                    public void onError(Exception error) {
                        operationCallback.onError(error);
                    }
                },
                new OperationResult() {
                    @Override
                    public void onComplete(Void result) {
                        eventDispatcher.deregisterForConnectionError(requestResponseChannel.getSendLink());
                        eventDispatcher.deregisterForConnectionError(requestResponseChannel.getReceiveLink());
                    }

                    @Override
                    public void onError(Exception error) {
                        eventDispatcher.deregisterForConnectionError(requestResponseChannel.getSendLink());
                        eventDispatcher.deregisterForConnectionError(requestResponseChannel.getReceiveLink());
                    }
                });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy