Search in sources :

Example 11 with TemporaryQueue

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

the class ActiveMQRASessionFactoryImpl method close.

/**
 * Close
 *
 * @throws JMSException Thrown if an error occurs
 */
@Override
public void close() throws JMSException {
    if (ActiveMQRASessionFactoryImpl.trace) {
        ActiveMQRALogger.LOGGER.trace("close() " + this);
    }
    if (closed) {
        return;
    }
    closed = true;
    synchronized (sessions) {
        for (Iterator<ActiveMQRASession> i = sessions.iterator(); i.hasNext(); ) {
            ActiveMQRASession session = i.next();
            try {
                session.closeSession();
            } catch (Throwable t) {
                ActiveMQRALogger.LOGGER.trace("Error closing session", t);
            }
            i.remove();
        }
    }
    synchronized (tempQueues) {
        for (Iterator<TemporaryQueue> i = tempQueues.iterator(); i.hasNext(); ) {
            TemporaryQueue temp = i.next();
            try {
                if (ActiveMQRASessionFactoryImpl.trace) {
                    ActiveMQRALogger.LOGGER.trace("Closing temporary queue " + temp + " for " + this);
                }
                temp.delete();
            } catch (Throwable t) {
                ActiveMQRALogger.LOGGER.trace("Error deleting temporary queue", t);
            }
            i.remove();
        }
    }
    synchronized (tempTopics) {
        for (Iterator<TemporaryTopic> i = tempTopics.iterator(); i.hasNext(); ) {
            TemporaryTopic temp = i.next();
            try {
                if (ActiveMQRASessionFactoryImpl.trace) {
                    ActiveMQRALogger.LOGGER.trace("Closing temporary topic " + temp + " for " + this);
                }
                temp.delete();
            } catch (Throwable t) {
                ActiveMQRALogger.LOGGER.trace("Error deleting temporary queue", t);
            }
            i.remove();
        }
    }
}
Also used : TemporaryQueue(javax.jms.TemporaryQueue) TemporaryTopic(javax.jms.TemporaryTopic)

Example 12 with TemporaryQueue

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

the class JmsTempDestinationTest method testDeleteDestinationWithSubscribersFails.

/**
 * Test you can't delete a Destination with Active Subscribers
 *
 * @throws JMSException
 */
@Test
public void testDeleteDestinationWithSubscribersFails() throws JMSException {
    Connection connection = factory.createConnection();
    connections.add(connection);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue queue = session.createTemporaryQueue();
    connection.start();
    session.createConsumer(queue);
    // now closed.
    try {
        queue.delete();
        Assert.fail("Should fail as Subscribers are active");
    } catch (JMSException e) {
        Assert.assertTrue("failed to throw an exception", true);
    }
}
Also used : Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) JMSException(javax.jms.JMSException) Session(javax.jms.Session) Test(org.junit.Test)

Example 13 with TemporaryQueue

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

the class JmsTempDestinationTest method testPublishFailsForClosedConnection.

/**
 * Make sure you cannot publish to a temp destination that does not exist
 * anymore.
 *
 * @throws JMSException
 * @throws InterruptedException
 * @throws URISyntaxException
 */
@Test
public void testPublishFailsForClosedConnection() throws Exception {
    Connection tempConnection = factory.createConnection();
    connections.add(tempConnection);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    connection.start();
    Session tempSession = tempConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final TemporaryQueue queue = tempSession.createTemporaryQueue();
    final ActiveMQConnection activeMQConnection = (ActiveMQConnection) connection;
    Assert.assertTrue("creation advisory received in time with async dispatch", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return activeMQConnection.activeTempDestinations.containsKey(queue);
        }
    }));
    // This message delivery should work since the temp connection is still
    // open.
    MessageProducer producer = session.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
    TextMessage message = session.createTextMessage("First");
    producer.send(message);
    // Closing the connection should destroy the temp queue that was
    // created.
    tempConnection.close();
    // Wait a little bit to let the delete take effect.
    Thread.sleep(5000);
    // now closed.
    try {
        message = session.createTextMessage("Hello");
        producer.send(message);
        Assert.fail("Send should fail since temp destination should not exist anymore.");
    } catch (JMSException e) {
        e.printStackTrace();
    }
}
Also used : Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 14 with TemporaryQueue

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

the class AdvisoryTempDestinationTests method testNoSlowConsumerAdvisory.

public void testNoSlowConsumerAdvisory() throws Exception {
    Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue queue = s.createTemporaryQueue();
    MessageConsumer consumer = s.createConsumer(queue);
    consumer.setMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message) {
        }
    });
    Topic advisoryTopic = AdvisorySupport.getSlowConsumerAdvisoryTopic((ActiveMQDestination) queue);
    s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
    // start throwing messages at the consumer
    MessageProducer producer = s.createProducer(queue);
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        BytesMessage m = s.createBytesMessage();
        m.writeBytes(new byte[1024]);
        producer.send(m);
    }
    Message msg = advisoryConsumer.receive(1000);
    assertNull(msg);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) MessageListener(javax.jms.MessageListener) TemporaryQueue(javax.jms.TemporaryQueue) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) Session(javax.jms.Session)

Example 15 with TemporaryQueue

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

the class AdvisoryTempDestinationTests method testSlowConsumerAdvisory.

public void testSlowConsumerAdvisory() throws Exception {
    Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue queue = s.createTemporaryQueue();
    MessageConsumer consumer = s.createConsumer(queue);
    assertNotNull(consumer);
    Topic advisoryTopic = AdvisorySupport.getSlowConsumerAdvisoryTopic((ActiveMQDestination) queue);
    s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
    // start throwing messages at the consumer
    MessageProducer producer = s.createProducer(queue);
    for (int i = 0; i < MESSAGE_COUNT; i++) {
        BytesMessage m = s.createBytesMessage();
        m.writeBytes(new byte[1024]);
        producer.send(m);
    }
    Message msg = advisoryConsumer.receive(1000);
    assertNotNull(msg);
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) TemporaryQueue(javax.jms.TemporaryQueue) BytesMessage(javax.jms.BytesMessage) MessageProducer(javax.jms.MessageProducer) Topic(javax.jms.Topic) 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