Search in sources :

Example 46 with TemporaryQueue

use of javax.jms.TemporaryQueue in project qpid-broker-j by apache.

the class AmqpManagementFacade method receiveManagementResponse.

private Map<String, Object> receiveManagementResponse(final MessageConsumer consumer, final Destination replyToDestination, final int responseStatus) throws JMSException {
    Message response = consumer.receive(5000);
    try {
        if (response != null) {
            int statusCode = response.getIntProperty("statusCode");
            if (statusCode == responseStatus) {
                if (response instanceof MapMessage) {
                    MapMessage bodyMap = (MapMessage) response;
                    Map<String, Object> result = new HashMap<>();
                    Enumeration keys = bodyMap.getMapNames();
                    while (keys.hasMoreElements()) {
                        final String key = String.valueOf(keys.nextElement());
                        Object value = bodyMap.getObject(key);
                        result.put(key, value);
                    }
                    return result;
                } else if (response instanceof ObjectMessage) {
                    Object body = ((ObjectMessage) response).getObject();
                    if (body instanceof Map) {
                        @SuppressWarnings("unchecked") Map<String, Object> bodyMap = (Map<String, Object>) body;
                        return new HashMap<>(bodyMap);
                    }
                } else {
                    return Collections.emptyMap();
                }
            } else {
                throw new OperationUnsuccessfulException(response.getStringProperty("statusDescription"), statusCode);
            }
        }
        throw new IllegalArgumentException("Cannot parse the results from a management response");
    } finally {
        consumer.close();
        if (_protocol == Protocol.AMQP_1_0) {
            ((TemporaryQueue) replyToDestination).delete();
        }
    }
}
Also used : Enumeration(java.util.Enumeration) StreamMessage(javax.jms.StreamMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) HashMap(java.util.HashMap) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) TemporaryQueue(javax.jms.TemporaryQueue) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 47 with TemporaryQueue

use of javax.jms.TemporaryQueue in project qpid-broker-j by apache.

the class AmqpManagementFacade method performOperationUsingAmqpManagement.

public Object performOperationUsingAmqpManagement(final String name, final String operation, final Session session, final String type, Map<String, Object> arguments) throws JMSException {
    Destination replyToDestination;
    Destination replyConsumerDestination;
    if (_protocol == Protocol.AMQP_1_0) {
        replyToDestination = session.createTemporaryQueue();
        replyConsumerDestination = replyToDestination;
    } else {
        replyToDestination = session.createQueue(AMQP_0_X_REPLY_TO_DESTINATION);
        replyConsumerDestination = session.createQueue(AMQP_0_X_CONSUMER_REPLY_DESTINATION);
    }
    MessageConsumer consumer = session.createConsumer(replyConsumerDestination);
    MessageProducer producer = session.createProducer(session.createQueue(_managementAddress));
    MapMessage opMessage = session.createMapMessage();
    opMessage.setStringProperty("type", type);
    opMessage.setStringProperty("operation", operation);
    opMessage.setStringProperty("index", "object-path");
    opMessage.setJMSReplyTo(replyToDestination);
    opMessage.setStringProperty("key", name);
    for (Map.Entry<String, Object> argument : arguments.entrySet()) {
        Object value = argument.getValue();
        if (value.getClass().isPrimitive() || value instanceof String) {
            opMessage.setObjectProperty(argument.getKey(), value);
        } else {
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonifiedValue;
            try {
                jsonifiedValue = objectMapper.writeValueAsString(value);
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException(String.format("Cannot convert the argument '%s' to JSON to meet JMS type restrictions", argument.getKey()));
            }
            opMessage.setObjectProperty(argument.getKey(), jsonifiedValue);
        }
    }
    producer.send(opMessage);
    if (session.getTransacted()) {
        session.commit();
    }
    Message response = consumer.receive(5000);
    try {
        int statusCode = response.getIntProperty("statusCode");
        if (statusCode < 200 || statusCode > 299) {
            throw new OperationUnsuccessfulException(response.getStringProperty("statusDescription"), statusCode);
        }
        if (response instanceof StreamMessage) {
            StreamMessage bodyStream = (StreamMessage) response;
            List<Object> result = new ArrayList<>();
            boolean done = false;
            do {
                try {
                    result.add(bodyStream.readObject());
                } catch (MessageEOFException mfe) {
                    // Expected - end of stream
                    done = true;
                }
            } while (!done);
            return result;
        } else if (response instanceof MapMessage) {
            MapMessage bodyMap = (MapMessage) response;
            Map<String, Object> result = new TreeMap<>();
            Enumeration mapNames = bodyMap.getMapNames();
            while (mapNames.hasMoreElements()) {
                String key = (String) mapNames.nextElement();
                result.put(key, bodyMap.getObject(key));
            }
            return result;
        } else if (response instanceof ObjectMessage) {
            return ((ObjectMessage) response).getObject();
        } else if (response instanceof BytesMessage) {
            BytesMessage bytesMessage = (BytesMessage) response;
            if (bytesMessage.getBodyLength() == 0) {
                return null;
            } else {
                byte[] buf = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(buf);
                return buf;
            }
        }
        throw new IllegalArgumentException("Cannot parse the results from a management operation.  JMS response message : " + response);
    } finally {
        if (session.getTransacted()) {
            session.commit();
        }
        consumer.close();
        if (_protocol == Protocol.AMQP_1_0) {
            ((TemporaryQueue) replyToDestination).delete();
        }
    }
}
Also used : Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) StreamMessage(javax.jms.StreamMessage) MapMessage(javax.jms.MapMessage) ObjectMessage(javax.jms.ObjectMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageEOFException(javax.jms.MessageEOFException) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) BytesMessage(javax.jms.BytesMessage) ObjectMessage(javax.jms.ObjectMessage) TemporaryQueue(javax.jms.TemporaryQueue) StreamMessage(javax.jms.StreamMessage) MessageProducer(javax.jms.MessageProducer) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 48 with TemporaryQueue

