Search in sources :

Example 11 with Producer

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

the class SenderNonTransacted 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> <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 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 producer
        Session session = connection.createSession(50, 50);
        Producer p = session.createProducer(target, toIntQoS(qosS));
        // Send messages non-transacted
        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)));
            p.send(msg);
        }
        Thread.sleep(2000);
        // Close everything down
        p.close();
        session.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Producer(com.swiftmq.amqp.v100.client.Producer) 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 12 with Producer

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

the class ReceiveTester method test.

public void test() {
    try {
        Connection connection = Util.createConnection();
        Session session = Util.createSession(connection);
        Producer producer = session.createProducer(address, qos);
        for (int i = 0; i < nMsgs; i++) {
            AMQPMessage msg = messageFactory.create(i);
            Map map = new HashMap();
            map.put(new AMQPString(PROPNAME), new AMQPInt(i));
            msg.setApplicationProperties(new ApplicationProperties(map));
            producer.send(msg, persistent, 5, -1);
        }
        Consumer consumer = session.createConsumer(address, 500, qos, true, null);
        for (int i = 0; i < nMsgs; i++) {
            AMQPMessage msg = consumer.receive(1000);
            if (msg != null) {
                messageFactory.verify(msg);
                if (!msg.isSettled())
                    msg.accept();
                receiveVerifier.add(msg);
            } else
                throw new Exception("message is null");
        }
        connection.close();
        DeliveryMemory deliveryMemory = consumer.getDeliveryMemory();
        System.out.println("Unsettled: " + deliveryMemory.getNumberUnsettled());
        connection = Util.createConnection();
        session = Util.createSession(connection);
        Consumer consumerRecover = session.createConsumer(address, 500, qos, true, null, deliveryMemory);
        consumerRecover.close();
        session.close();
        connection.close();
        connection = Util.createConnection();
        session = Util.createSession(connection);
        consumer = session.createConsumer(address, 500, qos, true, null);
        for (; ; ) {
            AMQPMessage msg = consumer.receive(1000);
            if (msg != null) {
                messageFactory.verify(msg);
                if (!msg.isSettled())
                    msg.accept();
                receiveVerifier.add(msg);
            } else
                break;
        }
        consumer.close();
        session.close();
        connection.close();
        receiveVerifier.verify();
    } catch (Exception e) {
        e.printStackTrace();
        fail("test failed: " + e);
    }
}
Also used : HashMap(java.util.HashMap) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AMQPInt(com.swiftmq.amqp.v100.types.AMQPInt) ApplicationProperties(com.swiftmq.amqp.v100.generated.messaging.message_format.ApplicationProperties) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) Map(java.util.Map) HashMap(java.util.HashMap)

Example 13 with Producer

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

the class SendTester method test.

public void test() {
    try {
        Connection connection = Util.createConnection();
        Session session = Util.createSession(connection);
        Producer producer = session.createProducer(address, qos);
        for (int i = 0; i < nMsgs; i++) {
            AMQPMessage msg = messageFactory.create(i);
            Map map = new HashMap();
            map.put(new AMQPString(PROPNAME), new AMQPInt(i));
            msg.setApplicationProperties(new ApplicationProperties(map));
            producer.send(msg, persistent, 5, -1);
        }
        connection.close();
        DeliveryMemory deliveryMemory = producer.getDeliveryMemory();
        System.out.println("Unsettled: " + deliveryMemory.getNumberUnsettled());
        connection = Util.createConnection();
        session = Util.createSession(connection);
        Producer producerRecover = session.createProducer(address, qos, deliveryMemory);
        producerRecover.close();
        session.close();
        connection.close();
        connection = Util.createConnection();
        session = Util.createSession(connection);
        Consumer consumer = session.createConsumer(address, 500, qos, true, null);
        for (; ; ) {
            AMQPMessage msg = consumer.receive(1000);
            if (msg != null) {
                messageFactory.verify(msg);
                if (!msg.isSettled())
                    msg.accept();
                receiveVerifier.add(msg);
            } else
                break;
        }
        consumer.close();
        session.close();
        connection.close();
        receiveVerifier.verify();
    } catch (Exception e) {
        e.printStackTrace();
        fail("test failed: " + e);
    }
}
Also used : HashMap(java.util.HashMap) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AMQPInt(com.swiftmq.amqp.v100.types.AMQPInt) ApplicationProperties(com.swiftmq.amqp.v100.generated.messaging.message_format.ApplicationProperties) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with Producer

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

