Search in sources :

Example 6 with SequenceNo

use of com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo 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 7 with SequenceNo

use of com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo in project swiftmq-client by iitsoftware.

the class SessionDispatcher method visit.

public void visit(POFillCache po) {
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", visit, po=" + po + " ...");
    Consumer c = po.getConsumer();
    FlowFrame flowFrame = new FlowFrame(mySession.getChannel());
    flowFrame.setHandle(new Handle(c.getHandle()));
    flowFrame.setAvailable(new AMQPUnsignedInt(0));
    flowFrame.setDrain(AMQPBoolean.FALSE);
    flowFrame.setNextIncomingId(new TransferNumber(nextIncomingId));
    flowFrame.setNextOutgoingId(new TransferNumber(nextOutgoingId));
    flowFrame.setLinkCredit(new AMQPUnsignedInt(po.getLinkCredit()));
    flowFrame.setIncomingWindow(new AMQPUnsignedInt(incomingWindow));
    flowFrame.setOutgoingWindow(new AMQPUnsignedInt(outgoingWindow));
    if (po.getLastDeliveryId() != -1)
        flowFrame.setDeliveryCount(new SequenceNo(po.getLastDeliveryId()));
    TxnIdIF txnIdIF = po.getTxnIdIF();
    if (txnIdIF != null) {
        Map map = new HashMap();
        map.put(new AMQPSymbol("txn-id"), txnIdIF);
        try {
            flowFrame.setProperties(new Fields(map));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    outboundHandler.send(flowFrame);
    if (pTracer.isEnabled())
        pTracer.trace(toString(), ", visit, po=" + po + " done");
}
Also used : TxnIdIF(com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF) IOException(java.io.IOException)

Example 8 with SequenceNo

use of com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo 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 9 with SequenceNo

use of com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo in project swiftmq-client by iitsoftware.

the class FlowFrame method decode.

private void decode() throws Exception {
    List l = body.getValue();
    AMQPType t = null;
    int idx = 0;
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            nextIncomingId = new TransferNumber(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'nextIncomingId' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    if (t.getCode() == AMQPTypeDecoder.NULL)
        throw new Exception("Mandatory field 'incomingWindow' in 'Flow' frame is NULL");
    try {
        incomingWindow = (AMQPUnsignedInt) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'incomingWindow' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    if (t.getCode() == AMQPTypeDecoder.NULL)
        throw new Exception("Mandatory field 'nextOutgoingId' in 'Flow' frame is NULL");
    try {
        nextOutgoingId = new TransferNumber(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'nextOutgoingId' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    if (t.getCode() == AMQPTypeDecoder.NULL)
        throw new Exception("Mandatory field 'outgoingWindow' in 'Flow' frame is NULL");
    try {
        outgoingWindow = (AMQPUnsignedInt) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'outgoingWindow' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            handle = new Handle(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'handle' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            deliveryCount = new SequenceNo(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'deliveryCount' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            linkCredit = (AMQPUnsignedInt) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'linkCredit' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            available = (AMQPUnsignedInt) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'available' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            drain = (AMQPBoolean) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'drain' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            echo = (AMQPBoolean) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'echo' in 'Flow' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            properties = new Fields(((AMQPMap) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'properties' in 'Flow' frame: " + e);
    }
}
Also used : SequenceNo(com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo) Fields(com.swiftmq.amqp.v100.generated.transport.definitions.Fields) TransferNumber(com.swiftmq.amqp.v100.generated.transport.definitions.TransferNumber) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Handle(com.swiftmq.amqp.v100.generated.transport.definitions.Handle)

Example 10 with SequenceNo

use of com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo in project swiftmq-ce by iitsoftware.

the class AMQPSequenceMessageFactory method create.

public AMQPMessage create(int sequenceNo) throws Exception {
    AMQPMessage msg = new AMQPMessage();
    List list = new ArrayList();
    list.add(new AMQPString("key1"));
    list.add(new AMQPLong(Integer.MAX_VALUE + 1));
    list.add(new AMQPString("key3"));
    list.add(new AMQPLong(Integer.MAX_VALUE + 2));
    msg.addAmqpSequence(new AmqpSequence(list));
    return msg;
}
Also used : AMQPLong(com.swiftmq.amqp.v100.types.AMQPLong) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) AMQPString(com.swiftmq.amqp.v100.types.AMQPString) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) AmqpSequence(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpSequence)

Aggregations

AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)7 IOException (java.io.IOException)6 SequenceNo (com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo)5 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)3 AMQPString (com.swiftmq.amqp.v100.types.AMQPString)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 AMQPLong (com.swiftmq.amqp.v100.types.AMQPLong)2 HashMap (java.util.HashMap)2 JMSException (javax.jms.JMSException)2 StreamException (com.rabbitmq.stream.StreamException)1 com.rabbitmq.stream.amqp (com.rabbitmq.stream.amqp)1 AMQPException (com.swiftmq.amqp.v100.client.AMQPException)1 com.swiftmq.amqp.v100.generated.messaging.message_format (com.swiftmq.amqp.v100.generated.messaging.message_format)1 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)1 AmqpSequence (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpSequence)1 Data (com.swiftmq.amqp.v100.generated.messaging.message_format.Data)1 Coordinator (com.swiftmq.amqp.v100.generated.transactions.coordination.Coordinator)1 TxnIdIF (com.swiftmq.amqp.v100.generated.transactions.coordination.TxnIdIF)1