Search in sources :

Example 21 with Header

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.

the class PropertyConverter_1_0_to_0_10Test method testArrivalTimeConversion.

@Test
public void testArrivalTimeConversion() {
    final long arrivalTime = System.currentTimeMillis() - 10000;
    Message_1_0 message = createTestMessage(new Header(), arrivalTime);
    final MessageTransferMessage convertedMessage = _messageConverter.convert(message, _namedAddressSpace);
    final DeliveryProperties deliveryProperties = convertedMessage.getStoredMessage().getMetaData().getDeliveryProperties();
    assertEquals("Unexpected timestamp", arrivalTime, deliveryProperties.getTimestamp());
}
Also used : Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) MessageTransferMessage(org.apache.qpid.server.protocol.v0_10.MessageTransferMessage) Message_1_0(org.apache.qpid.server.protocol.v1_0.Message_1_0) DeliveryProperties(org.apache.qpid.server.protocol.v0_10.transport.DeliveryProperties) Test(org.junit.Test)

Example 22 with Header

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.

the class PropertyConverter_1_0_to_0_8Test method testTTLConversion.

@Test
public void testTTLConversion() {
    long ttl = 10000;
    long arrivalTime = System.currentTimeMillis();
    long expectedExpiration = arrivalTime + ttl;
    Header header = new Header();
    header.setTtl(UnsignedInteger.valueOf(ttl));
    Message_1_0 message = createTestMessage(header, arrivalTime);
    final AMQMessage convertedMessage = _messageConverter.convert(message, _namedAddressSpace);
    BasicContentHeaderProperties convertedProperties = convertedMessage.getContentHeaderBody().getProperties();
    assertEquals("Unexpected expiration", expectedExpiration, convertedProperties.getExpiration());
}
Also used : Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) Message_1_0(org.apache.qpid.server.protocol.v1_0.Message_1_0) BasicContentHeaderProperties(org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties) AMQMessage(org.apache.qpid.server.protocol.v0_8.AMQMessage) Test(org.junit.Test)

Example 23 with Header

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.

the class MessageConverter_Internal_to_v1_0 method convertMetaData.

@Override
protected MessageMetaData_1_0 convertMetaData(final InternalMessage serverMessage, final EncodingRetainingSection<?> bodySection, final SectionEncoder sectionEncoder) {
    Header header = new Header();
    header.setDurable(serverMessage.isPersistent());
    header.setPriority(UnsignedByte.valueOf(serverMessage.getMessageHeader().getPriority()));
    if (serverMessage.getExpiration() != 0l && serverMessage.getArrivalTime() != 0l && serverMessage.getExpiration() >= serverMessage.getArrivalTime()) {
        header.setTtl(UnsignedInteger.valueOf(serverMessage.getExpiration() - serverMessage.getArrivalTime()));
    }
    Properties properties = new Properties();
    if (serverMessage.getMessageHeader().getEncoding() != null) {
        properties.setContentEncoding(Symbol.valueOf(serverMessage.getMessageHeader().getEncoding()));
    }
    properties.setCorrelationId(getCorrelationId(serverMessage));
    properties.setCreationTime(new Date(serverMessage.getMessageHeader().getTimestamp()));
    properties.setMessageId(getMessageId(serverMessage));
    Symbol contentType = getContentTypeSymbol(serverMessage.getMessageBody(), serverMessage.getMessageHeader().getMimeType());
    properties.setContentType(contentType);
    final String userId = serverMessage.getMessageHeader().getUserId();
    if (userId != null) {
        properties.setUserId(new Binary(userId.getBytes(StandardCharsets.UTF_8)));
    }
    properties.setReplyTo(serverMessage.getMessageHeader().getReplyTo());
    properties.setTo(serverMessage.getTo());
    ApplicationProperties applicationProperties = null;
    if (!serverMessage.getMessageHeader().getHeaderNames().isEmpty()) {
        try {
            applicationProperties = new ApplicationProperties(serverMessage.getMessageHeader().getHeaderMap());
        } catch (IllegalArgumentException e) {
            throw new MessageConversionException("Could not convert message from internal to 1.0" + " because conversion of 'application headers' failed.", e);
        }
    }
    final MessageAnnotations messageAnnotation = createMessageAnnotation(serverMessage.getMessageBody(), serverMessage.getMessageHeader().getMimeType(), bodySection);
    return new MessageMetaData_1_0(header.createEncodingRetainingSection(), null, messageAnnotation == null ? null : messageAnnotation.createEncodingRetainingSection(), properties.createEncodingRetainingSection(), applicationProperties == null ? null : applicationProperties.createEncodingRetainingSection(), null, serverMessage.getArrivalTime(), bodySection.getEncodedSize());
}
Also used : MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) Symbol(org.apache.qpid.server.protocol.v1_0.type.Symbol) MessageAnnotations(org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotations) ApplicationProperties(org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) ApplicationProperties(org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties) Properties(org.apache.qpid.server.protocol.v1_0.type.messaging.Properties) Date(java.util.Date)

