Search in sources :

Example 1 with AsyncReadOperation

use of com.mongodb.operation.AsyncReadOperation in project mongo-java-driver by mongodb.

the class MongoClientImpl method createOperationExecutor.

private static AsyncOperationExecutor createOperationExecutor(final MongoClientSettings settings, final Cluster cluster) {
    return new AsyncOperationExecutor() {

        @Override
        public <T> void execute(final AsyncReadOperation<T> operation, final ReadPreference readPreference, final SingleResultCallback<T> callback) {
            notNull("operation", operation);
            notNull("readPreference", readPreference);
            notNull("callback", callback);
            final SingleResultCallback<T> errHandlingCallback = errorHandlingCallback(callback, LOGGER);
            final AsyncReadBinding binding = getReadWriteBinding(readPreference, cluster);
            operation.executeAsync(binding, new SingleResultCallback<T>() {

                @Override
                public void onResult(final T result, final Throwable t) {
                    try {
                        errHandlingCallback.onResult(result, t);
                    } finally {
                        binding.release();
                    }
                }
            });
        }

        @Override
        public <T> void execute(final AsyncWriteOperation<T> operation, final SingleResultCallback<T> callback) {
            notNull("operation", operation);
            notNull("callback", callback);
            final AsyncWriteBinding binding = getReadWriteBinding(ReadPreference.primary(), cluster);
            operation.executeAsync(binding, new SingleResultCallback<T>() {

                @Override
                public void onResult(final T result, final Throwable t) {
                    try {
                        errorHandlingCallback(callback, LOGGER).onResult(result, t);
                    } finally {
                        binding.release();
                    }
                }
            });
        }
    };
}
Also used : ReadPreference(com.mongodb.ReadPreference) AsyncOperationExecutor(com.mongodb.operation.AsyncOperationExecutor) AsyncReadBinding(com.mongodb.binding.AsyncReadBinding) SingleResultCallback(com.mongodb.async.SingleResultCallback) AsyncWriteBinding(com.mongodb.binding.AsyncWriteBinding) AsyncWriteOperation(com.mongodb.operation.AsyncWriteOperation) AsyncReadOperation(com.mongodb.operation.AsyncReadOperation)

Aggregations

ReadPreference (com.mongodb.ReadPreference)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1 AsyncReadBinding (com.mongodb.binding.AsyncReadBinding)1 AsyncWriteBinding (com.mongodb.binding.AsyncWriteBinding)1 AsyncOperationExecutor (com.mongodb.operation.AsyncOperationExecutor)1 AsyncReadOperation (com.mongodb.operation.AsyncReadOperation)1 AsyncWriteOperation (com.mongodb.operation.AsyncWriteOperation)1