Search in sources :

Example 6 with AMQPBinary

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

the class Producer method recover.

protected void recover(AMQPMap remoteUnsettled) {
    try {
        if (remoteUnsettled != null) {
            Map<AMQPType, AMQPType> map = remoteUnsettled.getValue();
            for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
                Map.Entry entry = (Map.Entry) iter.next();
                final DeliveryTag deliveryTag = new DeliveryTag(((AMQPBinary) entry.getKey()).getValue());
                final AMQPList deliveryState = (AMQPList) entry.getValue();
                if (deliveryState != null) {
                    try {
                        DeliveryStateFactory.create(deliveryState).accept(new DeliveryStateVisitorAdapter() {

                            public void visit(Accepted impl) {
                                deliveryMemory.deliverySettled(deliveryTag);
                                mySession.dispatch(new POSendResumedTransfer(Producer.this, deliveryTag));
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (deliveryMemory.getNumberUnsettled() > 0) {
            Collection<UnsettledDelivery> unsettled = deliveryMemory.getUnsettled();
            for (Iterator<UnsettledDelivery> iter = unsettled.iterator(); iter.hasNext(); ) {
                UnsettledDelivery unsettledDelivery = iter.next();
                if (unsettledDelivery.getMessage() != null) {
                    AMQPMessage msg = unsettledDelivery.getMessage();
                    if (msg.getTxnIdIF() == null) {
                        POSendMessage po = new POSendMessage(null, this, msg, null, unsettledDelivery.getDeliveryTag());
                        po.setRecovery(true);
                        mySession.dispatch(po);
                    }
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : DeliveryStateVisitorAdapter(com.swiftmq.amqp.v100.generated.messaging.delivery_state.DeliveryStateVisitorAdapter) POSendResumedTransfer(com.swiftmq.amqp.v100.client.po.POSendResumedTransfer) POSendMessage(com.swiftmq.amqp.v100.client.po.POSendMessage) IOException(java.io.IOException) DeliveryTag(com.swiftmq.amqp.v100.generated.transport.definitions.DeliveryTag) AMQPMessage(com.swiftmq.amqp.v100.messaging.AMQPMessage) Accepted(com.swiftmq.amqp.v100.generated.messaging.delivery_state.Accepted) IOException(java.io.IOException) Iterator(java.util.Iterator) Map(java.util.Map)

Example 7 with AMQPBinary

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

use of com.swiftmq.amqp.v100.types.AMQPBinary in project swiftmq-ce by iitsoftware.

the class BodyTypeMessageFactory method create.

public MessageImpl create(AMQPMessage source) throws Exception {
    List data = source.getData();
    if (data != null && data.size() > 0) {
        if (data.size() == 1) {
            byte[] b = ((Data) data.get(0)).getValue();
            return new BytesMessageImpl(b, b.length);
        } else {
            BytesMessageImpl msg = new BytesMessageImpl();
            for (int i = 0; i < data.size(); i++) {
                msg.writeBytes(((Data) data.get(i)).getValue());
            }
            return msg;
        }
    }
    List sequenceList = source.getAmqpSequence();
    if (sequenceList != null && sequenceList.size() > 0) {
        StreamMessageImpl msg = new StreamMessageImpl();
        for (int i = 0; i < sequenceList.size(); i++) {
            AmqpSequence seq = (AmqpSequence) sequenceList.get(i);
            List list = seq.getValue();
            if (list != null && list.size() > 0) {
                for (int j = 0; j < list.size(); j++) {
                    try {
                        msg.writeObject(Util.convertAMQPtoJMS((AMQPType) list.get(j)));
                    } catch (Exception e) {
                        msg.setBooleanProperty(PROP_ERROR, true);
                        msg.setStringProperty(PROP_ERROR_CAUSE, e.toString());
                        break;
                    }
                }
            }
        }
        return msg;
    }
    AmqpValue amqpValue = source.getAmqpValue();
    if (amqpValue != null) {
        AMQPType value = amqpValue.getValue();
        AMQPDescribedConstructor constructor = value.getConstructor();
        int code = constructor != null ? constructor.getFormatCode() : value.getCode();
        if (AMQPTypeDecoder.isSymbol(code) || AMQPTypeDecoder.isString(code)) {
            TextMessageImpl msg = new TextMessageImpl();
            msg.setText((String) Util.convertAMQPtoJMS(value));
            return msg;
        }
        if (AMQPTypeDecoder.isBinary(code)) {
            byte[] b = ((AMQPBinary) value).getValue();
            return new BytesMessageImpl(b, b.length);
        }
        if (AMQPTypeDecoder.isList(code)) {
            StreamMessageImpl msg = new StreamMessageImpl();
            List list = ((AMQPList) value).getValue();
            for (int i = 0; i < list.size(); i++) {
                try {
                    msg.writeObject(Util.convertAMQPtoJMS((AMQPType) list.get(i)));
                } catch (Exception e) {
                    msg.setBooleanProperty(PROP_ERROR, true);
                    msg.setStringProperty(PROP_ERROR_CAUSE, e.toString());
                    break;
                }
            }
            return msg;
        }
        if (AMQPTypeDecoder.isMap(code)) {
            MapMessageImpl msg = new MapMessageImpl();
            Map map = ((AMQPMap) value).getValue();
            for (Iterator iter = map.entrySet().iterator(); iter.hasNext(); ) {
                Map.Entry entry = (Map.Entry) iter.next();
                try {
                    String name = Util.convertAMQPtoJMS((AMQPType) entry.getKey()).toString();
                    msg.setObject(name, Util.convertAMQPtoJMS((AMQPType) entry.getValue()));
                } catch (Exception e) {
                    msg.setBooleanProperty(PROP_ERROR, true);
                    msg.setStringProperty(PROP_ERROR_CAUSE, e.toString());
                    break;
                }
            }
            return msg;
        }
        // Everything else is a ObjectMessage
        ObjectMessageImpl msg = new ObjectMessageImpl();
        try {
            msg.setObject((Serializable) Util.convertAMQPtoJMS(value));
        } catch (Exception e) {
            msg.setBooleanProperty(PROP_ERROR, true);
            msg.setStringProperty(PROP_ERROR_CAUSE, e.toString());
        }
        return msg;
    }
    return new MessageImpl();
}
Also used : Data(com.swiftmq.amqp.v100.generated.messaging.message_format.Data) AmqpSequence(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpSequence) AmqpValue(com.swiftmq.amqp.v100.generated.messaging.message_format.AmqpValue) Iterator(java.util.Iterator) List(java.util.List) Map(java.util.Map)

Example 9 with AMQPBinary

use of com.swiftmq.amqp.v100.types.AMQPBinary in project swiftmq-ce by iitsoftware.

the class JMSMappingInboundTransformer method transformProperties.

protected void transformProperties(Properties properties, final MessageImpl jmsMessage, final DestinationFactory destinationFactory) throws Exception {
    if (overwriteMessageId) {
        jmsMessage.setJMSMessageID(nextMsgId());
    } else {
        MessageIdIF messageIdIF = properties.getMessageId();
        if (messageIdIF != null)
            messageIdIF.accept(new MessageIdVisitor() {

                public void visit(MessageIdUlong messageIdUlong) {
                    try {
                        jmsMessage.setJMSMessageID(String.valueOf(messageIdUlong.getValue()));
                    } catch (JMSException e) {
                    }
                }

                public void visit(MessageIdUuid messageIdUuid) {
                    try {
                        jmsMessage.setJMSMessageID(String.valueOf(messageIdUuid.getValue()));
                    } catch (JMSException e) {
                    }
                }

                public void visit(MessageIdBinary messageIdBinary) {
                    try {
                        jmsMessage.setJMSMessageID(new String(messageIdBinary.getValue()));
                    } catch (JMSException e) {
                    }
                }

                public void visit(MessageIdString messageIdString) {
                    try {
                        jmsMessage.setJMSMessageID(messageIdString.getValue());
                    } catch (JMSException e) {
                    }
                }
            });
    }
    AMQPBinary userId = properties.getUserId();
    if (userId != null)
        jmsMessage.setStringProperty(MessageImpl.PROP_USER_ID, new String(userId.getValue(), "ISO-8859-1"));
    AddressIF to = properties.getTo();
    if (to != null) {
        jmsMessage.setJMSDestination(destinationFactory.create(to));
        to.accept(new AddressVisitor() {

            public void visit(AddressString addressString) {
                try {
                    jmsMessage.setStringProperty(Util.PROP_AMQP_TO_ADDRESS, addressString.getValue());
                } catch (JMSException e) {
                }
            }
        });
    }
    AMQPString subject = properties.getSubject();
    if (subject != null)
        jmsMessage.setStringProperty(prefixVendor + "Subject", subject.getValue());
    AddressIF replyTo = properties.getReplyTo();
    if (replyTo != null)
        jmsMessage.setJMSReplyTo(destinationFactory.create(replyTo));
    MessageIdIF correlationIdIF = properties.getCorrelationId();
    if (correlationIdIF != null)
        correlationIdIF.accept(new MessageIdVisitor() {

            public void visit(MessageIdUlong messageIdUlong) {
                try {
                    jmsMessage.setJMSCorrelationID(String.valueOf(messageIdUlong.getValue()));
                } catch (JMSException e) {
                }
            }

            public void visit(MessageIdUuid messageIdUuid) {
                try {
                    jmsMessage.setJMSCorrelationID(String.valueOf(messageIdUuid.getValue()));
                } catch (JMSException e) {
                }
            }

            public void visit(MessageIdBinary messageIdBinary) {
                try {
                    jmsMessage.setJMSCorrelationID(new String(messageIdBinary.getValue()));
                } catch (JMSException e) {
                }
            }

            public void visit(MessageIdString messageIdString) {
                try {
                    jmsMessage.setJMSCorrelationID(messageIdString.getValue());
                } catch (JMSException e) {
                }
            }
        });
    AMQPSymbol contentType = properties.getContentType();
    if (contentType != null)
        jmsMessage.setStringProperty(prefixVendor + "ContentType", contentType.getValue());
    AMQPSymbol contentEncoding = properties.getContentEncoding();
    if (contentEncoding != null)
        jmsMessage.setStringProperty(prefixVendor + "ContentEncoding", contentEncoding.getValue());
    AMQPTimestamp ts = properties.getAbsoluteExpiryTime();
    if (ts != null)
        jmsMessage.setJMSExpiration(ts.getValue());
    AMQPTimestamp ct = properties.getCreationTime();
    if (ct != null)
        jmsMessage.setJMSTimestamp(ct.getValue());
    AMQPString groupId = properties.getGroupId();
    if (groupId != null)
        jmsMessage.setStringProperty(Util.PROP_GROUP_ID, groupId.getValue());
    SequenceNo groupSeq = properties.getGroupSequence();
    if (groupSeq != null)
        jmsMessage.setLongProperty(Util.PROP_GROUP_SEQ, groupSeq.getValue());
    AMQPString replyToGroupId = properties.getReplyToGroupId();
    if (replyToGroupId != null)
        jmsMessage.setStringProperty(prefixVendor + "ReplyToGroupID", replyToGroupId.getValue());
}
Also used : SequenceNo(com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo) JMSException(javax.jms.JMSException)

Aggregations

IOException (java.io.IOException)6 Map (java.util.Map)5 SequenceNo (com.swiftmq.amqp.v100.generated.transport.definitions.SequenceNo)4 AMQPMessage (com.swiftmq.amqp.v100.messaging.AMQPMessage)4 Iterator (java.util.Iterator)3 POSendMessage (com.swiftmq.amqp.v100.client.po.POSendMessage)2 AddressString (com.swiftmq.amqp.v100.generated.messaging.message_format.AddressString)2 Data (com.swiftmq.amqp.v100.generated.messaging.message_format.Data)2 Properties (com.swiftmq.amqp.v100.generated.messaging.message_format.Properties)2 Milliseconds (com.swiftmq.amqp.v100.generated.transport.definitions.Milliseconds)2 HashMap (java.util.HashMap)2 List (java.util.List)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 POSendResumedTransfer (com.swiftmq.amqp.v100.client.po.POSendResumedTransfer)1 Accepted (com.swiftmq.amqp.v100.generated.messaging.delivery_state.Accepted)1 DeliveryStateVisitorAdapter (com.swiftmq.amqp.v100.generated.messaging.delivery_state.DeliveryStateVisitorAdapter)1 com.swiftmq.amqp.v100.generated.messaging.message_format (com.swiftmq.amqp.v100.generated.messaging.message_format)1