Search in sources :

Example 86 with Session

use of com.swiftmq.amqp.v100.client.Session in project swiftmq-ce by iitsoftware.

the class Suite method suite.

public static Test suite() {
    int nPairs = Integer.parseInt(System.getProperty("npairs", "10"));
    TestSuite suite = new Suite();
    try {
        Connection connection = Util.createConnection();
        Session session = Util.createSession(connection);
        CountDownLatch countDownLatch = new CountDownLatch(nPairs);
        for (int i = 0; i < nPairs; i++) {
            suite.addTest(new Receiver("receive", QoS.AT_MOST_ONCE, Util.getQueueNamePrefix() + (i + 1), connection, session, countDownLatch));
            suite.addTest(new Sender("send", QoS.AT_MOST_ONCE, Util.getQueueNamePrefix() + (i + 1), connection, session));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return suite;
}
Also used : TestSuite(junit.framework.TestSuite) ActiveTestSuite(junit.extensions.ActiveTestSuite) Sender(amqp.v100.ptp.singlesession.Sender) TestSuite(junit.framework.TestSuite) ActiveTestSuite(junit.extensions.ActiveTestSuite) Connection(com.swiftmq.amqp.v100.client.Connection) Receiver(amqp.v100.ptp.singlesession.Receiver) CountDownLatch(java.util.concurrent.CountDownLatch) Session(com.swiftmq.amqp.v100.client.Session)

Example 87 with Session

use of com.swiftmq.amqp.v100.client.Session in project activemq by apache.

the class SwiftMQClientTest method testSendReceive.

@Test
public void testSendReceive() throws Exception {
    String queue = "testqueue";
    int nMsgs = 100;
    final String dataFormat = "%01024d";
    int qos = QoS.AT_MOST_ONCE;
    AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
    try {
        Connection connection = new Connection(ctx, "127.0.0.1", port, false);
        connection.setContainerId("client");
        connection.setIdleTimeout(-1);
        connection.setMaxFrameSize(1024 * 4);
        connection.setExceptionListener(new ExceptionListener() {

            public void onException(Exception e) {
                e.printStackTrace();
            }
        });
        connection.connect();
        {
            Session session = connection.createSession(10, 10);
            Producer p = session.createProducer(queue, qos);
            for (int i = 0; i < nMsgs; i++) {
                AMQPMessage msg = new AMQPMessage();
                System.out.println("Sending " + i);
                msg.setAmqpValue(new AmqpValue(new AMQPString(String.format(dataFormat, i))));
                p.send(msg);
            }
            p.close();
            session.close();
        }
        System.out.println("=======================================================================================");
        System.out.println(" receiving ");
        System.out.println("=======================================================================================");
        {
            Session session = connection.createSession(10, 10);
            Consumer c = session.createConsumer(queue, 100, qos, true, null);
            // Receive messages non-transacted
            int i = 0;
            while (i < nMsgs) {
                AMQPMessage msg = c.receive();
                if (msg != null) {
                    final AMQPType value = msg.getAmqpValue().getValue();
                    if (value instanceof AMQPString) {
                        String s = ((AMQPString) value).getValue();
                        assertEquals(String.format(dataFormat, i), s);
                        System.out.println("Received: " + i);
                    }
                    if (!msg.isSettled())
                        msg.accept();
                    i++;
                }
            }
            c.close();
            session.close();
        }
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : AMQPString(com.swiftmq.amqp.v100.types.AMQPString) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue) AMQPType(com.swiftmq.amqp.v100.types.AMQPType) AMQPContext(com.swiftmq.amqp.AMQPContext) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) Test(org.junit.Test)

Example 88 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class TransactedSender 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();
        Sender sender = connection.openSender(address);
        session.beginTransaction();
        sender.send(Message.create("Transacted Hello"));
        session.commitTransaction();
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) Connection(org.apache.qpid.protonj2.client.Connection) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) Session(org.apache.qpid.protonj2.client.Session)

