use of org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException in project qpid-protonj2 by apache.
the class ClientExceptionSupport method createConnectionRedirectException.
/**
* When a connection redirect type exception is received this method is called to create the
* appropriate redirect exception type containing the error details needed.
*
* @param error
* the Symbol that defines the redirection error type.
* @param message
* the basic error message that should used or amended for the returned exception.
* @param condition
* the ErrorCondition that describes the redirection.
*
* @return an Exception that captures the details of the redirection error.
*/
public static ClientConnectionRemotelyClosedException createConnectionRedirectException(Symbol error, String message, ErrorCondition condition) {
ClientConnectionRemotelyClosedException result;
Map<?, ?> info = condition.getInfo();
if (info == null) {
result = new ClientConnectionRemotelyClosedException(message + " : Redirection information not set.", new ClientErrorCondition(condition));
} else {
@SuppressWarnings("unchecked") ClientRedirect redirect = new ClientRedirect((Map<Symbol, Object>) info);
try {
result = new ClientConnectionRedirectedException(message, redirect.validate(), new ClientErrorCondition(condition));
} catch (Exception ex) {
result = new ClientConnectionRemotelyClosedException(message + " : " + ex.getMessage(), new ClientErrorCondition(condition));
}
}
return result;
}
use of org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException in project qpid-protonj2 by apache.
the class ClientLinkType method handleEngineShutdown.
protected final void handleEngineShutdown(Engine engine) {
if (!isDynamic() && !session.getConnection().getEngine().isShutdown()) {
recreateLinkForReconnect();
open();
} else {
final Connection connection = engine.connection();
final ClientException failureCause;
if (connection.getRemoteCondition() != null) {
failureCause = ClientExceptionSupport.convertToConnectionClosedException(connection.getRemoteCondition());
} else if (engine.failureCause() != null) {
failureCause = ClientExceptionSupport.convertToConnectionClosedException(engine.failureCause());
} else if (!isClosed()) {
failureCause = new ClientConnectionRemotelyClosedException("Remote closed without a specific error condition");
} else {
failureCause = null;
}
immediateLinkShutdown(failureCause);
}
}
use of org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException in project qpid-protonj2 by apache.
the class ReconnectStreamSenderTest method testStreamMessageWriteThatFlushesFailsAfterConnectionDroppedAndReconnected.
@Test
public void testStreamMessageWriteThatFlushesFailsAfterConnectionDroppedAndReconnected() throws Exception {
try (ProtonTestServer firstPeer = new ProtonTestServer();
ProtonTestServer finalPeer = new ProtonTestServer()) {
EncodedDataMatcher dataMatcher = new EncodedDataMatcher(new byte[] { 0, 1, 2, 3 });
TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
payloadMatcher.setMessageContentMatcher(dataMatcher);
firstPeer.expectSASLAnonymousConnect();
firstPeer.expectOpen().respond();
firstPeer.expectBegin().respond();
firstPeer.expectAttach().ofSender().respond();
firstPeer.remoteFlow().withLinkCredit(1).queue();
firstPeer.expectTransfer().withPayload(payloadMatcher).withMore(true);
firstPeer.dropAfterLastHandler();
firstPeer.start();
finalPeer.expectSASLAnonymousConnect();
finalPeer.expectOpen().respond();
finalPeer.expectBegin().respond();
finalPeer.expectAttach().ofSender().respond();
finalPeer.start();
final URI primaryURI = firstPeer.getServerURI();
final URI backupURI = finalPeer.getServerURI();
ConnectionOptions options = new ConnectionOptions();
options.maxFrameSize(32768);
options.idleTimeout(5, TimeUnit.SECONDS);
options.reconnectOptions().reconnectEnabled(true);
options.reconnectOptions().addReconnectLocation(backupURI.getHost(), backupURI.getPort());
Client container = Client.create();
Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
StreamSenderOptions senderOptions = new StreamSenderOptions();
senderOptions.sendTimeout(1000);
StreamSender sender = connection.openStreamSender("test-queue", senderOptions);
StreamSenderMessage message = sender.beginMessage();
OutputStream stream = message.body();
stream.write(new byte[] { 0, 1, 2, 3 });
stream.flush();
firstPeer.waitForScriptToComplete();
// Reconnection should have occurred now and we should not be able to flush data
// from the stream as its initial sender instance was closed on disconnect.
finalPeer.waitForScriptToComplete();
// Ensure that idle processing happens in case send blocks so we can see the
// send timed out exception
finalPeer.remoteEmptyFrame().later(5000);
finalPeer.remoteEmptyFrame().later(10000);
finalPeer.remoteEmptyFrame().later(15000);
// Test timeout kicks in now
finalPeer.remoteEmptyFrame().later(20000);
finalPeer.expectClose().respond();
byte[] payload = new byte[1024];
Arrays.fill(payload, (byte) 65);
try {
stream.write(payload);
stream.flush();
fail("Should not be able to write section after connection drop");
} catch (IOException ioe) {
assertFalse(ioe.getCause() instanceof ClientSendTimedOutException);
assertTrue(ioe.getCause() instanceof ClientConnectionRemotelyClosedException);
}
connection.closeAsync().get();
finalPeer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException in project qpid-protonj2 by apache.
the class ReconnectTest method testConnectionReportsFailedAfterMaxinitialReconnectAttempts.
@Test
public void testConnectionReportsFailedAfterMaxinitialReconnectAttempts() throws Exception {
try (ProtonTestServer firstPeer = new ProtonTestServer()) {
firstPeer.start();
final URI primaryURI = firstPeer.getServerURI();
firstPeer.close();
ConnectionOptions options = new ConnectionOptions();
options.reconnectOptions().reconnectEnabled(true);
// Try forever if connect succeeds once.
options.reconnectOptions().maxReconnectAttempts(-1);
options.reconnectOptions().maxInitialConnectionAttempts(3);
options.reconnectOptions().warnAfterReconnectAttempts(5);
options.reconnectOptions().reconnectDelay(10);
options.reconnectOptions().useReconnectBackOff(false);
Client container = Client.create();
Connection connection = container.connect(primaryURI.getHost(), primaryURI.getPort(), options);
try {
connection.openFuture().get();
fail("Should not successfully connect.");
} catch (ExecutionException exe) {
assertTrue(exe.getCause() instanceof ClientConnectionRemotelyClosedException);
}
try {
connection.defaultSender();
fail("Connection should be in a failed state now.");
} catch (ClientConnectionRemotelyClosedException cliEx) {
}
connection.close();
try {
connection.defaultSender();
fail("Connection should be in a closed state now.");
} catch (ClientIllegalStateException cliEx) {
}
}
}
use of org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException in project qpid-protonj2 by apache.
the class SenderTest method testSendBlockedForCreditFailsWhenConnectionDrops.
@Test
public void testSendBlockedForCreditFailsWhenConnectionDrops() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.dropAfterLastHandler(25);
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Sender test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
Session session = connection.openSession();
Sender sender = session.openSender("test-queue");
sender.openFuture().get();
Message<String> message = Message.create("Hello World");
try {
sender.send(message);
fail("Send should have timed out.");
} catch (ClientConnectionRemotelyClosedException cliEx) {
// Expected send to throw indicating that the remote closed unexpectedly
}
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations