Search in sources :

Example 1 with AmqpValue

use of com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue in project swiftmq-ce by iitsoftware.

the class ReceiverTransactedAcquisition 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);
        // Important to create the consumer without a link credit because the link credit is set by the acquisition
        Consumer c = session.createConsumer(source, 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();
        c.acquire(txSize, txnId);
        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.accept();
            currentTxSize++;
            if ((i + 1) % txSize == 0) {
                txc.commit(txnId);
                txnId = txc.createTxnId();
                c.acquire(txSize, txnId);
                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 2 with AmqpValue

use of com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue in project swiftmq-ce by iitsoftware.

the class ReplierNonTransacted 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> <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";
    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)
        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("Source      : " + source);
    System.out.println("Number Msgs : " + 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 consumer
        Session session = connection.createSession(50, 50);
        Consumer c = session.createConsumer(source, 100, toIntQoS(qosS), true, null);
        // Receive messages non-transacted
        for (int i = 0; i < nMsgs; i++) {
            AMQPMessage request = c.receive();
            if (request == null)
                break;
            AmqpValue value = request.getAmqpValue();
            System.out.println("Received: " + ((AMQPString) value.getValue()).getValue());
            if (!request.isSettled())
                request.accept();
            Properties prop = request.getProperties();
            if (prop == null)
                throw new Exception("Properties not set in request: " + request);
            AddressIF replyTo = prop.getReplyTo();
            Producer p = session.createProducer(replyTo.getValueString(), toIntQoS(qosS));
            AMQPMessage reply = new AMQPMessage();
            Properties prop2 = new Properties();
            prop2.setTo(replyTo);
            prop2.setCorrelationId(prop.getMessageId());
            reply.setProperties(prop2);
            String s = "RE: " + ((AMQPString) value.getValue()).getValue();
            reply.setAmqpValue(new AmqpValue(new AMQPString(s)));
            System.out.println("Send: " + s);
            p.send(reply);
            p.close();
        }
        // Close everything down
        Thread.sleep(2000);
        c.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)

Example 3 with AmqpValue

use of com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue in project swiftmq-ce by iitsoftware.

the class SenderTransacted 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> <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 target = "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)
        target = 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("Target      : " + target);
    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 producer
        Session session = connection.createSession(50, 50);
        Producer p = session.createProducer(target, toIntQoS(qosS));
        // Get the transaction controller
        TransactionController txc = session.getTransactionController();
        // Send messages in transactions in size <txSize>
        int currentTxSize = 0;
        TxnIdIF txnId = txc.createTxnId();
        for (int i = 0; i < nMsgs; i++) {
            AMQPMessage msg = new AMQPMessage();
            String s = "Message #" + (i + 1);
            System.out.println("Sending " + s);
            msg.setAmqpValue(new AmqpValue(new AMQPString(s)));
            msg.setTxnIdIF(txnId);
            p.send(msg);
            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);
        p.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 4 with AmqpValue

use of com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue in project swiftmq-ce by iitsoftware.

the class ReceiverNonTransacted 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> <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";
    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)
        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("Source      : " + source);
    System.out.println("Number Msgs : " + 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 consumer
        Session session = connection.createSession(50, 50);
        Consumer c = session.createConsumer(source, 100, toIntQoS(qosS), true, null);
        // Receive messages non-transacted
        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());
            if (!msg.isSettled())
                msg.accept();
        }
        // Close everything down
        Thread.sleep(2000);
        c.close();
        session.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Consumer(com.swiftmq.amqp.v100.client.Consumer) Connection(com.swiftmq.amqp.v100.client.Connection) AMQPContext(com.swiftmq.amqp.AMQPContext) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) JSSESocketFactory(com.swiftmq.net.JSSESocketFactory) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue) Session(com.swiftmq.amqp.v100.client.Session)

Example 5 with AmqpValue

use of com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue in project swiftmq-ce by iitsoftware.

the class AMQPValueMapMessageFactory method verify.

public void verify(AMQPMessage message) throws Exception {
    AmqpValue value = message.getAmqpValue();
    if (value == null)
        throw new Exception(("verify - no AmqpValue section found!"));
    AMQPType t = value.getValue();
    if (!(t instanceof AMQPMap))
        throw new Exception(("verify - AmqpValue does not contain an AmqpMap!"));
    Map map = ((AMQPMap) message.getAmqpValue().getValue()).getValue();
    for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
        Map.Entry entry = (Map.Entry) iter.next();
        if (!((entry.getKey() instanceof AMQPString && ((AMQPString) entry.getKey()).getValue().equals("key1")) || (entry.getKey() instanceof AMQPString && ((AMQPString) entry.getKey()).getValue().equals("key3")) || (entry.getKey() instanceof AMQPString && ((AMQPString) entry.getKey()).getValue().equals("REPLY")) || (entry.getKey() instanceof AMQPString && ((AMQPString) entry.getKey()).getValue().equals(String.valueOf(Integer.MAX_VALUE + 1))) || (entry.getKey() instanceof AMQPString && ((AMQPString) entry.getKey()).getValue().equals(String.valueOf(Integer.MAX_VALUE + 2))) || (entry.getKey() instanceof AMQPLong && ((AMQPLong) entry.getKey()).getValue() == Integer.MAX_VALUE + 1) || (entry.getKey() instanceof AMQPLong && ((AMQPLong) entry.getKey()).getValue() == Integer.MAX_VALUE + 2))) {
            System.out.println(map);
            throw new Exception("verify - invalid values in map detected: " + map);
        }
    }
}
Also used : AMQPType(com.swiftmq.amqp.v100.types.AMQPType) AMQPMap(com.swiftmq.amqp.v100.types.AMQPMap) AMQPLong(com.swiftmq.amqp.v100.types.AMQPLong) Iterator(java.util.Iterator) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) AMQPMap(com.swiftmq.amqp.v100.types.AMQPMap) Map(java.util.Map) HashMap(java.util.HashMap) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)

Aggregations

AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)23 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)18 AMQPString (com.swiftmq.amqp.v100.types.AMQPString)17 AMQPContext (com.swiftmq.amqp.AMQPContext)9 JSSESocketFactory (com.swiftmq.net.JSSESocketFactory)8 AMQPMap (com.swiftmq.amqp.v100.types.AMQPMap)5 Map (java.util.Map)5 HashMap (java.util.HashMap)4 Connection (com.swiftmq.amqp.v100.client.Connection)3 Session (com.swiftmq.amqp.v100.client.Session)3 Properties (com.swiftmq.amqp.v100.generated.messaging.message_format.Properties)3 TxnIdIF (com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF)3 AMQPByte (com.swiftmq.amqp.v100.types.AMQPByte)3 AMQPType (com.swiftmq.amqp.v100.types.AMQPType)3 AddressIF (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressIF)2 AmqpSequence (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpSequence)2 Data (com.swiftmq.amqp.v100.generated.messaging.message_format.Data)2 AMQPBoolean (com.swiftmq.amqp.v100.types.AMQPBoolean)2 AMQPLong (com.swiftmq.amqp.v100.types.AMQPLong)2 Iterator (java.util.Iterator)2