Search in sources :

Example 11 with AMQPType

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

the class Target method decode.

private void decode() throws Exception {
    List l = getValue();
    AMQPType t = null;
    int idx = 0;
    // Factory  : AddressFactory
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    if (t.getCode() != AMQPTypeDecoder.NULL)
        address = AddressFactory.create(t);
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            durable = new TerminusDurability(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'durable' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            expiryPolicy = new TerminusExpiryPolicy(((AMQPSymbol) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'expiryPolicy' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            timeout = new Seconds(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'timeout' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            dynamic = (AMQPBoolean) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'dynamic' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            dynamicNodeProperties = new NodeProperties(((AMQPMap) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'dynamicNodeProperties' in 'Target' type: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            capabilities = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'capabilities' in 'Target' type: " + e);
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) Seconds(com.swiftmq.amqp.v100.generated.transport.definitions.Seconds) IOException(java.io.IOException)

Example 12 with AMQPType

use of com.swiftmq.amqp.v100.types.AMQPType 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 13 with AMQPType

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

Example 14 with AMQPType

use of com.swiftmq.amqp.v100.types.AMQPType 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 15 with AMQPType

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

the class OpenFrame 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++);
    if (t.getCode() == AMQPTypeDecoder.NULL)
        throw new Exception("Mandatory field 'containerId' in 'Open' frame is NULL");
    try {
        containerId = (AMQPString) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'containerId' in 'Open' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            hostname = (AMQPString) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'hostname' in 'Open' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            maxFrameSize = (AMQPUnsignedInt) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'maxFrameSize' in 'Open' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            channelMax = (AMQPUnsignedShort) t;
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'channelMax' in 'Open' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            idleTimeOut = new Milliseconds(((AMQPUnsignedInt) t).getValue());
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'idleTimeOut' in 'Open' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            outgoingLocales = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'outgoingLocales' in 'Open' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            incomingLocales = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'incomingLocales' in 'Open' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            offeredCapabilities = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'offeredCapabilities' in 'Open' frame: " + e);
    }
    // Factory  : ./.
    if (idx >= l.size())
        return;
    t = (AMQPType) l.get(idx++);
    try {
        if (t.getCode() != AMQPTypeDecoder.NULL)
            desiredCapabilities = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
    } catch (ClassCastException e) {
        throw new Exception("Invalid type of field 'desiredCapabilities' in 'Open' 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 'Open' frame: " + e);
    }
}
Also used : Fields(com.swiftmq.amqp.v100.generated.transport.definitions.Fields) List(java.util.List) ArrayList(java.util.ArrayList) Milliseconds(com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)20 List (java.util.List)14 ArrayList (java.util.ArrayList)13 Map (java.util.Map)6 Iterator (java.util.Iterator)5 AmqpValue (com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue)4 Error (com.swiftmq.amqp.v100.generated.transport.definitions.Error)4 Fields (com.swiftmq.amqp.v100.generated.transport.definitions.Fields)4 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)4 Handle (com.swiftmq.amqp.v100.generated.transport.definitions.Handle)3 SequenceNo (com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo)3 AMQPType (com.swiftmq.amqp.v100.types.AMQPType)3 AMQPException (com.swiftmq.amqp.v100.client.AMQPException)2 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)2 Coordinator (com.swiftmq.amqp.v100.generated.transactions.coordination.Coordinator)2 Declared (com.swiftmq.amqp.v100.generated.transactions.coordination.Declared)2 Milliseconds (com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds)2 Seconds (com.swiftmq.amqp.v100.generated.transport.definitions.Seconds)2 TransferNumber (com.swiftmq.amqp.v100.generated.transport.definitions.TransferNumber)2 AMQPString (com.swiftmq.amqp.v100.types.AMQPString)2