Example 89 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class ClientLocalTransactionContext method handleTransactionDeclareFailed.

private void handleTransactionDeclareFailed(Transaction<TransactionController> transaction) {
    ClientFuture<Session> future = transaction.getAttachments().get(DECLARE_FUTURE_NAME);
    LOG.trace("Declare of transaction:{} failed", transaction);
    ClientException cause = ClientExceptionSupport.convertToNonFatalException(transaction.getCondition());
    future.failed(new ClientTransactionDeclarationException(cause.getMessage(), cause));
}
Also used : ClientTransactionDeclarationException(org.apache.qpid.protonj2.client.exceptions.ClientTransactionDeclarationException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) Session(org.apache.qpid.protonj2.client.Session)

Example 90 with Session

use of com.swiftmq.amqp.v100.client.Session in project qpid-protonj2 by apache.

the class MessageSendTest method doTestSendMessageWithMapPayloadArrivesWithAMQPValueBody.

private void doTestSendMessageWithMapPayloadArrivesWithAMQPValueBody(boolean useSetter) throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        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);
        final Map<String, UUID> payload = new HashMap<>();
        payload.put("1", UUID.randomUUID());
        payload.put("2", UUID.randomUUID());
        payload.put("3", UUID.randomUUID());
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort()).openFuture().get();
        Session session = connection.openSession().openFuture().get();
        SenderOptions options = new SenderOptions().deliveryMode(DeliveryMode.AT_MOST_ONCE);
        Sender sender = session.openSender("test-qos", options);
        // Gates send on remote flow having been sent and received
        session.openReceiver("dummy").openFuture().get();
        EncodedAmqpValueMatcher bodyMatcher = new EncodedAmqpValueMatcher(payload);
        TransferPayloadCompositeMatcher payloadMatcher = new TransferPayloadCompositeMatcher();
        payloadMatcher.setMessageContentMatcher(bodyMatcher);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectTransfer().withPayload(payloadMatcher).accept();
        peer.expectDetach().respond();
        peer.expectClose().respond();
        final Message<Map<String, UUID>> message;
        if (useSetter) {
            message = Message.<Map<String, UUID>>create().body(payload);
        } else {
            message = Message.create(payload);
        }
        final Tracker tracker = sender.send(message);
        assertNotNull(tracker);
        assertNotNull(tracker.settlementFuture().isDone());
        assertNotNull(tracker.settlementFuture().get().settled());
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) SenderOptions(org.apache.qpid.protonj2.client.SenderOptions) HashMap(java.util.HashMap) Connection(org.apache.qpid.protonj2.client.Connection) EncodedAmqpValueMatcher(org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher) TransferPayloadCompositeMatcher(org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher) URI(java.net.URI) Sender(org.apache.qpid.protonj2.client.Sender) UUID(java.util.UUID) Client(org.apache.qpid.protonj2.client.Client) HashMap(java.util.HashMap) Map(java.util.Map) Session(org.apache.qpid.protonj2.client.Session)

Aggregations

Session (org.apache.qpid.protonj2.client.Session)167 Client (org.apache.qpid.protonj2.client.Client)165 Connection (org.apache.qpid.protonj2.client.Connection)165 URI (java.net.URI)163 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)163 Test (org.junit.jupiter.api.Test)109 Sender (org.apache.qpid.protonj2.client.Sender)66 Receiver (org.apache.qpid.protonj2.client.Receiver)64 ExecutionException (java.util.concurrent.ExecutionException)35 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)35 Tracker (org.apache.qpid.protonj2.client.Tracker)32 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)29 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)28 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)25 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)20 ClientConnectionRemotelyClosedException (org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException)18 HashMap (java.util.HashMap)17 ClientIllegalStateException (org.apache.qpid.protonj2.client.exceptions.ClientIllegalStateException)16 EncodedAmqpValueMatcher (org.apache.qpid.protonj2.test.driver.matchers.types.EncodedAmqpValueMatcher)14 Delivery (org.apache.qpid.protonj2.client.Delivery)13