Search in sources :

Example 6 with AMQPContext

use of com.swiftmq.amqp.AMQPContext in project swiftmq-client by iitsoftware.

the class AMQPConnectionHolder method connect.

public void connect(String username, String password) throws Exception {
    // parse url
    URL url = new URL(urlString.replaceFirst("amqp:", "http:").replaceFirst("amqps:", "https:"));
    String hostname = url.getHost();
    if (hostname == null)
        hostname = "localhost";
    int port = url.getPort();
    if (port == -1)
        port = urlString.startsWith("amqps:") ? 5671 : 5672;
    // create connection
    AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
    if (username == null || username.trim().length() == 0)
        connection = new Connection(ctx, hostname, port, true);
    else
        connection = new Connection(ctx, hostname, port, username, password);
    if (urlString.startsWith("amqps:"))
        connection.setSocketFactory(new JSSESocketFactory());
    String query = url.getQuery();
    if (query != null) {
        Map<String, String> queryMap = getQueryMap(query);
        String v = queryMap.get(Q_MECHANISM);
        if (v != null)
            connection.setMechanism(v);
        v = queryMap.get(Q_MAXFRAMESIZE);
        if (v != null)
            connection.setMaxFrameSize(Long.parseLong(v));
        v = queryMap.get(Q_IDLETIMEOUT);
        if (v != null)
            connection.setIdleTimeout(Long.parseLong(v));
    }
    connection.connect();
}
Also used : AMQPContext(com.swiftmq.amqp.AMQPContext) JSSESocketFactory(com.swiftmq.net.JSSESocketFactory) URL(java.net.URL) Endpoint(com.swiftmq.admin.mgmt.Endpoint)

Example 7 with AMQPContext

use of com.swiftmq.amqp.AMQPContext in project swiftmq-client by iitsoftware.

the class AMQPRepo method connect.

public void connect(String urlString, String username, String password) throws Exception {
    // parse url
    URL url = new URL(urlString.replaceFirst("amqp:", "http:").replaceFirst("amqps:", "https:"));
    String hostname = url.getHost();
    if (hostname == null)
        hostname = "localhost";
    int port = url.getPort();
    if (port == -1)
        port = urlString.startsWith("amqps:") ? 5671 : 5672;
    // create connection
    AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
    if (username == null || username.trim().length() == 0)
        connection = new Connection(ctx, hostname, port, true);
    else
        connection = new Connection(ctx, hostname, port, username, password);
    if (urlString.startsWith("amqps:"))
        connection.setSocketFactory(new JSSESocketFactory());
    String query = url.getQuery();
    if (query != null) {
        Map<String, String> queryMap = getQueryMap(query);
        String v = queryMap.get(Q_MECHANISM);
        if (v != null)
            connection.setMechanism(v);
        v = queryMap.get(Q_MAXFRAMESIZE);
        if (v != null)
            connection.setMaxFrameSize(Long.parseLong(v));
        v = queryMap.get(Q_IDLETIMEOUT);
        if (v != null)
            connection.setIdleTimeout(Long.parseLong(v));
    }
    connection.connect();
}
Also used : AMQPContext(com.swiftmq.amqp.AMQPContext) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) JSSESocketFactory(com.swiftmq.net.JSSESocketFactory) URL(java.net.URL)

Example 8 with AMQPContext

use of com.swiftmq.amqp.AMQPContext 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 9 with AMQPContext

use of com.swiftmq.amqp.AMQPContext in project swiftmq-ce by iitsoftware.

the class ReceiverTransactedRetirement method main.

