Search in sources :

Example 21 with AMQPString

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

the class SessionDispatcher method visit.

public void visit(POAttachProducer po) {
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", visit, po=" + po + " ...");
    String name = null;
    DeliveryMemory deliveryMemory = po.getDeliveryMemory();
    if (deliveryMemory.getLinkName() != null)
        name = deliveryMemory.getLinkName();
    else {
        name = uniqueSessionId + "/" + po.getTarget() + "/" + (nextLinkId++);
        deliveryMemory.setLinkName(name);
    }
    Producer producer = new Producer(mySession, po.getTarget(), name, po.getQoS(), deliveryMemory);
    int handle = ArrayListTool.setFirstFreeOrExpand(handles, producer);
    producer.setHandle(handle);
    po.setLink(producer);
    waitingPO.put(name, po);
    try {
        AttachFrame attachFrame = new AttachFrame(mySession.getChannel());
        attachFrame.setName(new AMQPString(name));
        attachFrame.setHandle(new Handle(handle));
        attachFrame.setRole(Role.SENDER);
        switch(producer.getQoS()) {
            case QoS.AT_LEAST_ONCE:
                attachFrame.setRcvSettleMode(ReceiverSettleMode.FIRST);
                break;
            case QoS.AT_MOST_ONCE:
                attachFrame.setSndSettleMode(SenderSettleMode.SETTLED);
                break;
            case QoS.EXACTLY_ONCE:
                attachFrame.setRcvSettleMode(ReceiverSettleMode.SECOND);
                break;
        }
        Source source = new Source();
        source.setAddress(new AddressString(name));
        source.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
        source.setTimeout(new Seconds(0));
        attachFrame.setSource(source);
        String t = po.getTarget();
        if (t.equals(Coordinator.DESCRIPTOR_NAME)) {
            Coordinator coordinator = new Coordinator();
            coordinator.setCapabilities(new AMQPArray(AMQPTypeDecoder.SYM8, new AMQPType[] { TxnCapability.LOCAL_TRANSACTIONS }));
            attachFrame.setTarget(coordinator);
        } else {
            Target target = new Target();
            target.setAddress(new AddressString(t));
            target.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
            target.setTimeout(new Seconds(0));
            attachFrame.setTarget(target);
        }
        attachFrame.setInitialDeliveryCount(new SequenceNo(producer.getDeliveryCountSnd()));
        attachFrame.setUnsettled(getUnsettledMap(producer.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) AddressString(com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString) Coordinator(com.swiftmq.amqp.v100.generated.transactions.coordination.Coordinator) IOException(java.io.IOException)

Example 22 with AMQPString

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

the class SessionDispatcher method visit.

public void visit(POAttachConsumer po) {
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", visit, po=" + po + " ...");
    String name = null;
    DeliveryMemory deliveryMemory = po.getDeliveryMemory();
    if (deliveryMemory.getLinkName() != null)
        name = deliveryMemory.getLinkName();
    else {
        name = uniqueSessionId + "/" + po.getSource() + "/" + (nextLinkId++);
        deliveryMemory.setLinkName(name);
    }
    Consumer consumer = null;
    if (po.getLinkCredit() == -1)
        consumer = new Consumer(mySession, po.getSource(), name, po.getQoS(), deliveryMemory);
    else
        consumer = new Consumer(mySession, po.getSource(), name, po.getLinkCredit(), po.getQoS(), deliveryMemory);
    int handle = ArrayListTool.setFirstFreeOrExpand(handles, consumer);
    consumer.setHandle(handle);
    po.setLink(consumer);
    waitingPO.put(name, po);
    try {
        AttachFrame attachFrame = new AttachFrame(mySession.getChannel());
        attachFrame.setName(new AMQPString(name));
        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);
        source.setDurable(TerminusDurability.NONE);
        source.setExpiryPolicy(TerminusExpiryPolicy.LINK_DETACH);
        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(name));
        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 23 with AMQPString

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

the class Connection method close.

protected void close(String condition, String description) {
    if (closed)
        return;
    if (condition == null) {
        Semaphore sem = new Semaphore();
        POSendClose po = new POSendClose(sem, condition == null ? null : new AMQPSymbol(condition), description == null ? null : new AMQPString(description));
        connectionDispatcher.dispatch(po);
        sem.waitHere();
    } else {
        POSendClose po = new POSendClose(null, condition == null ? null : new AMQPSymbol(condition), description == null ? null : new AMQPString(description));
        connectionDispatcher.dispatch(po);
    }
    cancel();
    if (exceptionListener != null && condition != null && description != null)
        exceptionListener.onException(new ConnectionClosedException(condition + " / " + description));
}
Also used : AMQPSymbol(com.swiftmq.amqp.v100.types.AMQPSymbol) Semaphore(com.swiftmq.tools.concurrent.Semaphore) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) POSendClose(com.swiftmq.amqp.v100.client.po.POSendClose)