use of javax.jms.TemporaryQueue in project qpid-broker-j by apache.

the class TemporaryQueuePrefixTest method testNoPrefixSet.

@Test
public void testNoPrefixSet() throws Exception {
    Connection connection = getConnection();
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue queue = session.createTemporaryQueue();
        assertTrue(queue.getQueueName() + " does not start with \"TempQueue\".", queue.getQueueName().startsWith("TempQueue"));
    } finally {
        connection.close();
    }
}
Also used : Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) Session(javax.jms.Session) Test(org.junit.Test)

Example 49 with TemporaryQueue

use of javax.jms.TemporaryQueue in project qpid-broker-j by apache.

the class TemporaryQueuePrefixTest method testTwoDomains.

@Test
public void testTwoDomains() throws Exception {
    final String primaryPrefix = "/testPrefix";
    updateGlobalAddressDomains("[\"" + primaryPrefix + "\", \"/foo\" ]");
    Connection connection = getConnection();
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue queue = session.createTemporaryQueue();
        assertFalse(queue.getQueueName() + " has superfluous slash in prefix.", queue.getQueueName().startsWith(("[\"" + primaryPrefix + "\", \"/foo\" ]") + "/"));
        assertTrue(queue.getQueueName() + " does not start with expected prefix \"" + primaryPrefix + "\".", queue.getQueueName().startsWith(primaryPrefix));
    } finally {
        connection.close();
    }
}
Also used : Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) Session(javax.jms.Session) Test(org.junit.Test)

Example 50 with TemporaryQueue

use of javax.jms.TemporaryQueue in project qpid-broker-j by apache.

the class TemporaryQueuePrefixTest method testPrefix.

@Test
public void testPrefix() throws Exception {
    String prefix = "/testPrefix";
    updateGlobalAddressDomains("[ \"" + prefix + "\" ]");
    Connection connection = getConnection();
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue queue = session.createTemporaryQueue();
        assertTrue(queue.getQueueName() + " does not start with expected prefix \"" + prefix + "/\".", queue.getQueueName().startsWith(prefix + "/"));
    } finally {
        connection.close();
    }
}
Also used : Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

TemporaryQueue (javax.jms.TemporaryQueue)91 Session (javax.jms.Session)56 Test (org.junit.Test)47 Connection (javax.jms.Connection)45 MessageProducer (javax.jms.MessageProducer)38 MessageConsumer (javax.jms.MessageConsumer)35 Message (javax.jms.Message)27 JMSException (javax.jms.JMSException)25 TextMessage (javax.jms.TextMessage)24 JMSContext (javax.jms.JMSContext)13 BytesMessage (javax.jms.BytesMessage)10 Destination (javax.jms.Destination)8 Queue (javax.jms.Queue)8 ConnectionFactory (javax.jms.ConnectionFactory)7 ObjectMessage (javax.jms.ObjectMessage)7 JMSConsumer (javax.jms.JMSConsumer)6 TemporaryTopic (javax.jms.TemporaryTopic)6 HashMap (java.util.HashMap)5 MapMessage (javax.jms.MapMessage)5 StreamMessage (javax.jms.StreamMessage)5