use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class ClientLocalTransactionContext method handleTransactionDischargeFailed.
private void handleTransactionDischargeFailed(Transaction<TransactionController> transaction) {
ClientFuture<Session> future = transaction.getAttachments().get(DISCHARGE_FUTURE_NAME);
LOG.trace("Discharge of transaction:{} failed", transaction);
ClientException cause = ClientExceptionSupport.convertToNonFatalException(transaction.getCondition());
future.failed(new ClientTransactionRolledBackException(cause.getMessage(), cause));
}
use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class TransactedReceiver method main.
public static void main(String[] args) throws Exception {
final String serverHost = System.getProperty("HOST", "localhost");
final int serverPort = Integer.getInteger("PORT", 5672);
final String address = System.getProperty("ADDRESS", "transaction-example");
final Client client = Client.create();
final ConnectionOptions options = new ConnectionOptions();
options.user(System.getProperty("USER"));
options.password(System.getProperty("PASSWORD"));
try (Connection connection = client.connect(serverHost, serverPort, options)) {
Session session = connection.openSession();
Receiver receiver = session.openReceiver(address);
session.beginTransaction();
Delivery delivery = receiver.receive();
Message<String> message = delivery.message();
System.out.println("Received message with body: " + message.body());
session.commitTransaction();
}
}
use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class StreamSenderTest method testSendCustomMessageWithMultipleAmqpValueSections.
@Test
public void testSendCustomMessageWithMultipleAmqpValueSections() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
// Hidden session for stream sender
peer.expectBegin().respond();
peer.expectAttach().ofSender().respond();
peer.remoteFlow().withLinkCredit(10).queue();
// Open a receiver to ensure sender link has processed
peer.expectAttach().respond();
// the inbound flow frame we sent previously before send.
peer.expectFlow();
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()).openFuture().get();
Session session = connection.openSession().openFuture().get();
StreamSenderOptions options = new StreamSenderOptions();
options.deliveryMode(DeliveryMode.AT_MOST_ONCE);
options.writeBufferSize(Integer.MAX_VALUE);
StreamSender sender = connection.openStreamSender("test-qos", options);
// Create a custom message format send context and ensure that no early buffer writes take place
StreamSenderMessage message = sender.beginMessage();
assertEquals(sender, message.sender());
assertNull(message.tracker());
assertEquals(Header.DEFAULT_PRIORITY, message.priority());
assertEquals(Header.DEFAULT_DELIVERY_COUNT, message.deliveryCount());
assertEquals(Header.DEFAULT_FIRST_ACQUIRER, message.firstAcquirer());
assertEquals(Header.DEFAULT_TIME_TO_LIVE, message.timeToLive());
assertEquals(Header.DEFAULT_DURABILITY, message.durable());
message.messageFormat(17);
// Gates send on remote flow having been sent and received
session.openReceiver("dummy").openFuture().get();
HeaderMatcher headerMatcher = new HeaderMatcher(true);
headerMatcher.withDurable(true);
headerMatcher.withPriority((byte) 1);
headerMatcher.withTtl(65535);
headerMatcher.withFirstAcquirer(true);
headerMatcher.withDeliveryCount(2);
// Note: This is a specification violation but could be used by other message formats
// and we don't attempt to enforce at the Send Context what users write
EncodedAmqpValueMatcher bodyMatcher1 = new EncodedAmqpValueMatcher("one", true);
EncodedAmqpValueMatcher bodyMatcher2 = new EncodedAmqpValueMatcher("two", true);
EncodedAmqpValueMatcher bodyMatcher3 = new EncodedAmqpValueMatcher("three", false);
TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
payloadMatcher.setHeadersMatcher(headerMatcher);
payloadMatcher.addMessageContentMatcher(bodyMatcher1);
payloadMatcher.addMessageContentMatcher(bodyMatcher2);
payloadMatcher.addMessageContentMatcher(bodyMatcher3);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
peer.expectTransfer().withMore(false).withMessageFormat(17).withPayload(payloadMatcher).accept();
peer.expectDetach().respond();
peer.expectEnd().respond();
peer.expectClose().respond();
// Populate all Header values
Header header = new Header();
header.setDurable(true);
header.setPriority((byte) 1);
header.setTimeToLive(65535);
header.setFirstAcquirer(true);
header.setDeliveryCount(2);
message.header(header);
message.addBodySection(new AmqpValue<>("one"));
message.addBodySection(new AmqpValue<>("two"));
message.addBodySection(new AmqpValue<>("three"));
message.complete();
assertNotNull(message.tracker());
assertEquals(17, message.messageFormat());
Wait.assertTrue(() -> message.tracker().settlementFuture().isDone());
assertTrue(message.tracker().settlementFuture().get().settled());
assertThrows(ClientIllegalStateException.class, () -> message.addBodySection(new AmqpValue<>("three")));
assertThrows(ClientIllegalStateException.class, () -> message.body());
assertThrows(ClientIllegalStateException.class, () -> message.rawOutputStream());
assertThrows(ClientIllegalStateException.class, () -> message.abort());
sender.closeAsync().get(10, TimeUnit.SECONDS);
connection.closeAsync().get(10, TimeUnit.SECONDS);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class TransactionsTest method testExceptionOnCommitWhenCoordinatorRejectsDischarge.
@Test
public void testExceptionOnCommitWhenCoordinatorRejectsDischarge() throws Exception {
final String errorMessage = "Transaction aborted due to timeout";
final byte[] txnId1 = new byte[] { 0, 1, 2, 3 };
final byte[] txnId2 = new byte[] { 1, 1, 2, 3 };
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectCoordinatorAttach().respond();
peer.remoteFlow().withLinkCredit(4).queue();
peer.expectDeclare().accept(txnId1);
peer.expectDischarge().withFail(false).withTxnId(txnId1).reject(TransactionErrors.TRANSACTION_TIMEOUT.toString(), "Transaction aborted due to timeout");
peer.expectDeclare().accept(txnId2);
peer.expectDischarge().withFail(true).withTxnId(txnId2).accept();
peer.expectEnd().respond();
peer.expectClose().respond();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
Session session = connection.openSession().openFuture().get();
session.beginTransaction();
try {
session.commitTransaction();
fail("Commit should have failed after link closed.");
} catch (ClientTransactionRolledBackException expected) {
// Expect this to time out.
String message = expected.getMessage();
assertTrue(message.contains(errorMessage));
}
session.beginTransaction();
session.rollbackTransaction();
session.closeAsync();
connection.closeAsync().get();
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.
the class TransactionsTest method testBeginTransactionAndClose.
/**
* Create a transaction and then close the Session which result in the remote rolling back
* the transaction by default so the client doesn't manually roll it back itself.
*
* @throws Exception
*/
@Test
public void testBeginTransactionAndClose() throws Exception {
try (ProtonTestServer peer = new ProtonTestServer()) {
peer.expectSASLAnonymousConnect();
peer.expectOpen().respond();
peer.expectBegin().respond();
peer.expectCoordinatorAttach().respond();
peer.remoteFlow().withLinkCredit(2).queue();
peer.expectDeclare().accept();
peer.expectEnd().respond();
peer.expectClose().respond();
peer.start();
URI remoteURI = peer.getServerURI();
LOG.info("Test started, peer listening on: {}", remoteURI);
Client container = Client.create();
Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
Session session = connection.openSession().openFuture().get();
session.beginTransaction();
session.closeAsync();
connection.closeAsync().get(10, TimeUnit.SECONDS);
peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
}
}
Aggregations