Example 24 with AMQPString

use of com.swiftmq.amqp.v100.types.AMQPString in project rabbitmq-stream-java-client by rabbitmq.

the class SwiftMqCodec method encode.

@Override
public EncodedMessage encode(Message message) {
    AMQPMessage outboundMessage;
    if (message instanceof SwiftMqAmqpMessageWrapper) {
        outboundMessage = ((SwiftMqAmqpMessageWrapper) message).message;
    } else {
        outboundMessage = new AMQPMessage();
        if (message.getProperties() != null) {
            com.rabbitmq.stream.Properties headers = message.getProperties();
            com.swiftmq.amqp.v100.generated.messaging.message_format.Properties properties = new com.swiftmq.amqp.v100.generated.messaging.message_format.Properties();
            boolean propertiesSet = false;
            if (headers.getMessageId() != null) {
                if (headers.getMessageId() instanceof String) {
                    properties.setMessageId(new MessageIdString(headers.getMessageIdAsString()));
                } else if (headers.getMessageId().getClass().isPrimitive() || headers.getMessageId() instanceof Long) {
                    properties.setMessageId(new MessageIdUlong(headers.getMessageIdAsLong()));
                } else if (headers.getMessageId().getClass().isArray()) {
                    properties.setMessageId(new MessageIdBinary(headers.getMessageIdAsBinary()));
                } else if (headers.getMessageId() instanceof UUID) {
                    properties.setMessageId(new MessageIdUuid(headers.getMessageIdAsUuid()));
                } else {
                    throw new IllegalStateException("Type not supported for message ID:" + properties.getMessageId().getClass());
                }
                propertiesSet = true;
            }
            if (headers.getUserId() != null) {
                properties.setUserId(new AMQPBinary(headers.getUserId()));
                propertiesSet = true;
            }
            if (headers.getTo() != null) {
                properties.setTo(new AddressString(headers.getTo()));
                propertiesSet = true;
            }
            if (headers.getSubject() != null) {
                properties.setSubject(new AMQPString(headers.getSubject()));
                propertiesSet = true;
            }
            if (headers.getReplyTo() != null) {
                properties.setReplyTo(new AddressString(headers.getReplyTo()));
                propertiesSet = true;
            }
            if (headers.getCorrelationId() != null) {
                if (headers.getCorrelationId() instanceof String) {
                    properties.setCorrelationId(new MessageIdString(headers.getCorrelationIdAsString()));
                } else if (headers.getCorrelationId().getClass().isPrimitive() || headers.getCorrelationId() instanceof Long) {
                    properties.setCorrelationId(new MessageIdUlong(headers.getCorrelationIdAsLong()));
                } else if (headers.getCorrelationId().getClass().isArray()) {
                    properties.setCorrelationId(new MessageIdBinary(headers.getCorrelationIdAsBinary()));
                } else if (headers.getCorrelationId() instanceof UUID) {
                    properties.setCorrelationId(new MessageIdUuid(headers.getCorrelationIdAsUuid()));
                } else {
                    throw new IllegalStateException("Type not supported for correlation ID:" + properties.getCorrelationId().getClass());
                }
                propertiesSet = true;
            }
            if (headers.getContentType() != null) {
                properties.setContentType(new AMQPSymbol(headers.getContentType()));
                propertiesSet = true;
            }
            if (headers.getContentEncoding() != null) {
                properties.setContentEncoding(new AMQPSymbol(headers.getContentEncoding()));
                propertiesSet = true;
            }
            if (headers.getAbsoluteExpiryTime() > 0) {
                properties.setAbsoluteExpiryTime(new AMQPTimestamp(headers.getAbsoluteExpiryTime()));
                propertiesSet = true;
            }
            if (headers.getCreationTime() > 0) {
                properties.setCreationTime(new AMQPTimestamp(headers.getCreationTime()));
                propertiesSet = true;
            }
            if (headers.getGroupId() != null) {
                properties.setGroupId(new AMQPString(headers.getGroupId()));
                propertiesSet = true;
            }
            if (headers.getGroupSequence() >= 0) {
                properties.setGroupSequence(new SequenceNo(headers.getGroupSequence()));
                propertiesSet = true;
            }
            if (headers.getReplyToGroupId() != null) {
                properties.setReplyToGroupId(new AMQPString(headers.getReplyToGroupId()));
                propertiesSet = true;
            }
            if (propertiesSet) {
                outboundMessage.setProperties(properties);
            }
        }
        if (message.getApplicationProperties() != null && !message.getApplicationProperties().isEmpty()) {
            Map<AMQPType, AMQPType> applicationProperties = new LinkedHashMap<>(message.getApplicationProperties().size());
            for (Map.Entry<String, Object> entry : message.getApplicationProperties().entrySet()) {
                applicationProperties.put(new AMQPString(entry.getKey()), convertToSwiftMqType(entry.getValue()));
            }
            try {
                outboundMessage.setApplicationProperties(new ApplicationProperties(applicationProperties));
            } catch (IOException e) {
                throw new StreamException("Error while setting application properties", e);
            }
        }
        if (message.getMessageAnnotations() != null && !message.getMessageAnnotations().isEmpty()) {
            Map<AMQPType, AMQPType> messageAnnotations = new LinkedHashMap<>(message.getMessageAnnotations().size());
            for (Map.Entry<String, Object> entry : message.getMessageAnnotations().entrySet()) {
                messageAnnotations.put(new AMQPSymbol(entry.getKey()), convertToSwiftMqType(entry.getValue()));
            }
            try {
                outboundMessage.setMessageAnnotations(new MessageAnnotations(messageAnnotations));
            } catch (IOException e) {
                throw new StreamException("Error while setting message annotations", e);
            }
        }
        if (message.getBodyAsBinary() != null) {
            outboundMessage.addData(new Data(message.getBodyAsBinary()));
        }
    }
    try {
        int bufferSize;
        // FIXME estimate the size with all message data
        if (outboundMessage.getData() != null && !outboundMessage.getData().isEmpty()) {
            bufferSize = outboundMessage.getData().get(0).getPredictedSize();
        } else {
            bufferSize = 8192;
        }
        DataByteArrayOutputStream output = new DataByteArrayOutputStream(bufferSize);
        outboundMessage.writeContent(output);
        return new EncodedMessage(output.getCount(), output.getBuffer());
    } catch (IOException e) {
        throw new StreamException("Error while writing AMQP 1.0 message to output stream");
    }
}
Also used : SequenceNo(com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo) DataByteArrayOutputStream(com.swiftmq.tools.util.DataByteArrayOutputStream) com.swiftmq.amqp.v100.generated.messaging.message_format(com.swiftmq.amqp.v100.generated.messaging.message_format) LinkedHashMap(java.util.LinkedHashMap) StreamException(com.rabbitmq.stream.StreamException) UUID(java.util.UUID) com.rabbitmq.stream.amqp(com.rabbitmq.stream.amqp) IOException(java.io.IOException) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 25 with AMQPString

use of com.swiftmq.amqp.v100.types.AMQPString 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)

Aggregations

AMQPString (com.swiftmq.amqp.v100.types.AMQPString)29 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)24 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)17 HashMap (java.util.HashMap)14 Map (java.util.Map)14 IOException (java.io.IOException)13 AMQPContext (com.swiftmq.amqp.AMQPContext)9 AMQPMap (com.swiftmq.amqp.v100.types.AMQPMap)8 JSSESocketFactory (com.swiftmq.net.JSSESocketFactory)8 ApplicationProperties (com.swiftmq.amqp.v100.generated.messaging.message_format.ApplicationProperties)6 Properties (com.swiftmq.amqp.v100.generated.messaging.message_format.Properties)6 QueueException (com.swiftmq.swiftlet.queue.QueueException)5 AMQPException (com.swiftmq.amqp.v100.client.AMQPException)4 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)4 TxnIdIF (com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF)4 Milliseconds (com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds)4 SequenceNo (com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo)4 AMQPBoolean (com.swiftmq.amqp.v100.types.AMQPBoolean)4 Iterator (java.util.Iterator)4 Connection (com.swiftmq.amqp.v100.client.Connection)3