Example 24 with Header

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.

the class MessageConverter_from_1_0 method getTtl.

public static Long getTtl(final Message_1_0 serverMsg) {
    HeaderSection headerSection = serverMsg.getHeaderSection();
    if (headerSection != null) {
        Header header = headerSection.getValue();
        headerSection.dispose();
        if (header != null) {
            UnsignedInteger ttl = header.getTtl();
            if (ttl != null) {
                return ttl.longValue();
            }
        }
    }
    return null;
}
Also used : Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) UnsignedInteger(org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger) HeaderSection(org.apache.qpid.server.protocol.v1_0.type.messaging.HeaderSection)

Example 25 with Header

use of org.apache.qpid.server.protocol.v1_0.type.messaging.Header in project qpid-broker-j by apache.

the class ConsumerTarget_1_0 method doSend.

@Override
public void doSend(final MessageInstanceConsumer consumer, final MessageInstance entry, boolean batch) {
    ServerMessage serverMessage = entry.getMessage();
    Message_1_0 message;
    final MessageConverter<? super ServerMessage, Message_1_0> converter;
    if (serverMessage instanceof Message_1_0) {
        converter = null;
        message = (Message_1_0) serverMessage;
    } else {
        if (!serverMessage.checkValid()) {
            throw new MessageConversionException(String.format("Cannot convert malformed message '%s'", serverMessage));
        }
        converter = (MessageConverter<? super ServerMessage, Message_1_0>) MessageConverterRegistry.getConverter(serverMessage.getClass(), Message_1_0.class);
        if (converter == null) {
            throw new ServerScopedRuntimeException(String.format("Could not find message converter from '%s' to '%s'." + " This is unexpected since we should not try to send if the converter is not present.", serverMessage.getClass(), Message_1_0.class));
        }
        message = converter.convert(serverMessage, _linkEndpoint.getAddressSpace());
    }
    Transfer transfer = new Transfer();
    try {
        QpidByteBuffer bodyContent = message.getContent();
        HeaderSection headerSection = message.getHeaderSection();
        UnsignedInteger ttl = headerSection == null ? null : headerSection.getValue().getTtl();
        if (entry.getDeliveryCount() != 0 || ttl != null) {
            Header header = new Header();
            if (headerSection != null) {
                final Header oldHeader = headerSection.getValue();
                header.setDurable(oldHeader.getDurable());
                header.setPriority(oldHeader.getPriority());
                if (ttl != null) {
                    long timeSpentOnBroker = System.currentTimeMillis() - message.getArrivalTime();
                    final long adjustedTtl = Math.max(0L, ttl.longValue() - timeSpentOnBroker);
                    header.setTtl(UnsignedInteger.valueOf(adjustedTtl));
                }
                headerSection.dispose();
            }
            if (entry.getDeliveryCount() != 0) {
                header.setDeliveryCount(UnsignedInteger.valueOf(entry.getDeliveryCount()));
            }
            headerSection = header.createEncodingRetainingSection();
        }
        List<QpidByteBuffer> payload = new ArrayList<>();
        if (headerSection != null) {
            payload.add(headerSection.getEncodedForm());
            headerSection.dispose();
        }
        EncodingRetainingSection<?> section;
        if ((section = message.getDeliveryAnnotationsSection()) != null) {
            payload.add(section.getEncodedForm());
            section.dispose();
        }
        if ((section = message.getMessageAnnotationsSection()) != null) {
            payload.add(section.getEncodedForm());
            section.dispose();
        }
        if ((section = message.getPropertiesSection()) != null) {
            payload.add(section.getEncodedForm());
            section.dispose();
        }
        if ((section = message.getApplicationPropertiesSection()) != null) {
            payload.add(section.getEncodedForm());
            section.dispose();
        }
        payload.add(bodyContent);
        if ((section = message.getFooterSection()) != null) {
            payload.add(section.getEncodedForm());
            section.dispose();
        }
        try (QpidByteBuffer combined = QpidByteBuffer.concatenate(payload)) {
            transfer.setPayload(combined);
        }
        payload.forEach(QpidByteBuffer::dispose);
        byte[] data = new byte[8];
        ByteBuffer.wrap(data).putLong(_deliveryTag++);
        final Binary tag = new Binary(data);
        transfer.setDeliveryTag(tag);
        if (_linkEndpoint.isAttached()) {
            boolean sendPreSettled = SenderSettleMode.SETTLED.equals(getEndpoint().getSendingSettlementMode());
            if (sendPreSettled) {
                transfer.setSettled(true);
                if (_acquires && _transactionId == null) {
                    transfer.setState(new Accepted());
                }
            } else {
                final UnsettledAction action;
                if (_acquires) {
                    action = new DispositionAction(tag, entry, consumer);
                    addUnacknowledgedMessage(entry);
                } else {
                    action = new DoNothingAction();
                }
                _linkEndpoint.addUnsettled(tag, action, entry);
            }
            if (_transactionId != null) {
                TransactionalState state = new TransactionalState();
                state.setTxnId(_transactionId);
                transfer.setState(state);
            }
            if (_acquires && _transactionId != null) {
                try {
                    ServerTransaction txn = _linkEndpoint.getTransaction(_transactionId);
                    txn.addPostTransactionAction(new ServerTransaction.Action() {

                        @Override
                        public void postCommit() {
                        }

                        @Override
                        public void onRollback() {
                            entry.release(consumer);
                            _linkEndpoint.updateDisposition(tag, null, true);
                        }
                    });
                    final TransactionLogResource owningResource = entry.getOwningResource();
                    if (owningResource instanceof TransactionMonitor) {
                        ((TransactionMonitor) owningResource).registerTransaction(txn);
                    }
                } catch (UnknownTransactionException e) {
                    entry.release(consumer);
                    getEndpoint().close(new Error(TransactionError.UNKNOWN_ID, e.getMessage()));
                    return;
                }
            }
            getSession().getAMQPConnection().registerMessageDelivered(message.getSize());
            getEndpoint().transfer(transfer, false);
            if (sendPreSettled && _acquires && _transactionId == null) {
                handleAcquiredEntrySentPareSettledNonTransactional(entry, consumer);
            }
        } else {
            entry.release(consumer);
        }
    } finally {
        transfer.dispose();
        if (converter != null) {
            converter.dispose(message);
        }
    }
}
Also used : ServerMessage(org.apache.qpid.server.message.ServerMessage) ArrayList(java.util.ArrayList) HeaderSection(org.apache.qpid.server.protocol.v1_0.type.messaging.HeaderSection) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException) ServerTransaction(org.apache.qpid.server.txn.ServerTransaction) TransactionMonitor(org.apache.qpid.server.txn.TransactionMonitor) MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) TransactionError(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError) Accepted(org.apache.qpid.server.protocol.v1_0.type.messaging.Accepted) TransactionalState(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionalState) Header(org.apache.qpid.server.protocol.v1_0.type.messaging.Header) Transfer(org.apache.qpid.server.protocol.v1_0.type.transport.Transfer) QpidByteBuffer(org.apache.qpid.server.bytebuffer.QpidByteBuffer) Binary(org.apache.qpid.server.protocol.v1_0.type.Binary) UnsignedInteger(org.apache.qpid.server.protocol.v1_0.type.UnsignedInteger) TransactionLogResource(org.apache.qpid.server.store.TransactionLogResource)

