Search in sources :

Example 16 with StoredMessage

use of org.apache.qpid.server.store.StoredMessage in project qpid-broker-j by apache.

the class MessageConverter_0_8_to_0_10 method convertToStoredMessage.

private StoredMessage<MessageMetaData_0_10> convertToStoredMessage(final AMQMessage message_0_8) {
    final MessageMetaData_0_10 messageMetaData_0_10 = convertMetaData(message_0_8);
    final int metadataSize = messageMetaData_0_10.getStorableSize();
    return new StoredMessage<MessageMetaData_0_10>() {

        @Override
        public MessageMetaData_0_10 getMetaData() {
            return messageMetaData_0_10;
        }

        @Override
        public long getMessageNumber() {
            return message_0_8.getMessageNumber();
        }

        @Override
        public QpidByteBuffer getContent(final int offset, final int length) {
            return message_0_8.getContent(offset, length);
        }

        @Override
        public int getContentSize() {
            return messageMetaData_0_10.getContentSize();
        }

        @Override
        public int getMetadataSize() {
            return metadataSize;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isInMemory() {
            return true;
        }

        @Override
        public boolean flowToDisk() {
            return false;
        }

        @Override
        public void reallocate() {
        }
    };
}
Also used : StoredMessage(org.apache.qpid.server.store.StoredMessage) MessageMetaData_0_10(org.apache.qpid.server.protocol.v0_10.MessageMetaData_0_10)

Example 17 with StoredMessage

use of org.apache.qpid.server.store.StoredMessage in project qpid-broker-j by apache.

the class MessageConverter_Internal_to_v0_8 method convertToStoredMessage.

private StoredMessage<MessageMetaData> convertToStoredMessage(final InternalMessage serverMsg, final NamedAddressSpace addressSpace) {
    Object messageBody = serverMsg.getMessageBody();
    ObjectToMimeContentConverter converter = MimeContentConverterRegistry.getBestFitObjectToMimeContentConverter(messageBody);
    final byte[] messageContent;
    try {
        messageContent = converter == null ? new byte[] {} : converter.toMimeContent(messageBody);
    } catch (IllegalArgumentException e) {
        throw new MessageConversionException("Could not convert message from Internal to 0-8 because" + " conversion of message content failed.", e);
    }
    String mimeType = converter == null ? null : converter.getMimeType();
    mimeType = improveMimeType(serverMsg, mimeType);
    final MessageMetaData messageMetaData_0_8 = convertMetaData(serverMsg, addressSpace, mimeType, messageContent.length);
    final int metadataSize = messageMetaData_0_8.getStorableSize();
    return new StoredMessage<MessageMetaData>() {

        @Override
        public MessageMetaData getMetaData() {
            return messageMetaData_0_8;
        }

        @Override
        public long getMessageNumber() {
            return serverMsg.getMessageNumber();
        }

        @Override
        public QpidByteBuffer getContent(final int offset, final int length) {
            return QpidByteBuffer.wrap(messageContent, offset, length);
        }

        @Override
        public int getContentSize() {
            return messageContent.length;
        }

        @Override
        public int getMetadataSize() {
            return metadataSize;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean isInMemory() {
            return true;
        }

        @Override
        public boolean flowToDisk() {
            return false;
        }

        @Override
        public void reallocate() {
        }
    };
}
Also used : ObjectToMimeContentConverter(org.apache.qpid.server.message.mimecontentconverter.ObjectToMimeContentConverter) MessageConversionException(org.apache.qpid.server.protocol.converter.MessageConversionException) StoredMessage(org.apache.qpid.server.store.StoredMessage)

Example 18 with StoredMessage

use of org.apache.qpid.server.store.StoredMessage in project qpid-broker-j by apache.

the class ServerSessionDelegate method messageTransfer.

@Override
public void messageTransfer(ServerSession ssn, final MessageTransfer xfr) {
    try {
        if (ssn.blockingTimeoutExceeded()) {
            getEventLogger(ssn).message(ChannelMessages.FLOW_CONTROL_IGNORED());
            ssn.close(ErrorCodes.MESSAGE_TOO_LARGE, "Session flow control was requested, but not enforced by sender");
        } else if (xfr.getBodySize() > ssn.getConnection().getMaxMessageSize()) {
            exception(ssn, xfr, ExecutionErrorCode.RESOURCE_LIMIT_EXCEEDED, "Message size of " + xfr.getBodySize() + " greater than allowed maximum of " + ssn.getConnection().getMaxMessageSize());
        } else {
            final MessageDestination destination = getDestinationForMessage(ssn, xfr);
            final DeliveryProperties delvProps = xfr.getHeader() == null ? null : xfr.getHeader().getDeliveryProperties();
            if (delvProps != null && delvProps.hasTtl() && !delvProps.hasExpiration()) {
                delvProps.setExpiration(System.currentTimeMillis() + delvProps.getTtl());
            }
            final MessageMetaData_0_10 messageMetaData = new MessageMetaData_0_10(xfr);
            final NamedAddressSpace virtualHost = getAddressSpace(ssn);
            try {
                ssn.getAMQPConnection().checkAuthorizedMessagePrincipal(getMessageUserId(xfr));
                ssn.authorisePublish(destination, messageMetaData.getRoutingKey(), messageMetaData.isImmediate(), ssn.getAMQPConnection().getLastReadTime());
            } catch (AccessControlException e) {
                ExecutionErrorCode errorCode = ExecutionErrorCode.UNAUTHORIZED_ACCESS;
                exception(ssn, xfr, errorCode, e.getMessage());
                return;
            }
            final MessageStore store = virtualHost.getMessageStore();
            final StoredMessage<MessageMetaData_0_10> storeMessage = createStoreMessage(xfr, messageMetaData, store);
            final MessageTransferMessage message = new MessageTransferMessage(storeMessage, ssn.getReference());
            MessageReference<MessageTransferMessage> reference = message.newReference();
            try {
                final InstanceProperties instanceProperties = new InstanceProperties() {

                    @Override
                    public Object getProperty(final Property prop) {
                        switch(prop) {
                            case EXPIRATION:
                                return message.getExpiration();
                            case IMMEDIATE:
                                return message.isImmediate();
                            case MANDATORY:
                                return (delvProps == null || !delvProps.getDiscardUnroutable()) && xfr.getAcceptMode() == MessageAcceptMode.EXPLICIT;
                            case PERSISTENT:
                                return message.isPersistent();
                            case REDELIVERED:
                                return delvProps.getRedelivered();
                        }
                        return null;
                    }
                };
                RoutingResult<MessageTransferMessage> routingResult = ssn.enqueue(message, instanceProperties, destination);
                boolean explictlyRejected = routingResult.containsReject(RejectType.LIMIT_EXCEEDED);
                if (!routingResult.hasRoutes() || explictlyRejected) {
                    boolean closeWhenNoRoute = ssn.getAMQPConnection().getPort().getCloseWhenNoRoute();
                    boolean discardUnroutable = delvProps != null && delvProps.getDiscardUnroutable();
                    if (!discardUnroutable && xfr.getAcceptMode() == MessageAcceptMode.EXPLICIT) {
                        RangeSet rejects = RangeSetFactory.createRangeSet();
                        rejects.add(xfr.getId());
                        MessageReject reject = new MessageReject(rejects, MessageRejectCode.UNROUTABLE, "Unroutable");
                        ssn.invoke(reject);
                    } else if (!discardUnroutable && closeWhenNoRoute && explictlyRejected) {
                        ExecutionErrorCode code = ExecutionErrorCode.RESOURCE_LIMIT_EXCEEDED;
                        String errorMessage = String.format("No route for message with destination '%s' and routing key '%s' : %s", xfr.getDestination(), message.getInitialRoutingAddress(), routingResult.getRejectReason());
                        ExecutionException ex = new ExecutionException();
                        ex.setErrorCode(code);
                        ex.setDescription(errorMessage);
                        ssn.invoke(ex);
                        ssn.close(ErrorCodes.RESOURCE_ERROR, errorMessage);
                        return;
                    } else {
                        getEventLogger(ssn).message(ExchangeMessages.DISCARDMSG(destination.getName(), messageMetaData.getRoutingKey()));
                    }
                }
                // TODO: we currently do not send MessageAccept when AcceptMode is EXPLICIT
                if (ssn.isTransactional()) {
                    ssn.processed(xfr);
                } else {
                    ssn.recordFuture(Futures.immediateFuture(null), new CommandProcessedAction(ssn, xfr));
                }
            } catch (VirtualHostUnavailableException e) {
                getServerConnection(ssn).sendConnectionCloseAsync(ConnectionCloseCode.CONNECTION_FORCED, e.getMessage());
            } finally {
                reference.release();
            }
        }
    } finally {
        xfr.dispose();
    }
}
Also used : MessageStore(org.apache.qpid.server.store.MessageStore) MessageDestination(org.apache.qpid.server.message.MessageDestination) InstanceProperties(org.apache.qpid.server.message.InstanceProperties) NamedAddressSpace(org.apache.qpid.server.model.NamedAddressSpace) AccessControlException(java.security.AccessControlException) MessageReference(org.apache.qpid.server.message.MessageReference) RoutingResult(org.apache.qpid.server.message.RoutingResult) VirtualHostUnavailableException(org.apache.qpid.server.virtualhost.VirtualHostUnavailableException) StoredMessage(org.apache.qpid.server.store.StoredMessage) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject)

Example 19 with StoredMessage

use of org.apache.qpid.server.store.StoredMessage in project qpid-broker-j by apache.

the class AbstractQueue method enqueue.

// ------ Enqueue / Dequeue
@Override
public final void enqueue(ServerMessage message, Action<? super MessageInstance> action, MessageEnqueueRecord enqueueRecord) {
    if (_recovering.get() != RECOVERED) {
        _enqueuingWhileRecovering.incrementAndGet();
        boolean addedToRecoveryQueue;
        try {
            if (addedToRecoveryQueue = (_recovering.get() == RECOVERING)) {
                _postRecoveryQueue.add(new EnqueueRequest(message, action, enqueueRecord));
            }
        } finally {
            _enqueuingWhileRecovering.decrementAndGet();
        }
        if (!addedToRecoveryQueue) {
            while (_recovering.get() != RECOVERED) {
                Thread.yield();
            }
            doEnqueue(message, action, enqueueRecord);
        }
    } else {
        doEnqueue(message, action, enqueueRecord);
    }
    final StoredMessage storedMessage = message.getStoredMessage();
    if ((_virtualHost.isOverTargetSize() || QpidByteBuffer.getAllocatedDirectMemorySize() > _flowToDiskThreshold) && storedMessage.isInMemory()) {
        storedMessage.flowToDisk();
    }
}
Also used : StoredMessage(org.apache.qpid.server.store.StoredMessage)

Example 20 with StoredMessage

use of org.apache.qpid.server.store.StoredMessage in project qpid-broker-j by apache.

the class InternalMessage method createReadOnlyHandle.

private static StoredMessage<InternalMessageMetaData> createReadOnlyHandle(final long messageNumber, final boolean persistent, final InternalMessageHeader header, final Object messageBody) {
    try (ByteArrayOutputStream bytesOut = new ByteArrayOutputStream()) {
        try (ObjectOutputStream os = new ObjectOutputStream(bytesOut)) {
            os.writeObject(messageBody);
            final byte[] bytes = bytesOut.toByteArray();
            final InternalMessageMetaData metaData = InternalMessageMetaData.create(persistent, header, bytes.length);
            final int metadataSize = metaData.getStorableSize();
            return new StoredMessage<InternalMessageMetaData>() {

                @Override
                public InternalMessageMetaData getMetaData() {
                    return metaData;
                }

                @Override
                public long getMessageNumber() {
                    return messageNumber;
                }

                @Override
                public QpidByteBuffer getContent(final int offset, final int length) {
                    return QpidByteBuffer.wrap(bytes, offset, length);
                }

                @Override
                public int getContentSize() {
                    return bytes.length;
                }

                @Override
                public int getMetadataSize() {
                    return metadataSize;
                }

                @Override
                public void remove() {
                    throw new UnsupportedOperationException();
                }

                @Override
                public boolean isInMemory() {
                    return true;
                }

                @Override
                public boolean flowToDisk() {
                    return false;
                }

                @Override
                public void reallocate() {
                }
            };
        }
    } catch (IOException e) {
        throw new ConnectionScopedRuntimeException("Unexpected IO Exception on operation in memory", e);
    }
}
Also used : StoredMessage(org.apache.qpid.server.store.StoredMessage) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

StoredMessage (org.apache.qpid.server.store.StoredMessage)23 MessageReference (org.apache.qpid.server.message.MessageReference)5 ServerMessage (org.apache.qpid.server.message.ServerMessage)4 Transaction (org.apache.qpid.server.store.Transaction)4 UUID (java.util.UUID)3 LogMessage (org.apache.qpid.server.logging.LogMessage)3 HashMap (java.util.HashMap)2 ObjectToMimeContentConverter (org.apache.qpid.server.message.mimecontentconverter.ObjectToMimeContentConverter)2 Queue (org.apache.qpid.server.model.Queue)2 MessageMetaData_0_10 (org.apache.qpid.server.protocol.v0_10.MessageMetaData_0_10)2 MessageMetaData (org.apache.qpid.server.protocol.v0_8.MessageMetaData)2 ContentHeaderBody (org.apache.qpid.server.protocol.v0_8.transport.ContentHeaderBody)2 MessagePublishInfo (org.apache.qpid.server.protocol.v0_8.transport.MessagePublishInfo)2 ConvertedContentAndMimeType (org.apache.qpid.server.protocol.v1_0.MessageConverter_from_1_0.ConvertedContentAndMimeType)2 MessageConverter_from_1_0.getAmqp0xConvertedContentAndMimeType (org.apache.qpid.server.protocol.v1_0.MessageConverter_from_1_0.getAmqp0xConvertedContentAndMimeType)2 MessageEnqueueRecord (org.apache.qpid.server.store.MessageEnqueueRecord)2 MessageStore (org.apache.qpid.server.store.MessageStore)2 StorableMessageMetaData (org.apache.qpid.server.store.StorableMessageMetaData)2 TransactionLogResource (org.apache.qpid.server.store.TransactionLogResource)2 DatabaseEntry (com.sleepycat.je.DatabaseEntry)1