public static void main(String[] args) {
    if (args.length == 1 && args[0].equals("?")) {
        System.out.println();
        System.out.println("Usage: <host> <port> <source> <nmsgs> <qos> <txsize> <authanon> [<username> <password>]");
        System.out.println("       <qos> ::= AT_LEAST_ONCE | AT_MOST_ONCE | EXACTLY_ONCE");
        System.out.println("       Suppress <username> <password> and set <authanon> to false to avoid SASL.");
        System.out.println();
        System.exit(0);
    }
    String host = "localhost";
    int port = 5672;
    String source = "testqueue";
    int nMsgs = 100;
    String qosS = "EXACTLY_ONCE";
    int txSize = 10;
    boolean authAnon = true;
    String user = null;
    String password = null;
    if (args.length >= 1)
        host = args[0];
    if (args.length >= 2)
        port = Integer.parseInt(args[1]);
    if (args.length >= 3)
        source = args[2];
    if (args.length >= 4)
        nMsgs = Integer.parseInt(args[3]);
    if (args.length >= 5)
        qosS = args[4];
    if (args.length >= 6)
        txSize = Integer.parseInt(args[5]);
    if (args.length >= 7)
        authAnon = Boolean.parseBoolean(args[6]);
    if (args.length >= 8)
        user = args[7];
    if (args.length >= 9)
        password = args[8];
    System.out.println();
    System.out.println("Host        : " + host);
    System.out.println("Port        : " + port);
    System.out.println("Source      : " + source);
    System.out.println("Number Msgs : " + nMsgs);
    System.out.println("QoS         : " + qosS);
    System.out.println("Tx Size     : " + txSize);
    System.out.println("Auth as Anon: " + authAnon);
    System.out.println("User        : " + user);
    System.out.println("Password    : " + password);
    System.out.println();
    try {
        // Create connection and connect
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection connection = null;
        if (args.length < 8)
            connection = new Connection(ctx, host, port, authAnon);
        else
            connection = new Connection(ctx, host, port, user, password);
        if (port == 5671) {
            System.out.println("Using SSL on port 5671");
            connection.setSocketFactory(new JSSESocketFactory());
        }
        connection.connect();
        // Create session and consumer
        Session session = connection.createSession(50, 50);
        Consumer c = session.createConsumer(source, 100, toIntQoS(qosS), true, null);
        // Get the transaction controller
        TransactionController txc = session.getTransactionController();
        // Receive messages in transactions in size <txSize>
        int currentTxSize = 0;
        TxnIdIF txnId = txc.createTxnId();
        for (int i = 0; i < nMsgs; i++) {
            AMQPMessage msg = c.receive();
            if (msg == null)
                break;
            AmqpValue value = msg.getAmqpValue();
            System.out.println("Received: " + ((AMQPString) value.getValue()).getValue());
            msg.setTxnIdIF(txnId);
            msg.accept();
            currentTxSize++;
            if ((i + 1) % txSize == 0) {
                txc.commit(txnId);
                txnId = txc.createTxnId();
                currentTxSize = 0;
            }
        }
        if (currentTxSize > 0)
            txc.commit(txnId);
        // Close everything down
        Thread.sleep(2000);
        c.close();
        session.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TxnIdIF(com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue) AMQPContext(com.swiftmq.amqp.AMQPContext) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) JSSESocketFactory(com.swiftmq.net.JSSESocketFactory)

Example 10 with AMQPContext

use of com.swiftmq.amqp.AMQPContext in project swiftmq-ce by iitsoftware.

the class RequestorNonTransacted method main.

public static void main(String[] args) {
    if (args.length == 1 && args[0].equals("?")) {
        System.out.println();
        System.out.println("Usage: <host> <port> <target> <nreq> <qos> <authanon> [<username> <password>]");
        System.out.println("       <qos> ::= AT_LEAST_ONCE | AT_MOST_ONCE | EXACTLY_ONCE");
        System.out.println("       Suppress <username> <password> and set <authanon> to false to avoid SASL.");
        System.out.println();
        System.exit(0);
    }
    String host = "localhost";
    int port = 5672;
    String target = "testqueue";
    int nMsgs = 100;
    String qosS = "EXACTLY_ONCE";
    boolean authAnon = true;
    String user = null;
    String password = null;
    if (args.length >= 1)
        host = args[0];
    if (args.length >= 2)
        port = Integer.parseInt(args[1]);
    if (args.length >= 3)
        target = args[2];
    if (args.length >= 4)
        nMsgs = Integer.parseInt(args[3]);
    if (args.length >= 5)
        qosS = args[4];
    if (args.length >= 6)
        authAnon = Boolean.parseBoolean(args[5]);
    if (args.length >= 7)
        user = args[6];
    if (args.length >= 8)
        password = args[7];
    System.out.println();
    System.out.println("Host        : " + host);
    System.out.println("Port        : " + port);
    System.out.println("Target      : " + target);
    System.out.println("Number Req  : " + nMsgs);
    System.out.println("QoS         : " + qosS);
    System.out.println("Auth as Anon: " + authAnon);
    System.out.println("User        : " + user);
    System.out.println("Password    : " + password);
    System.out.println();
    try {
        // Create connection and connect
        AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
        Connection connection = null;
        if (args.length < 7)
            connection = new Connection(ctx, host, port, authAnon);
        else
            connection = new Connection(ctx, host, port, user, password);
        if (port == 5671) {
            System.out.println("Using SSL on port 5671");
            connection.setSocketFactory(new JSSESocketFactory());
        }
        connection.connect();
        // Create session and producer
        Session session = connection.createSession(50, 50);
        Producer p = session.createProducer(target, toIntQoS(qosS));
        // Create a temp queue and a consumer
        Consumer c = session.createConsumer(100, toIntQoS(qosS));
        AddressIF tempDest = c.getRemoteAddress();
        // Do request / reply
        for (int i = 0; i < nMsgs; i++) {
            // Send the request
            AMQPMessage request = new AMQPMessage();
            Properties prop = new Properties();
            prop.setReplyTo(tempDest);
            request.setProperties(prop);
            String s = "Message #" + (i + 1);
            System.out.println("Sending " + s);
            request.setAmqpValue(new AmqpValue(new AMQPString(s)));
            p.send(request);
            // Receive the reply
            AMQPMessage reply = c.receive();
            if (reply == null)
                break;
            if (!reply.isSettled())
                reply.accept();
            AmqpValue value = reply.getAmqpValue();
            System.out.println("Received: " + ((AMQPString) value.getValue()).getValue());
        }
        Thread.sleep(2000);
        // Close everything down
        c.close();
        p.close();
        session.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : AddressIF(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressIF) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) Properties(com.swiftmq.amqp.v100.generated.messaging.message_format.Properties) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue) AMQPContext(com.swiftmq.amqp.AMQPContext) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) JSSESocketFactory(com.swiftmq.net.JSSESocketFactory)

Aggregations

AMQPContext (com.swiftmq.amqp.AMQPContext)12 AMQPString (com.swiftmq.amqp.v100.types.AMQPString)10 JSSESocketFactory (com.swiftmq.net.JSSESocketFactory)10 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)9 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)9 Connection (com.swiftmq.amqp.v100.client.Connection)4 Session (com.swiftmq.amqp.v100.client.Session)3 TxnIdIF (com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF)3 AddressIF (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressIF)2 Properties (com.swiftmq.amqp.v100.generated.messaging.message_format.Properties)2 URL (java.net.URL)2 Endpoint (com.swiftmq.admin.mgmt.Endpoint)1 Consumer (com.swiftmq.amqp.v100.client.Consumer)1 DurableConsumer (com.swiftmq.amqp.v100.client.DurableConsumer)1 Producer (com.swiftmq.amqp.v100.client.Producer)1 AMQPType (com.swiftmq.amqp.v100.types.AMQPType)1 SocketFactory (com.swiftmq.net.SocketFactory)1 Test (org.junit.Test)1