protected synchronized void flushToDisk() { if (!memoryList.isEmpty() && store != null) { long start = 0; if (LOG.isTraceEnabled()) { start = System.currentTimeMillis(); LOG.trace( "{}, flushToDisk() mem list size: {} {}", new Object[] { name, memoryList.size(), (systemUsage != null ? systemUsage.getMemoryUsage() : "") }); } for (Iterator<MessageReference> iterator = memoryList.iterator(); iterator.hasNext(); ) { MessageReference node = iterator.next(); node.decrementReferenceCount(); ByteSequence bs; try { bs = getByteSequence(node.getMessage()); getDiskList().addLast(node.getMessageId().toString(), bs); } catch (IOException e) { LOG.error("Failed to write to disk list", e); throw new RuntimeException(e); } } memoryList.clear(); setCacheEnabled(false); LOG.trace( "{}, flushToDisk() done - {} ms {}", new Object[] { name, (System.currentTimeMillis() - start), (systemUsage != null ? systemUsage.getMemoryUsage() : "") }); } }
private void discardExpiredMessage(MessageReference reference) { LOG.debug("Discarding expired message {}", reference); if (broker.isExpired(reference)) { ConnectionContext context = new ConnectionContext(new NonCachedMessageEvaluationContext()); context.setBroker(broker); ((Destination) reference.getRegionDestination()) .messageExpired(context, null, new IndirectMessageReference(reference.getMessage())); } }
@Override public synchronized void destroy() throws Exception { stop(); for (Iterator<MessageReference> i = memoryList.iterator(); i.hasNext(); ) { MessageReference node = i.next(); node.decrementReferenceCount(); } memoryList.clear(); destroyDiskList(); }
protected synchronized void expireOldMessages() { if (!memoryList.isEmpty()) { for (Iterator<MessageReference> iterator = memoryList.iterator(); iterator.hasNext(); ) { MessageReference node = iterator.next(); if (node.isExpired()) { node.decrementReferenceCount(); discardExpiredMessage(node); iterator.remove(); } } } }
/** @return the next pending message */ @Override public synchronized MessageReference next() { MessageReference reference = iter.next(); last = reference; if (!isDiskListEmpty()) { // got from disk reference.getMessage().setRegionDestination(regionDestination); reference.getMessage().setMemoryUsage(this.getSystemUsage().getMemoryUsage()); } reference.incrementReferenceCount(); return reference; }
/** * @param node * @see * org.apache.activemq.broker.region.cursors.AbstractPendingMessageCursor#remove(org.apache.activemq.broker.region.MessageReference) */ @Override public synchronized void remove(MessageReference node) { if (memoryList.remove(node) != null) { node.decrementReferenceCount(); } if (!isDiskListEmpty()) { try { getDiskList().remove(node.getMessageId().getPlistLocator()); } catch (IOException e) { throw new RuntimeException(e); } } }
@Override public synchronized void addMessageFirst(MessageReference node) throws Exception { batchList.addMessageFirst(node); size++; node.incrementReferenceCount(); // this.messageSize.addSize(node.getMessage().getSize()); }
/** @return true if there are no pending messages */ @Override public synchronized boolean isEmpty() { if (memoryList.isEmpty() && isDiskListEmpty()) { return true; } for (Iterator<MessageReference> iterator = memoryList.iterator(); iterator.hasNext(); ) { MessageReference node = iterator.next(); if (node == QueueMessageReference.NULL_MESSAGE) { continue; } if (!node.isDropped()) { return false; } // We can remove dropped references. iterator.remove(); } return isDiskListEmpty(); }
public Message[] browse(ActiveMQDestination destination) throws Exception { final List<Message> result = new ArrayList<Message>(); if (retainedMessage != null) { DestinationFilter filter = DestinationFilter.parseFilter(destination); if (filter.matches(retainedMessage.getMessage().getDestination())) { result.add(retainedMessage.getMessage()); } } Message[] messages = result.toArray(new Message[result.size()]); if (wrapped != null) { final Message[] wrappedMessages = wrapped.browse(destination); if (wrappedMessages != null && wrappedMessages.length > 0) { final int origLen = messages.length; messages = Arrays.copyOf(messages, origLen + wrappedMessages.length); System.arraycopy(wrappedMessages, 0, messages, origLen, wrappedMessages.length); } } return messages; }
@Override public void messageDelivered(ConnectionContext context, MessageReference messageReference) { if (isLogAll() || isLogConsumerEvents() || isLogInternalEvents()) { String msg = "Unable to display message."; msg = messageReference.getMessage().toString(); LOG.info("Message delivered: {}", msg); } super.messageDelivered(context, messageReference); }
public boolean add(ConnectionContext context, MessageReference node) throws Exception { final Message message = node.getMessage(); final Object retainValue = message.getProperty(RETAIN_PROPERTY); // retain property set to true final boolean retain = retainValue != null && Boolean.parseBoolean(retainValue.toString()); if (retain) { if (message.getContent().getLength() > 0) { // non zero length message content retainedMessage = message.copy(); retainedMessage.getMessage().removeProperty(RETAIN_PROPERTY); retainedMessage.getMessage().setProperty(RETAINED_PROPERTY, true); } else { // clear retained message retainedMessage = null; } // TODO should we remove the publisher's retain property?? node.getMessage().removeProperty(RETAIN_PROPERTY); } return wrapped == null ? true : wrapped.add(context, node); }
@Override public void messageExpired( ConnectionContext context, MessageReference message, Subscription subscription) { if (isLogAll() || isLogInternalEvents()) { String msg = "Unable to display message."; msg = message.getMessage().toString(); LOG.info("Message has expired: {}", msg); } super.messageExpired(context, message, subscription); }
@Override public synchronized LinkedList<MessageReference> pageInList(int maxItems) { LinkedList<MessageReference> result = new LinkedList<MessageReference>(); int count = 0; for (Iterator<MessageReference> i = memoryList.iterator(); i.hasNext() && count < maxItems; ) { MessageReference ref = i.next(); ref.incrementReferenceCount(); result.add(ref); count++; } if (count < maxItems && !isDiskListEmpty()) { for (Iterator<MessageReference> i = new DiskIterator(); i.hasNext() && count < maxItems; ) { Message message = (Message) i.next(); message.setRegionDestination(regionDestination); message.setMemoryUsage(this.getSystemUsage().getMemoryUsage()); message.incrementReferenceCount(); result.add(message); count++; } } return result; }
@Override public boolean sendToDeadLetterQueue( ConnectionContext context, MessageReference messageReference, Subscription subscription, Throwable poisonCause) { if (isLogAll() || isLogInternalEvents()) { String msg = "Unable to display message."; msg = messageReference.getMessage().toString(); LOG.info("Sending to DLQ: {}", msg); } return super.sendToDeadLetterQueue(context, messageReference, subscription, poisonCause); }
/** * add message to await dispatch * * @param node */ @Override public synchronized void addMessageFirst(MessageReference node) { if (!node.isExpired()) { try { regionDestination = (Destination) node.getMessage().getRegionDestination(); if (isDiskListEmpty()) { if (hasSpace()) { memoryList.addMessageFirst(node); node.incrementReferenceCount(); setCacheEnabled(true); return; } } if (!hasSpace()) { if (isDiskListEmpty()) { expireOldMessages(); if (hasSpace()) { memoryList.addMessageFirst(node); node.incrementReferenceCount(); return; } else { flushToDisk(); } } } systemUsage.getTempUsage().waitForSpace(); node.decrementReferenceCount(); ByteSequence bs = getByteSequence(node.getMessage()); Object locator = getDiskList().addFirst(node.getMessageId().toString(), bs); node.getMessageId().setPlistLocator(locator); } catch (Exception e) { LOG.error( "Caught an Exception adding a message: {} first to FilePendingMessageCursor ", node, e); throw new RuntimeException(e); } } else { discardExpiredMessage(node); } }
@Override public synchronized boolean tryAddMessageLast(MessageReference node, long maxWaitTime) throws Exception { if (!node.isExpired()) { try { regionDestination = (Destination) node.getMessage().getRegionDestination(); if (isDiskListEmpty()) { if (hasSpace() || this.store == null) { memoryList.addMessageLast(node); node.incrementReferenceCount(); setCacheEnabled(true); return true; } } if (!hasSpace()) { if (isDiskListEmpty()) { expireOldMessages(); if (hasSpace()) { memoryList.addMessageLast(node); node.incrementReferenceCount(); return true; } else { flushToDisk(); } } } if (systemUsage.getTempUsage().waitForSpace(maxWaitTime)) { ByteSequence bs = getByteSequence(node.getMessage()); getDiskList().addLast(node.getMessageId().toString(), bs); return true; } return false; } catch (Exception e) { LOG.error( "Caught an Exception adding a message: {} first to FilePendingMessageCursor ", node, e); throw new RuntimeException(e); } } else { discardExpiredMessage(node); } // message expired return true; }
public boolean isAllowedToConsume(MessageReference n) throws IOException { if (messageAuthorizationPolicy != null) { return messageAuthorizationPolicy.isAllowedToConsume(this, n.getMessage()); } return true; }