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;
}
}
}
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) {
}
}
}
}
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();
}
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;
}
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();
}
Aggregations