Search in sources :

Example 41 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueControlImpl method convertMessagesToMaps.

/**
 * @param refs
 * @return
 */
private Map<String, Object>[] convertMessagesToMaps(List<MessageReference> refs) throws ActiveMQException {
    Map<String, Object>[] messages = new Map[refs.size()];
    int i = 0;
    for (MessageReference ref : refs) {
        Message message = ref.getMessage();
        messages[i++] = message.toMap();
    }
    return messages;
}
Also used : CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) Message(org.apache.activemq.artemis.api.core.Message) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) HashMap(java.util.HashMap) Map(java.util.Map)

Example 42 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueControlImpl method countMessages.

@Override
public long countMessages(final String filterStr) throws Exception {
    checkStarted();
    clearIO();
    try {
        Filter filter = FilterImpl.createFilter(filterStr);
        if (filter == null) {
            return getMessageCount();
        } else {
            try (LinkedListIterator<MessageReference> iterator = queue.browserIterator()) {
                int count = 0;
                try {
                    while (iterator.hasNext()) {
                        MessageReference ref = iterator.next();
                        if (filter.match(ref.getMessage())) {
                            count++;
                        }
                    }
                } catch (NoSuchElementException ignored) {
                // this could happen through paging browsing
                }
                return count;
            }
        }
    } finally {
        blockOnIO();
    }
}
Also used : Filter(org.apache.activemq.artemis.core.filter.Filter) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) NoSuchElementException(java.util.NoSuchElementException)

Example 43 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueControlImpl method listMessages.

@Override
public Map<String, Object>[] listMessages(final String filterStr) throws Exception {
    checkStarted();
    clearIO();
    try {
        Filter filter = FilterImpl.createFilter(filterStr);
        List<Map<String, Object>> messages = new ArrayList<>();
        queue.flushExecutor();
        try (LinkedListIterator<MessageReference> iterator = queue.browserIterator()) {
            try {
                while (iterator.hasNext()) {
                    MessageReference ref = iterator.next();
                    if (filter == null || filter.match(ref.getMessage())) {
                        Message message = ref.getMessage();
                        messages.add(message.toMap());
                    }
                }
            } catch (NoSuchElementException ignored) {
            // this could happen through paging browsing
            }
            return messages.toArray(new Map[messages.size()]);
        }
    } catch (ActiveMQException e) {
        throw new IllegalStateException(e.getMessage());
    } finally {
        blockOnIO();
    }
}
Also used : CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) Message(org.apache.activemq.artemis.api.core.Message) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Filter(org.apache.activemq.artemis.core.filter.Filter) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) HashMap(java.util.HashMap) Map(java.util.Map) NoSuchElementException(java.util.NoSuchElementException)

Example 44 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueControlImpl method browse.

@Override
public CompositeData[] browse(int page, int pageSize) throws Exception {
    String filter = null;
    checkStarted();
    clearIO();
    try {
        long index = 0;
        long start = (page - 1) * pageSize;
        long end = Math.min(page * pageSize, queue.getMessageCount());
        ArrayList<CompositeData> c = new ArrayList<>();
        Filter thefilter = FilterImpl.createFilter(filter);
        queue.flushExecutor();
        try (LinkedListIterator<MessageReference> iterator = queue.browserIterator()) {
            try {
                while (iterator.hasNext() && index < end) {
                    MessageReference ref = iterator.next();
                    if (thefilter == null || thefilter.match(ref.getMessage())) {
                        if (index >= start) {
                            c.add(OpenTypeSupport.convert(ref));
                        }
                    }
                    index++;
                }
            } catch (NoSuchElementException ignored) {
            // this could happen through paging browsing
            }
            CompositeData[] rc = new CompositeData[c.size()];
            c.toArray(rc);
            return rc;
        }
    } catch (ActiveMQException e) {
        throw new IllegalStateException(e.getMessage());
    } finally {
        blockOnIO();
    }
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Filter(org.apache.activemq.artemis.core.filter.Filter) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) NoSuchElementException(java.util.NoSuchElementException)

Example 45 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueControlImpl method getFirstMessage.

protected Map<String, Object>[] getFirstMessage() throws Exception {
    checkStarted();
    clearIO();
    try {
        List<Map<String, Object>> messages = new ArrayList<>();
        queue.flushExecutor();
        try (LinkedListIterator<MessageReference> iterator = queue.browserIterator()) {
            // returns just the first, as it's the first only
            if (iterator.hasNext()) {
                MessageReference ref = iterator.next();
                Message message = ref.getMessage();
                messages.add(message.toMap());
            }
            return messages.toArray(new Map[1]);
        }
    } finally {
        blockOnIO();
    }
}
Also used : CoreMessage(org.apache.activemq.artemis.core.message.impl.CoreMessage) Message(org.apache.activemq.artemis.api.core.Message) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MessageReference (org.apache.activemq.artemis.core.server.MessageReference)82 ArrayList (java.util.ArrayList)29 QueueImpl (org.apache.activemq.artemis.core.server.impl.QueueImpl)26 Test (org.junit.Test)26 FakeConsumer (org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer)18 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)14 Message (org.apache.activemq.artemis.api.core.Message)12 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)11 Queue (org.apache.activemq.artemis.core.server.Queue)10 HashMap (java.util.HashMap)9 NoSuchElementException (java.util.NoSuchElementException)9 Map (java.util.Map)8 Filter (org.apache.activemq.artemis.core.filter.Filter)7 LinkedList (java.util.LinkedList)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)5 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)4 BindingsTransactionImpl (org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)4 HashSet (java.util.HashSet)3