Aggregations

Header (org.apache.qpid.server.protocol.v1_0.type.messaging.Header)45 Test (org.junit.Test)44 Message_1_0 (org.apache.qpid.server.protocol.v1_0.Message_1_0)32 DeliveryProperties (org.apache.qpid.server.protocol.v0_10.transport.DeliveryProperties)19 MessageTransferMessage (org.apache.qpid.server.protocol.v0_10.MessageTransferMessage)18 Properties (org.apache.qpid.server.protocol.v1_0.type.messaging.Properties)17 QpidByteBuffer (org.apache.qpid.server.bytebuffer.QpidByteBuffer)16 ApplicationProperties (org.apache.qpid.server.protocol.v1_0.type.messaging.ApplicationProperties)14 BasicContentHeaderProperties (org.apache.qpid.server.protocol.v0_8.transport.BasicContentHeaderProperties)13 MessageAnnotations (org.apache.qpid.server.protocol.v1_0.type.messaging.MessageAnnotations)13 DeliveryAnnotations (org.apache.qpid.server.protocol.v1_0.type.messaging.DeliveryAnnotations)12 MessageProperties (org.apache.qpid.server.protocol.v0_10.transport.MessageProperties)11 AMQMessage (org.apache.qpid.server.protocol.v0_8.AMQMessage)11 Date (java.util.Date)9 InternalMessage (org.apache.qpid.server.message.internal.InternalMessage)8 MessageMetaData_1_0 (org.apache.qpid.server.protocol.v1_0.MessageMetaData_1_0)8 Binary (org.apache.qpid.server.protocol.v1_0.type.Binary)8 Footer (org.apache.qpid.server.protocol.v1_0.type.messaging.Footer)7 ArrayList (java.util.ArrayList)6 Symbol (org.apache.qpid.server.protocol.v1_0.type.Symbol)6