Search in sources :

Example 1 with DurableConsumer

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

the class SessionDispatcher method visit.

public void visit(POAttachDurableConsumer po) {
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", visit, po=" + po + " ...");
    DeliveryMemory deliveryMemory = po.getDeliveryMemory();
    if (deliveryMemory.getLinkName() != null)
        deliveryMemory.setLinkName(po.getLinkName());
    DurableConsumer consumer = new DurableConsumer(mySession, po.getSource(), po.getLinkName(), po.getLinkCredit(), po.getQoS(), deliveryMemory);
    int handle = ArrayListTool.setFirstFreeOrExpand(handles, consumer);
    consumer.setHandle(handle);
    po.setLink(consumer);
    waitingPO.put(po.getLinkName(), po);
    try {
        AttachFrame attachFrame = new AttachFrame(mySession.getChannel());
        attachFrame.setName(new AMQPString(po.getLinkName()));
        attachFrame.setHandle(new Handle(handle));
        attachFrame.setRole(Role.RECEIVER);
        if (consumer.getQoS() == QoS.AT_MOST_ONCE)
            attachFrame.setSndSettleMode(SenderSettleMode.SETTLED);
        Source source = new Source();
        String s = po.getSource();
        if (s != null)
            source.setAddress(new AddressString(s));
        else
            source.setDynamic(AMQPBoolean.TRUE);
        // This identifies a durable
        source.setDurable(TerminusDurability.CONFIGURATION);
        source.setExpiryPolicy(po.getExpiryPolicy());
        source.setTimeout(new Seconds(0));
        Map m = null;
        if (po.isNoLocal()) {
            m = new HashMap();
            m.put(new AMQPSymbol("no-local-filter"), new NoLocalFilter());
        }
        if (po.getSelector() != null) {
            if (m == null)
                m = new HashMap();
            m.put(new AMQPSymbol("jms-selector-filter"), new SelectorFilter(po.getSelector()));
        }
        if (m != null)
            source.setFilter(new FilterSet(m));
        attachFrame.setSource(source);
        Target target = new Target();
        target.setAddress(new AddressString(uniqueSessionId + "/" + po.getSource() + "/" + (nextLinkId++)));
        target.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
        target.setTimeout(new Seconds(0));
        attachFrame.setTarget(target);
        attachFrame.setUnsettled(getUnsettledMap(consumer.getDeliveryMemory()));
        outboundHandler.send(attachFrame);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", visit, po=" + po + " done");
}
Also used : AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) NoLocalFilter(com.swiftmq.amqp.v100.generated.filter.filter_types.NoLocalFilter) AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) IOException(java.io.IOException) SelectorFilter(com.swiftmq.amqp.v100.generated.filter.filter_types.SelectorFilter)

Example 2 with DurableConsumer

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

the class DurableConsumer method unsubscribe.

/**
 * Unsubscribes the durable consumer and destroys the durable link.
 *
 * @throws AMQPException
 */
public void unsubscribe() throws AMQPException {
    if (!closed)
        close();
    Semaphore sem = new Semaphore();
    POAttachDurableConsumer po = new POAttachDurableConsumer(sem, name, source, linkCredit, qoS, false, null, TerminusExpiryPolicy.LINK_DETACH, deliveryMemory);
    mySession.getSessionDispatcher().dispatch(po);
    sem.waitHere();
    if (!po.isSuccess())
        throw new AMQPException(po.getException());
    DurableConsumer c = (DurableConsumer) po.getLink();
    c.close();
}
Also used : POAttachDurableConsumer(com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer) Semaphore(com.swiftmq.tools.concurrent.Semaphore) POAttachDurableConsumer(com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer)

Example 3 with DurableConsumer

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

the class ReceiverNonTransactedDurable 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> <containerid> <durablelinkname> <keep> <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 = "testtopic";
    int nMsgs = 100;
    String qosS = "EXACTLY_ONCE";
    String cid = "testcid";
    String durableName = "testdurable";
    boolean keepDurable = false;
    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)
        cid = args[5];
    if (args.length >= 7)
        durableName = args[6];
    if (args.length >= 8)
        keepDurable = Boolean.parseBoolean(args[7]);
    if (args.length >= 9)
        authAnon = Boolean.parseBoolean(args[8]);
    if (args.length >= 10)
        user = args[9];
    if (args.length >= 11)
        password = args[10];
    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("Container Id: " + cid);
    System.out.println("Durable Name: " + durableName);
    System.out.println("Keep Durable: " + keepDurable);
    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 < 10)
            connection = new Connection(ctx, host, port, authAnon);
        else
            connection = new Connection(ctx, host, port, user, password);
        connection.setContainerId(cid);
        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);
        DurableConsumer c = session.createDurableConsumer(durableName, source, 100, toIntQoS(qosS), false, 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();
        if (!keepDurable)
            c.unsubscribe();
        session.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Connection(com.swiftmq.amqp.v100.client.Connection) AMQPContext(com.swiftmq.amqp.AMQPContext) DurableConsumer(com.swiftmq.amqp.v100.client.DurableConsumer) 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)

Aggregations

AMQPContext (com.swiftmq.amqp.AMQPContext)1 Connection (com.swiftmq.amqp.v100.client.Connection)1 DurableConsumer (com.swiftmq.amqp.v100.client.DurableConsumer)1 Session (com.swiftmq.amqp.v100.client.Session)1 POAttachDurableConsumer (com.swiftmq.amqp.v100.client.po.POAttachDurableConsumer)1 NoLocalFilter (com.swiftmq.amqp.v100.generated.filter.filter_types.NoLocalFilter)1 SelectorFilter (com.swiftmq.amqp.v100.generated.filter.filter_types.SelectorFilter)1 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)1 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)1 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)1 AMQPString (com.swiftmq.amqp.v100.types.AMQPString)1 JSSESocketFactory (com.swiftmq.net.JSSESocketFactory)1 Semaphore (com.swiftmq.tools.concurrent.Semaphore)1 IOException (java.io.IOException)1