Search in sources :

Example 1 with ConnectionOpenedEvent

use of com.mongodb.event.ConnectionOpenedEvent in project mongo-java-driver by mongodb.

the class InternalStreamConnection method openAsync.

@Override
public void openAsync(final SingleResultCallback<Void> callback) {
    isTrue("Open already called", stream == null, callback);
    try {
        stream = streamFactory.create(serverId.getAddress());
    } catch (Throwable t) {
        callback.onResult(null, t);
        return;
    }
    stream.openAsync(new AsyncCompletionHandler<Void>() {

        @Override
        public void completed(final Void aVoid) {
            connectionInitializer.initializeAsync(InternalStreamConnection.this, new SingleResultCallback<ConnectionDescription>() {

                @Override
                public void onResult(final ConnectionDescription result, final Throwable t) {
                    if (t != null) {
                        close();
                        callback.onResult(null, t);
                    } else {
                        description = result;
                        opened.set(true);
                        connectionListener.connectionOpened(new ConnectionOpenedEvent(getId()));
                        if (LOGGER.isInfoEnabled()) {
                            LOGGER.info(format("Opened connection [%s] to %s", getId(), serverId.getAddress()));
                        }
                        callback.onResult(null, null);
                    }
                }
            });
        }

        @Override
        public void failed(final Throwable t) {
            callback.onResult(null, t);
        }
    });
}
Also used : ConnectionOpenedEvent(com.mongodb.event.ConnectionOpenedEvent) SingleResultCallback(com.mongodb.async.SingleResultCallback)

Example 2 with ConnectionOpenedEvent

use of com.mongodb.event.ConnectionOpenedEvent in project mongo-java-driver by mongodb.

the class InternalStreamConnection method open.

@Override
public void open() {
    isTrue("Open already called", stream == null);
    stream = streamFactory.create(serverId.getAddress());
    try {
        stream.open();
        description = connectionInitializer.initialize(this);
        opened.set(true);
        connectionListener.connectionOpened(new ConnectionOpenedEvent(getId()));
        LOGGER.info(format("Opened connection [%s] to %s", getId(), serverId.getAddress()));
    } catch (Throwable t) {
        close();
        if (t instanceof MongoException) {
            throw (MongoException) t;
        } else {
            throw new MongoException(t.toString(), t);
        }
    }
}
Also used : ConnectionOpenedEvent(com.mongodb.event.ConnectionOpenedEvent) MongoException(com.mongodb.MongoException)

Aggregations

ConnectionOpenedEvent (com.mongodb.event.ConnectionOpenedEvent)2 MongoException (com.mongodb.MongoException)1 SingleResultCallback (com.mongodb.async.SingleResultCallback)1