the class Replier method serviceRequests.

public void serviceRequests() {
    try {
        for (int i = 0; i < nMsgs; i++) {
            AMQPMessage request = consumer.receive();
            if (request != null) {
                messageFactory.verify(request);
                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();
                if (replyTo == null)
                    throw new Exception("replyTo not set in request: " + request);
                Producer p = getSession().createProducer(replyTo.getValueString(), qos);
                AMQPMessage reply = messageFactory.createReplyMessage(request);
                Properties prop2 = new Properties();
                prop2.setTo(replyTo);
                prop2.setCorrelationId(prop.getMessageId());
                reply.setProperties(prop2);
                p.send(reply);
                p.close();
            } else
                throw new Exception("Msg == null at i=" + i);
        }
    } catch (Exception e) {
        fail("test failed: " + e);
    }
}
Also used : AddressIF(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressIF) Producer(com.swiftmq.amqp.v100.client.Producer) Properties(com.swiftmq.amqp.v100.generated.messaging.message_format.Properties) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage)

Example 15 with Producer

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

the class Replier method serviceRequests.

public void serviceRequests() {
    try {
        boolean rollback = false;
        int i = 0;
        while (i < nMsgs) {
            TxnIdIF txnIdIF = txc.createTxnId();
            consumer.acquire(1, txnIdIF);
            AMQPMessage request = consumer.receive();
            if (request != null) {
                messageFactory.verify(request);
                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();
                if (replyTo == null)
                    throw new Exception("replyTo not set in request: " + request);
                Producer p = getSession().createProducer(replyTo.getValueString(), qos);
                AMQPMessage reply = messageFactory.createReplyMessage(request);
                reply.setTxnIdIF(txnIdIF);
                Properties prop2 = new Properties();
                prop2.setTo(replyTo);
                prop2.setCorrelationId(prop.getMessageId());
                reply.setProperties(prop2);
                p.send(reply);
                p.close();
            } else
                throw new Exception("Msg == null at i=" + i);
            if (rollback)
                txc.rollback(txnIdIF);
            else {
                txc.commit(txnIdIF);
                i++;
            }
            rollback = !rollback;
        }
    } catch (Exception e) {
        fail("test failed: " + e);
    }
}
Also used : TxnIdIF(com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF) AddressIF(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressIF) Producer(com.swiftmq.amqp.v100.client.Producer) Properties(com.swiftmq.amqp.v100.generated.messaging.message_format.Properties) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage)

Aggregations

AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)13 AddressIF (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressIF)7 Properties (com.swiftmq.amqp.v100.generated.messaging.message_format.Properties)7 AMQPString (com.swiftmq.amqp.v100.types.AMQPString)7 Producer (com.swiftmq.amqp.v100.client.Producer)6 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)6 TxnIdIF (com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF)6 AMQPContext (com.swiftmq.amqp.AMQPContext)5 JSSESocketFactory (com.swiftmq.net.JSSESocketFactory)4 ApplicationProperties (com.swiftmq.amqp.v100.generated.messaging.message_format.ApplicationProperties)2 AMQPInt (com.swiftmq.amqp.v100.types.AMQPInt)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Connection (com.swiftmq.amqp.v100.client.Connection)1 Session (com.swiftmq.amqp.v100.client.Session)1 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)1 Coordinator (com.swiftmq.amqp.v100.generated.transactions.coordination.Coordinator)1 TransactionalState (com.swiftmq.amqp.v100.generated.transactions.coordination.TransactionalState)1 Packager (com.swiftmq.amqp.v100.transport.Packager)1