Search in sources :

Example 1 with ClientFuture

use of org.apache.activemq.transport.amqp.client.util.ClientFuture in project activemq-artemis by apache.

the class AmqpConnection method connect.

public void connect() throws Exception {
    if (connected.compareAndSet(false, true)) {
        transport.connect();
        final ClientFuture future = new ClientFuture();
        serializer.execute(new Runnable() {

            @Override
            public void run() {
                if (!noContainerID) {
                    getEndpoint().setContainer(safeGetContainerId());
                }
                getEndpoint().setHostname(remoteURI.getHost());
                if (!getDesiredCapabilities().isEmpty()) {
                    getEndpoint().setDesiredCapabilities(getDesiredCapabilities().toArray(new Symbol[0]));
                }
                if (!getOfferedCapabilities().isEmpty()) {
                    getEndpoint().setOfferedCapabilities(getOfferedCapabilities().toArray(new Symbol[0]));
                }
                if (!getOfferedProperties().isEmpty()) {
                    getEndpoint().setProperties(getOfferedProperties());
                }
                if (getIdleTimeout() > 0) {
                    protonTransport.setIdleTimeout(getIdleTimeout());
                }
                protonTransport.setMaxFrameSize(getMaxFrameSize());
                protonTransport.setChannelMax(getChannelMax());
                protonTransport.bind(getEndpoint());
                Sasl sasl = protonTransport.sasl();
                if (sasl != null) {
                    sasl.client();
                }
                authenticator = new SaslAuthenticator(sasl, username, password, authzid, mechanismRestriction);
                ((TransportImpl) protonTransport).setProtocolTracer(new AmqpProtocolTracer(AmqpConnection.this));
                open(future);
                pumpToProtonTransport(future);
            }
        });
        try {
            if (connectTimeout <= 0) {
                future.sync();
            } else {
                future.sync(connectTimeout, TimeUnit.MILLISECONDS);
                if (getEndpoint().getRemoteState() != EndpointState.ACTIVE) {
                    throw new IOException("Failed to connect after configured timeout.");
                }
            }
        } catch (Throwable e) {
            try {
                close();
            } catch (Throwable ignore) {
            }
            throw e;
        }
    }
}
Also used : Sasl(org.apache.qpid.proton.engine.Sasl) ClientFuture(org.apache.activemq.transport.amqp.client.util.ClientFuture) SaslAuthenticator(org.apache.activemq.transport.amqp.client.sasl.SaslAuthenticator) InactivityIOException(org.apache.activemq.transport.InactivityIOException) IOException(java.io.IOException)

Example 2 with ClientFuture

use of org.apache.activemq.transport.amqp.client.util.ClientFuture in project activemq-artemis by apache.

the class AmqpConnection method close.

public void close() {
    if (closed.compareAndSet(false, true)) {
        final ClientFuture request = new ClientFuture();
        serializer.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    // just signal success.
                    if (!transport.isConnected()) {
                        request.onSuccess();
                    }
                    if (getEndpoint() != null) {
                        close(request);
                    } else {
                        request.onSuccess();
                    }
                    pumpToProtonTransport(request);
                } catch (Exception e) {
                    LOG.debug("Caught exception while closing proton connection");
                }
            }
        });
        try {
            if (closeTimeout <= 0) {
                request.sync();
            } else {
                request.sync(closeTimeout, TimeUnit.MILLISECONDS);
            }
        } catch (IOException e) {
            LOG.warn("Error caught while closing Provider: ", e.getMessage());
        } finally {
            if (transport != null) {
                try {
                    transport.close();
                } catch (Exception e) {
                    LOG.debug("Cuaght exception while closing down Transport: {}", e.getMessage());
                }
            }
            serializer.shutdownNow();
            try {
                if (!serializer.awaitTermination(10, TimeUnit.SECONDS)) {
                    LOG.warn("Serializer didn't shutdown cleanly");
                }
            } catch (InterruptedException e) {
            }
        }
    }
}
Also used : ClientFuture(org.apache.activemq.transport.amqp.client.util.ClientFuture) InactivityIOException(org.apache.activemq.transport.InactivityIOException) IOException(java.io.IOException) InactivityIOException(org.apache.activemq.transport.InactivityIOException) IOException(java.io.IOException)

Example 3 with ClientFuture

use of org.apache.activemq.transport.amqp.client.util.ClientFuture in project activemq-artemis by apache.

the class AmqpConnection method sendRawBytes.

// ----- Access to low level IO for specific test cases -------------------//
public void sendRawBytes(final byte[] rawData) throws Exception {
    checkClosed();
    final ClientFuture request = new ClientFuture();
    serializer.execute(new Runnable() {

        @Override
        public void run() {
            checkClosed();
            try {
                transport.send(Unpooled.wrappedBuffer(rawData));
            } catch (IOException e) {
                fireClientException(e);
            } finally {
                request.onSuccess();
            }
        }
    });
    request.sync();
}
Also used : ClientFuture(org.apache.activemq.transport.amqp.client.util.ClientFuture) InactivityIOException(org.apache.activemq.transport.InactivityIOException) IOException(java.io.IOException)

