Search in sources :

Example 11 with QueueRequestor

use of javax.jms.QueueRequestor in project wildfly by wildfly.

the class RemoteActiveMQProviderJMSOperations method createRemoteQueue.

private void createRemoteQueue(String queueName) {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616", "guest", "guest");
    try (Connection connection = cf.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
        connection.start();
        Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
        QueueRequestor requestor = new QueueRequestor((QueueSession) session, managementQueue);
        Message m = session.createMessage();
        org.apache.activemq.artemis.api.jms.management.JMSManagementHelper.putOperationInvocation(m, ResourceNames.BROKER, "createQueue", queueName, queueName, true, RoutingType.ANYCAST.name());
        Message reply = requestor.request(m);
        System.out.println("Creating queue " + queueName + " returned " + reply);
        if (!reply.getBooleanProperty("_AMQ_OperationSucceeded")) {
            String body = reply.getBody(String.class);
            if (!destinationAlreadyExist(body)) {
                System.out.println("Creation of queue " + queueName + " has failed because of " + body);
            }
        }
    } catch (JMSException ex) {
        ex.printStackTrace();
        throw new JMSOperationsException(ex);
    }
    System.out.println("Queue " + queueName + " has been created");
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) QueueRequestor(javax.jms.QueueRequestor) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Queue(javax.jms.Queue) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession)

Example 12 with QueueRequestor

use of javax.jms.QueueRequestor in project wildfly by wildfly.

the class RemoteActiveMQProviderJMSOperations method deleteRemoteTopic.

private void deleteRemoteTopic(String topicName) {
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61616", "guest", "guest");
    try (Connection connection = cf.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
        connection.start();
        Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
        QueueRequestor requestor = new QueueRequestor((QueueSession) session, managementQueue);
        Message m = session.createMessage();
        org.apache.activemq.artemis.api.jms.management.JMSManagementHelper.putOperationInvocation(m, ResourceNames.BROKER, "deleteAddress", topicName, true);
        Message reply = requestor.request(m);
        System.out.println("Deleting topic " + topicName + " returned " + reply);
        if (!reply.getBooleanProperty("_AMQ_OperationSucceeded")) {
            String body = reply.getBody(String.class);
            System.out.println("Deleting of topic " + topicName + " has failed because of " + body);
        }
    } catch (JMSException ex) {
        ex.printStackTrace();
        throw new JMSOperationsException(ex);
    }
    System.out.println("Topic " + topicName + " has been deleted");
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) QueueRequestor(javax.jms.QueueRequestor) Message(javax.jms.Message) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) Queue(javax.jms.Queue) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession)

Example 13 with QueueRequestor

use of javax.jms.QueueRequestor in project activemq-artemis by apache.

the class ArtemisFeatureTest method test.

@Test(timeout = 5 * 60 * 1000)
public void test() throws Throwable {
    executeCommand("bundle:list");
    withinReason(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            assertTrue("artemis bundle installed", verifyBundleInstalled("artemis-server-osgi"));
            return true;
        }
    });
    Object service = waitForService("(objectClass=org.apache.activemq.artemis.core.server.ActiveMQServer)", 30000);
    assertNotNull(service);
    LOG.info("have service " + service);
    executeCommand("service:list -n");
    Connection connection = null;
    try {
        JmsConnectionFactory factory = new JmsConnectionFactory("amqp://localhost:5672");
        connection = factory.createConnection(USER, PASSWORD);
        connection.start();
        QueueSession sess = (QueueSession) connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        Queue queue = sess.createQueue("exampleQueue");
        MessageProducer producer = sess.createProducer(queue);
        producer.send(sess.createTextMessage("TEST"));
        // Test browsing
        try (QueueBrowser browser = sess.createBrowser(queue)) {
            Enumeration messages = browser.getEnumeration();
            while (messages.hasMoreElements()) {
                messages.nextElement();
            }
        }
        // Test management
        Queue managementQueue = sess.createQueue("activemq.management");
        QueueRequestor requestor = new QueueRequestor(sess, managementQueue);
        connection.start();
        TextMessage m = sess.createTextMessage();
        m.setStringProperty("_AMQ_ResourceName", "broker");
        m.setStringProperty("_AMQ_OperationName", "getQueueNames");
        m.setText("[\"ANYCAST\"]");
        Message reply = requestor.request(m);
        String json = ((TextMessage) reply).getText();
        JsonArray array = Json.createReader(new StringReader(json)).readArray();
        List<JsonString> queues = (List<JsonString>) array.get(0);
        assertNotNull(queues);
        assertFalse(queues.isEmpty());
        MessageConsumer consumer = sess.createConsumer(queue);
        Message msg = consumer.receive(5000);
        assertNotNull(msg);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) Connection(javax.jms.Connection) JsonString(javax.json.JsonString) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) JsonArray(javax.json.JsonArray) QueueRequestor(javax.jms.QueueRequestor) StringReader(java.io.StringReader) List(java.util.List) ArrayList(java.util.ArrayList) MessageProducer(javax.jms.MessageProducer) JsonString(javax.json.JsonString) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) QueueBrowser(javax.jms.QueueBrowser) TextMessage(javax.jms.TextMessage) Test(org.junit.Test)

Example 14 with QueueRequestor

use of javax.jms.QueueRequestor in project activemq-artemis by apache.

the class PreacknowledgeExample method getMessageCount.

// To do this we send a management message to get the message count.
// In real life you wouldn't create a new session every time you send a management message
private static int getMessageCount(final Connection connection) throws Exception {
    QueueSession session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
    QueueRequestor requestor = new QueueRequestor(session, managementQueue);
    connection.start();
    Message m = session.createMessage();
    JMSManagementHelper.putAttribute(m, ResourceNames.QUEUE + "exampleQueue", "messageCount");
    Message response = requestor.request(m);
    int messageCount = (Integer) JMSManagementHelper.getResult(response, Integer.class);
    return messageCount;
}
Also used : QueueRequestor(javax.jms.QueueRequestor) QueueConnection(javax.jms.QueueConnection) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession)

Example 15 with QueueRequestor

use of javax.jms.QueueRequestor in project activemq-artemis by apache.

the class ManagementExample method main.

public static void main(final String[] args) throws Exception {
    QueueConnection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Perfom a lookup on the queue
        Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 3. Perform a lookup on the Connection Factory
        QueueConnectionFactory cf = (QueueConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4.Create a JMS Connection
        connection = cf.createQueueConnection();
        // Step 5. Create a JMS Session
        QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 6. Create a JMS Message Producer
        MessageProducer producer = session.createProducer(queue);
        // Step 7. Create a Text Message
        TextMessage message = session.createTextMessage("This is a text message");
        System.out.println("Sent message: " + message.getText());
        // Step 8. Send the Message
        producer.send(message);
        // Step 9. create the JMS management queue.
        // It is a "special" queue and it is not looked up from JNDI but constructed directly
        Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");
        // Step 10. Create a QueueRequestor for the management queue (see queue-requestor example)
        QueueRequestor requestor = new QueueRequestor(session, managementQueue);
        // Step 11. Start the Connection to allow the queue requestor to receive replies
        connection.start();
        // Step 12. Create a JMS message which is used to send a management message
        Message m = session.createMessage();
        // Step 13. Use a helper class to fill the JMS message with management information:
        // * the name of the resource to manage
        // * in this case, we want to retrieve the value of the messageCount of the queue
        JMSManagementHelper.putAttribute(m, ResourceNames.QUEUE + "exampleQueue", "messageCount");
        // Step 14. Use the requestor to send the request and wait for the reply
        Message reply = requestor.request(m);
        // Step 15. Use a helper class to retrieve the operation result
        int messageCount = (Integer) JMSManagementHelper.getResult(reply, Integer.class);
        System.out.println(queue.getQueueName() + " contains " + messageCount + " messages");
        // Step 16. Create another JMS message to use as a management message
        m = session.createMessage();
        // Step 17. Use a helper class to fill the JMS message with management information:
        // * the object name of the resource to manage (i.e. the queue)
        // * in this case, we want to call the "removeMessage" operation with the JMS MessageID
        // of the message sent to the queue in step 8.
        JMSManagementHelper.putOperationInvocation(m, ResourceNames.QUEUE + "exampleQueue", "removeMessages", FilterConstants.ACTIVEMQ_USERID + " = '" + message.getJMSMessageID() + "'");
        // Step 18 Use the requestor to send the request and wait for the reply
        reply = requestor.request(m);
        // Step 19. Use a helper class to check that the operation has succeeded
        boolean success = JMSManagementHelper.hasOperationSucceeded(reply);
        System.out.println("operation invocation has succeeded: " + success);
        // Step 20. Use a helper class to retrieve the operation result
        // in that case, a long which is 1 if the message was removed, 0 else
        boolean messageRemoved = 1 == (long) JMSManagementHelper.getResult(reply);
        System.out.println("message has been removed: " + messageRemoved);
        // Step 21. Create a JMS Message Consumer on the queue
        MessageConsumer messageConsumer = session.createConsumer(queue);
        // Step 22. Trying to receive a message. Since the only message in the queue was removed by a management
        // operation,
        // there is none to consume. The call will timeout after 5000ms and messageReceived will be null
        TextMessage messageReceived = (TextMessage) messageConsumer.receive(5000);
        System.out.println("Received message: " + messageReceived);
    } finally {
        // Step 23. Be sure to close the resources!
        if (initialContext != null) {
            initialContext.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) QueueConnectionFactory(javax.jms.QueueConnectionFactory) InitialContext(javax.naming.InitialContext) QueueRequestor(javax.jms.QueueRequestor) QueueConnection(javax.jms.QueueConnection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) QueueSession(javax.jms.QueueSession) TextMessage(javax.jms.TextMessage)

Aggregations

QueueRequestor (javax.jms.QueueRequestor)19 Message (javax.jms.Message)16 QueueSession (javax.jms.QueueSession)16 Queue (javax.jms.Queue)11 Connection (javax.jms.Connection)9 Session (javax.jms.Session)8 JMSException (javax.jms.JMSException)6 TextMessage (javax.jms.TextMessage)6 QueueConnection (javax.jms.QueueConnection)5 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)4 StringReader (java.io.StringReader)2 List (java.util.List)2 MessageConsumer (javax.jms.MessageConsumer)2 MessageProducer (javax.jms.MessageProducer)2 QueueConnectionFactory (javax.jms.QueueConnectionFactory)2 InitialContext (javax.naming.InitialContext)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 QueueBrowser (javax.jms.QueueBrowser)1