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();
}
}
}
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();
}
}
}
use of javax.jms.TemporaryQueue in project qpid-broker-j by apache.
the class JMSReplyToTest method testRequestResponseUsingTemporaryJmsReplyTo.
@Test
public void testRequestResponseUsingTemporaryJmsReplyTo() throws Exception {
Queue requestQueue = createQueue(getTestName() + ".request");
Connection connection = getConnection();
try {
AtomicReference<Throwable> exceptionHolder = createAsynchronousConsumer(connection, requestQueue);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TemporaryQueue replyToQueue = session.createTemporaryQueue();
MessageConsumer replyConsumer = session.createConsumer(replyToQueue);
Message requestMessage = session.createTextMessage("Request");
requestMessage.setJMSReplyTo(replyToQueue);
MessageProducer producer = session.createProducer(requestQueue);
producer.send(requestMessage);
Message responseMessage = replyConsumer.receive(getReceiveTimeout());
assertNotNull("Response message not received", responseMessage);
assertEquals("Correlation id of the response should match message id of the request", responseMessage.getJMSCorrelationID(), requestMessage.getJMSMessageID());
assertNull("Unexpected exception in responder", exceptionHolder.get());
} finally {
connection.close();
}
}
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();
}
}
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();
}
}
Aggregations