Example 4 with ClientFuture

use of org.apache.activemq.transport.amqp.client.util.ClientFuture in project activemq-artemis by apache.

the class AmqpConnection method createSession.

/**
 * Creates a new Session instance used to create AMQP resources like
 * senders and receivers.
 *
 * @return a new AmqpSession that can be used to create links.
 * @throws Exception if an error occurs during creation.
 */
public AmqpSession createSession() throws Exception {
    checkClosed();
    final AmqpSession session = new AmqpSession(AmqpConnection.this, getNextSessionId());
    final ClientFuture request = new ClientFuture();
    serializer.execute(new Runnable() {

        @Override
        public void run() {
            checkClosed();
            session.setEndpoint(getEndpoint().session());
            session.setStateInspector(getStateInspector());
            session.open(request);
            pumpToProtonTransport(request);
        }
    });
    request.sync();
    return session;
}
Also used : ClientFuture(org.apache.activemq.transport.amqp.client.util.ClientFuture)

Example 5 with ClientFuture

use of org.apache.activemq.transport.amqp.client.util.ClientFuture in project activemq-artemis by apache.

the class AmqpReceiver method accept.

/**
 * Accepts a message that was dispatched under the given Delivery instance.
 *
 * This method allows for the session that is used in the accept to be specified by the
 * caller. This allows for an accepted message to be involved in a transaction that is being
 * managed by some other session other than the one that created this receiver.
 *
 * @param delivery
 *        the Delivery instance to accept.
 * @param session
 *        the session under which the message is being accepted.
 * @param settle
 *        true if the receiver should settle the delivery or just send the disposition.
 *
 * @throws IOException
 *         if an error occurs while sending the accept.
 */
public void accept(final Delivery delivery, final AmqpSession session, final boolean settle) throws IOException {
    checkClosed();
    if (delivery == null) {
        throw new IllegalArgumentException("Delivery to accept cannot be null");
    }
    if (session == null) {
        throw new IllegalArgumentException("Session given cannot be null");
    }
    if (session.getConnection() != this.session.getConnection()) {
        throw new IllegalArgumentException("The session used for accept must originate from the connection that created this receiver.");
    }
    final ClientFuture request = new ClientFuture();
    session.getScheduler().execute(new Runnable() {

        @Override
        public void run() {
            checkClosed();
            try {
                if (!delivery.isSettled()) {
                    if (session.isInTransaction()) {
                        Binary txnId = session.getTransactionId().getRemoteTxId();
                        if (txnId != null) {
                            TransactionalState txState = new TransactionalState();
                            txState.setOutcome(Accepted.getInstance());
                            txState.setTxnId(txnId);
                            delivery.disposition(txState);
                            session.getTransactionContext().registerTxConsumer(AmqpReceiver.this);
                        }
                    } else {
                        delivery.disposition(Accepted.getInstance());
                    }
                    if (settle) {
                        delivery.settle();
                    }
                }
                session.pumpToProtonTransport(request);
                request.onSuccess();
            } catch (Exception e) {
                request.onFailure(e);
            }
        }
    });
    request.sync();
}
Also used : ClientFuture(org.apache.activemq.transport.amqp.client.util.ClientFuture) Binary(org.apache.qpid.proton.amqp.Binary) JmsOperationTimedOutException(org.apache.qpid.jms.JmsOperationTimedOutException) InvalidDestinationException(javax.jms.InvalidDestinationException) IOException(java.io.IOException) TransactionalState(org.apache.qpid.proton.amqp.transaction.TransactionalState)

Aggregations

ClientFuture (org.apache.activemq.transport.amqp.client.util.ClientFuture)30 IOException (java.io.IOException)15 InvalidDestinationException (javax.jms.InvalidDestinationException)9 JmsOperationTimedOutException (org.apache.qpid.jms.JmsOperationTimedOutException)8 InactivityIOException (org.apache.activemq.transport.InactivityIOException)3 ClientFutureSynchronization (org.apache.activemq.transport.amqp.client.util.ClientFutureSynchronization)3 SaslAuthenticator (org.apache.activemq.transport.amqp.client.sasl.SaslAuthenticator)1 AsyncResult (org.apache.activemq.transport.amqp.client.util.AsyncResult)1 Binary (org.apache.qpid.proton.amqp.Binary)1 Modified (org.apache.qpid.proton.amqp.messaging.Modified)1 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)1 TransactionalState (org.apache.qpid.proton.amqp.transaction.TransactionalState)1 Sasl (org.apache.qpid.proton.engine.Sasl)1