Search in sources :

Example 1 with QueueMessage

use of org.motechproject.admin.domain.QueueMessage in project motech by motech.

the class BrokerStatisticsControllerTest method shouldReturnMessageInformationGivenQueueName.

@Test
public void shouldReturnMessageInformationGivenQueueName() throws Exception {
    given(mBeanService.getQueueMessages("foo")).willReturn(Arrays.asList(new QueueMessage("123", false, new DateTime())));
    mockMvc.perform(MockMvcRequestBuilders.get("/queues/browse?queueName=foo")).andExpect(status().isOk()).andExpect(content().string(new StringContains("\"messageId\":\"123"))).andExpect(content().string(new StringContains("\"redelivered\":false")));
}
Also used : QueueMessage(org.motechproject.admin.domain.QueueMessage) DateTime(org.joda.time.DateTime) StringContains(org.hamcrest.text.StringContains) Test(org.junit.Test)

Example 2 with QueueMessage

use of org.motechproject.admin.domain.QueueMessage in project motech by motech.

the class MBeanService method getQueueMessages.

/**
 * Retrieves a list of messages for the given JMS queue.
 *
 * @param queueName The name of the queue for which messages should be retrieved.
 * @return {@link List} of messages for the given queue.
 */
@PreAuthorize(SecurityConstants.MANAGE_ACTIVEMQ)
public List<QueueMessage> getQueueMessages(String queueName) {
    try {
        ArrayList<QueueMessage> queueMessages = new ArrayList<>();
        QueueViewMBean queueViewMBean = mBeanServer.getQueueViewMBean(queueName);
        for (CompositeData compositeData : queueViewMBean.browse()) {
            if (compositeData != null) {
                String messageId = (String) compositeData.get(JMS_MESSAGE_ID);
                Boolean redelivered = (Boolean) compositeData.get(JMS_REDELIVERED);
                Date timestamp = (Date) compositeData.get(JMS_TIMESTAMP);
                queueMessages.add(new QueueMessage(messageId, redelivered, DateUtil.newDateTime(timestamp)));
            }
        }
        return queueMessages;
    } catch (OpenDataException openDataException) {
        throw new MotechException(String.format("Could not Browse MBean for queue %s", queueName), openDataException);
    } catch (IOException ioException) {
        throw new MotechException(String.format("Could not access MBean for queue %s", queueName), ioException);
    }
}
Also used : MotechException(org.motechproject.commons.api.MotechException) QueueMessage(org.motechproject.admin.domain.QueueMessage) OpenDataException(javax.management.openmbean.OpenDataException) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) QueueViewMBean(org.apache.activemq.broker.jmx.QueueViewMBean) IOException(java.io.IOException) Date(java.util.Date) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

QueueMessage (org.motechproject.admin.domain.QueueMessage)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 CompositeData (javax.management.openmbean.CompositeData)1 OpenDataException (javax.management.openmbean.OpenDataException)1 QueueViewMBean (org.apache.activemq.broker.jmx.QueueViewMBean)1 StringContains (org.hamcrest.text.StringContains)1 DateTime (org.joda.time.DateTime)1 Test (org.junit.Test)1 MotechException (org.motechproject.commons.api.MotechException)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1