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

com.mongodb.internal.connection.SendMessageCallback Maven / Gradle / Ivy

Go to download

The MongoDB Java Driver uber-artifact, containing the legacy driver, the mongodb-driver, mongodb-driver-core, and bson

There is a newer version: 3.12.14
Show newest version
/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.internal.connection;

import com.mongodb.async.SingleResultCallback;
import com.mongodb.event.CommandListener;
import org.bson.io.OutputBuffer;

import static com.mongodb.internal.connection.ProtocolHelper.sendCommandFailedEvent;

class SendMessageCallback implements SingleResultCallback {
    private final OutputBuffer buffer;
    private final InternalConnection connection;
    private final SingleResultCallback receiveMessageCallback;
    private final int requestId;
    private final RequestMessage message;
    private final CommandListener commandListener;
    private final long startTimeNanos;
    private final SingleResultCallback callback;
    private final String commandName;

    SendMessageCallback(final InternalConnection connection, final OutputBuffer buffer, final RequestMessage message,
                        final String commandName, final long startTimeNanos, final CommandListener commandListener,
                        final SingleResultCallback callback, final SingleResultCallback receiveMessageCallback) {
        this(connection, buffer, message, message.getId(), commandName, startTimeNanos, commandListener, callback, receiveMessageCallback);
    }

    SendMessageCallback(final InternalConnection connection, final OutputBuffer buffer, final RequestMessage message,
                        final int requestId, final String commandName, final long startTimeNanos, final CommandListener commandListener,
                        final SingleResultCallback callback, final SingleResultCallback receiveMessageCallback) {
        this.buffer = buffer;
        this.connection = connection;
        this.message = message;
        this.commandName = commandName;
        this.commandListener = commandListener;
        this.startTimeNanos = startTimeNanos;
        this.callback = callback;
        this.receiveMessageCallback = receiveMessageCallback;
        this.requestId = requestId;
    }

    @Override
    public void onResult(final Void result, final Throwable t) {
        buffer.close();
        if (t != null) {
            if (commandListener != null){
                sendCommandFailedEvent(message, commandName, connection.getDescription(), System.nanoTime() - startTimeNanos, t,
                        commandListener);
            }
            callback.onResult(null, t);
        } else {
            connection.receiveMessageAsync(requestId, receiveMessageCallback);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy