Search in sources :

Example 66 with TemporaryQueue

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

the class CreateLotsOfTemporaryQueuesTest method testCreateLotsOfTemporaryQueues.

public void testCreateLotsOfTemporaryQueues() throws Exception {
    LOG.info("Creating " + numberToCreate + " temporary queue(s)");
    Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
    for (int i = 0; i < numberToCreate; i++) {
        if (i % 1000 == 0) {
            LOG.info("attempt " + i);
        }
        TemporaryQueue temporaryQueue = session.createTemporaryQueue();
        temporaryQueue.delete();
        Thread.sleep(sleep);
    }
    LOG.info("Created " + numberToCreate + " temporary queue(s)");
}
Also used : TemporaryQueue(javax.jms.TemporaryQueue) Session(javax.jms.Session)

Example 67 with TemporaryQueue

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

the class AdvisoryOpenWireTest method testTempQueueLeakManyConnections.

@Test
public void testTempQueueLeakManyConnections() throws Exception {
    final Connection[] connections = new Connection[20];
    try {
        for (int i = 0; i < connections.length; i++) {
            connections[i] = factory.createConnection();
            connections[i].start();
        }
        Session session = connections[0].createSession(false, Session.AUTO_ACKNOWLEDGE);
        for (int i = 0; i < connections.length; i++) {
            TemporaryQueue temporaryQueue = session.createTemporaryQueue();
            temporaryQueue.delete();
        }
        Object[] addressResources = server.getManagementService().getResources(AddressControl.class);
        for (Object addressResource : addressResources) {
            if (((AddressControl) addressResource).getAddress().equals("ActiveMQ.Advisory.TempQueue")) {
                AddressControl addressControl = (AddressControl) addressResource;
                Wait.waitFor(() -> addressControl.getMessageCount() == 0);
                assertNotNull("addressControl for temp advisory", addressControl);
                Wait.assertEquals(0, addressControl::getMessageCount);
            }
        }
    // sleep a bit to allow message count to go down.
    } finally {
        for (Connection conn : connections) {
            if (conn != null) {
                conn.close();
            }
        }
    }
}
Also used : AddressControl(org.apache.activemq.artemis.api.core.management.AddressControl) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) Session(javax.jms.Session) Test(org.junit.Test)

Example 68 with TemporaryQueue

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

the class AdvisoryOpenWireTest method testTempQueueLeak.

@Test
public void testTempQueueLeak() throws Exception {
    Connection connection = null;
    try {
        connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue temporaryQueue = session.createTemporaryQueue();
        temporaryQueue.delete();
        Object[] queueResources = server.getManagementService().getResources(QueueControl.class);
        for (Object queueResource : queueResources) {
            if (((QueueControl) queueResource).getAddress().equals("ActiveMQ.Advisory.TempQueue")) {
                QueueControl queueControl = (QueueControl) queueResource;
                Wait.waitFor(() -> queueControl.getMessageCount() == 0);
                assertNotNull("addressControl for temp advisory", queueControl);
                Wait.assertEquals(0, queueControl::getMessageCount);
                Wait.assertEquals(2, queueControl::getMessagesAdded);
            }
        }
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
}
Also used : Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) Session(javax.jms.Session) Test(org.junit.Test)

Example 69 with TemporaryQueue

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

the class SimpleOpenWireTest method testTempQueueDelete.

@Test
public void testTempQueueDelete() throws Exception {
    connection.start();
    QueueSession queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue tempQueue = queueSession.createTemporaryQueue();
    ActiveMQConnection newConn = (ActiveMQConnection) factory.createConnection();
    try {
        QueueSession newQueueSession = newConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
        QueueSender queueSender = newQueueSession.createSender(tempQueue);
        Message msg = queueSession.createMessage();
        queueSender.send(msg);
        try {
            QueueReceiver consumer = newQueueSession.createReceiver(tempQueue);
            fail("should have gotten exception but got consumer: " + consumer);
        } catch (JMSException ex) {
        // correct
        }
        connection.close();
        try {
            Message newMsg = newQueueSession.createMessage();
            queueSender.send(newMsg);
        } catch (JMSException e) {
        // ok
        }
    } finally {
        newConn.close();
    }
}
Also used : MapMessage(javax.jms.MapMessage) StreamMessage(javax.jms.StreamMessage) ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) QueueSender(javax.jms.QueueSender) QueueReceiver(javax.jms.QueueReceiver) ActiveMQConnection(org.apache.activemq.ActiveMQConnection) TemporaryQueue(javax.jms.TemporaryQueue) JMSException(javax.jms.JMSException) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 70 with TemporaryQueue

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

the class SimpleRequestServer method main.

public static void main(final String[] args) throws Exception {
    final Map<String, TextMessage> requestMap = new HashMap<>();
    Connection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Start the request server
        SimpleRequestServer server = new SimpleRequestServer();
        server.start();
        // Step 2. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 3. Lookup the queue for sending the request message
        Queue requestQueue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 4. Lookup for the Connection Factory
        ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 5. Create a JMS Connection
        connection = cf.createConnection();
        // Step 6. Start the connection.
        connection.start();
        // Step 7. Create a JMS Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 8. Create a JMS Message Producer to send request message
        MessageProducer producer = session.createProducer(requestQueue);
        // Step 9. Create a temporary queue used to send reply message
        TemporaryQueue replyQueue = session.createTemporaryQueue();
        // Step 10. Create consumer to receive reply message
        MessageConsumer replyConsumer = session.createConsumer(replyQueue);
        // Step 11. Create a request Text Message
        TextMessage requestMsg = session.createTextMessage("A request message");
        // Step 12. Set the ReplyTo header so that the request receiver knows where to send the reply.
        requestMsg.setJMSReplyTo(replyQueue);
        // Step 13. Sent the request message
        producer.send(requestMsg);
        System.out.println("Request message sent.");
        // Step 14. Put the request message to the map. Later we can use it to
        // check out which request message a reply message is for. Here we use the MessageID as the
        // correlation id (JMSCorrelationID). You don't have to use it though. You can use some arbitrary string for
        // example.
        requestMap.put(requestMsg.getJMSMessageID(), requestMsg);
        // Step 15. Receive the reply message.
        TextMessage replyMessageReceived = (TextMessage) replyConsumer.receive();
        System.out.println("Received reply: " + replyMessageReceived.getText());
        System.out.println("CorrelatedId: " + replyMessageReceived.getJMSCorrelationID());
        // Step 16. Check out which request message is this reply message sent for.
        // Here we just have one request message for illustrative purpose. In real world there may be many requests and
        // many replies.
        TextMessage matchedMessage = requestMap.get(replyMessageReceived.getJMSCorrelationID());
        System.out.println("We found matched request: " + matchedMessage.getText());
        // Step 17. close the consumer.
        replyConsumer.close();
        // Step 18. Delete the temporary queue
        replyQueue.delete();
        // Step 19. Shutdown the request server
        server.shutdown();
    } finally {
        // Step 20. Be sure to close our JMS resources!
        if (connection != null) {
            connection.close();
        }
        // Step 21. Also close the initialContext!
        if (initialContext != null) {
            initialContext.close();
        }
    }
}
Also used : MessageConsumer(javax.jms.MessageConsumer) HashMap(java.util.HashMap) Connection(javax.jms.Connection) InitialContext(javax.naming.InitialContext) ConnectionFactory(javax.jms.ConnectionFactory) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TemporaryQueue(javax.jms.TemporaryQueue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

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