/** * Returns the result of an operation invocation or an attribute value. * <br> * If an error occurred on the server, {@link #hasOperationSucceeded(Message)} will return {@code false}. * and the result will be a String corresponding to the server exception. */ public static Object getResult(final ICoreMessage message) throws Exception { return getResult(message, null); }
/** * Stores an operation invocation in a message to invoke the corresponding operation the value from the server resource. * * @param message message * @param resourceName the name of the resource * @param operationName the name of the operation to invoke on the resource * @see ResourceNames */ public static void putOperationInvocation(final ICoreMessage message, final String resourceName, final String operationName) throws Exception { ManagementHelper.putOperationInvocation(message, resourceName, operationName, (Object[]) null); }
/** * Returns whether the invocation of the management operation on the server resource succeeded. */ public static boolean hasOperationSucceeded(final Message message) throws JMSException { return ManagementHelper.hasOperationSucceeded(JMSManagementHelper.getCoreMessage(message)); }
protected Object invokeSyncOperation(final String resourceName, final String operationName, final Object... parameters) throws Exception { ClientMessage message = clientSession.createMessage(false); ManagementHelper.putOperationInvocation(message, resourceName, operationName, parameters); ClientMessage reply; try { reply = requestor.request(message, 3000); } catch (Exception e) { throw new IllegalStateException("Exception while invoking " + operationName + " on " + resourceName, e); } if (reply == null) { throw new IllegalStateException("no reply received when invoking " + operationName + " on " + resourceName); } if (!ManagementHelper.hasOperationSucceeded(reply)) { throw new IllegalStateException("operation failed when invoking " + operationName + " on " + resourceName + ": " + ManagementHelper.getResult(reply)); } return ManagementHelper.getResult(reply); }
private Integer getQueueID(ClientSession session, SimpleString queueName) throws Exception { Integer queueID = -1; Object result; try (ClientRequestor requestor = new ClientRequestor(session, "activemq.management")) { ClientMessage managementMessage = session.createMessage(false); ManagementHelper.putAttribute(managementMessage, ResourceNames.QUEUE + queueName, "ID"); session.start(); logger.debug("Requesting ID for: " + queueName); ClientMessage reply = requestor.request(managementMessage); result = ManagementHelper.getResult(reply); } if (result != null && result instanceof Number) { queueID = ((Number) result).intValue(); } return queueID; }
@Test public void testHandleManagementMessageWithUnknownAttribute() throws Exception { Configuration config = createBasicConfig().setJMXManagementEnabled(false); ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, false)); server.start(); // invoke attribute and operation on the server ICoreMessage message = new CoreMessage(1, 100); ManagementHelper.putAttribute(message, ResourceNames.BROKER, "started"); ICoreMessage reply = server.getManagementService().handleMessage(message); Assert.assertTrue(ManagementHelper.hasOperationSucceeded(reply)); Assert.assertTrue((Boolean) ManagementHelper.getResult(reply)); }
/** * Returns whether the JMS message corresponds to the result of a management attribute value. */ public static boolean isAttributesResult(final Message message) { return !ManagementHelper.isOperationResult(message); }
@Test public void testHandleManagementMessageWithOperation() throws Exception { String queue = RandomUtil.randomString(); String address = RandomUtil.randomString(); Configuration config = createBasicConfig().setJMXManagementEnabled(false); ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, false)); server.start(); // invoke attribute and operation on the server CoreMessage message = new CoreMessage(1, 100); ManagementHelper.putOperationInvocation(message, ResourceNames.BROKER, "createQueue", queue, address); Message reply = server.getManagementService().handleMessage(message); Assert.assertTrue(ManagementHelper.hasOperationSucceeded(reply)); }
/** * Returns the result of an operation invocation or an attribute value. * <br> * If an error occurred on the server, {@link #hasOperationSucceeded(Message)} will return {@code false}. * and the result will be a String corresponding to the server exception. */ public static Object[] getResults(final Message message) throws Exception { return ManagementHelper.getResults(JMSManagementHelper.getCoreMessage(message)); }
/** * Stores a resource attribute in a JMS message to retrieve the value from the server resource. * * @param message JMS message * @param resourceName the name of the resource * @param attribute the name of the attribute * @throws JMSException if an exception occurs while putting the information in the message * @see org.apache.activemq.artemis.api.core.management.ResourceNames */ public static void putAttribute(final Message message, final String resourceName, final String attribute) throws JMSException { ManagementHelper.putAttribute(JMSManagementHelper.getCoreMessage(message), resourceName, attribute); }
/** * Returns whether the JMS message corresponds to the result of a management attribute value. */ public static boolean isAttributesResult(final Message message) throws JMSException { return ManagementHelper.isAttributesResult(JMSManagementHelper.getCoreMessage(message)); }
public Object invokeOperation(final Class desiredType, final String operationName, final Object... args) throws Exception { try (ClientSessionFactory sessionFactory = locator.createSessionFactory(); ClientSession session = getSession(sessionFactory); ClientRequestor requestor = getClientRequestor(session)) { ClientMessage m = session.createMessage(false); ManagementHelper.putOperationInvocation(m, resourceName, operationName, args); ClientMessage reply = requestor.request(m); if (reply != null) { if (ManagementHelper.hasOperationSucceeded(reply)) { return ManagementHelper.getResult(reply, desiredType); } else { throw new Exception((String) ManagementHelper.getResult(reply)); } } else { return null; } } }
ManagementHelper.putAttribute(managementMessage, ResourceNames.QUEUE + queue, "ID"); managementSession.start(); if (logger.isDebugEnabled()) { Number idObject = (Number) ManagementHelper.getResult(reply); queueID = idObject.longValue();
@Test public void testHandleManagementMessageWithKnownAttribute() throws Exception { Configuration config = createBasicConfig().setJMXManagementEnabled(false); ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, false)); server.start(); // invoke attribute and operation on the server ICoreMessage message = new CoreMessage(1, 100); ManagementHelper.putAttribute(message, ResourceNames.BROKER, "attribute.Does.Not.Exist"); ICoreMessage reply = server.getManagementService().handleMessage(message); Assert.assertFalse(ManagementHelper.hasOperationSucceeded(reply)); Assert.assertNotNull(ManagementHelper.getResult(reply)); }
/** * Returns whether the JMS message corresponds to the result of a management operation invocation. */ public static boolean isOperationResult(final Message message) throws JMSException { return ManagementHelper.isOperationResult(JMSManagementHelper.getCoreMessage(message)); }
/** * Returns the result of an operation invocation or an attribute value. * <br> * If an error occurred on the server, {@link #hasOperationSucceeded(Message)} will return {@code false}. * and the result will be a String corresponding to the server exception. */ public static Object getResult(final ICoreMessage message, Class desiredType) throws Exception { Object[] res = ManagementHelper.getResults(message); if (res != null) { return JsonUtil.convertJsonValue(res[0], desiredType); } else { return null; } }
/** * Stores a resource attribute in a JMS message to retrieve the value from the server resource. * * @param message JMS message * @param resourceName the name of the resource * @param attribute the name of the attribute * @throws JMSException if an exception occurs while putting the information in the message * @see org.apache.activemq.artemis.api.core.management.ResourceNames */ public static void putAttribute(final Message message, final String resourceName, final String attribute) throws JMSException { ManagementHelper.putAttribute(JMSManagementHelper.getCoreMessage(message), resourceName, attribute); }
/** * Returns whether the JMS message corresponds to the result of a management attribute value. */ public static boolean isAttributesResult(final Message message) throws JMSException { return ManagementHelper.isAttributesResult(JMSManagementHelper.getCoreMessage(message)); }
@Test public void testHandleManagementMessageWithOperationWhichFails() throws Exception { Configuration config = createBasicConfig().setJMXManagementEnabled(false); ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, false)); server.start(); // invoke attribute and operation on the server CoreMessage message = new CoreMessage(1, 100); ManagementHelper.putOperationInvocation(message, ResourceNames.BROKER, "thereIsNoSuchOperation"); ICoreMessage reply = server.getManagementService().handleMessage(message); Assert.assertFalse(ManagementHelper.hasOperationSucceeded(reply)); Assert.assertNotNull(ManagementHelper.getResult(reply)); }
/** * Stores an operation invocation in a JMS message to invoke the corresponding operation the value from the server resource. * * @param message JMS message * @param resourceName the name of the resource * @param operationName the name of the operation to invoke on the resource * @throws JMSException if an exception occurs while putting the information in the message * @see org.apache.activemq.artemis.api.core.management.ResourceNames */ public static void putOperationInvocation(final Message message, final String resourceName, final String operationName) throws JMSException { try { ManagementHelper.putOperationInvocation(JMSManagementHelper.getCoreMessage(message), resourceName, operationName); } catch (Exception e) { throw JMSManagementHelper.convertFromException(e); } }