Search in sources :

Example 56 with TemporaryQueue

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

the class JmsTempDestinationTest method testPublishFailsForDestroyedTempDestination.

/**
 * Make sure you cannot publish to a temp destination that does not exist
 * anymore.
 *
 * @throws JMSException
 * @throws InterruptedException
 */
@Test
public void testPublishFailsForDestroyedTempDestination() throws Exception {
    Connection tempConnection = factory.createConnection();
    connections.add(tempConnection);
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    connection.start();
    // In artemis, if you send a message to a topic where the consumer isn't there yet,
    // message will get lost. So the create temp queue request has to happen
    // after the connection is started (advisory consumer registered).
    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);
    // deleting the Queue will cause sends to fail
    queue.delete();
    // 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) {
        Assert.assertTrue("failed to throw an exception", true);
    }
}
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 57 with TemporaryQueue

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

the class JMSTemporaryDestinationTest method testDeleteTemporaryQueue.

@Test(timeout = 30000)
public void testDeleteTemporaryQueue() throws Exception {
    Connection connection = createConnection();
    try {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final javax.jms.Queue queue = session.createTemporaryQueue();
        assertNotNull(queue);
        assertTrue(queue instanceof TemporaryQueue);
        Queue queueView = getProxyToQueue(queue.getQueueName());
        assertNotNull(queueView);
        TemporaryQueue tempQueue = (TemporaryQueue) queue;
        tempQueue.delete();
        assertTrue("Temp Queue should be deleted.", Wait.waitFor(new Wait.Condition() {

            @Override
            public boolean isSatisfied() throws Exception {
                return getProxyToQueue(queue.getQueueName()) == null;
            }
        }, TimeUnit.SECONDS.toMillis(30), TimeUnit.MILLISECONDS.toMillis(50)));
    } finally {
        connection.close();
    }
}
Also used : Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) Queue(org.apache.activemq.artemis.core.server.Queue) TemporaryQueue(javax.jms.TemporaryQueue) Session(javax.jms.Session) Test(org.junit.Test)

Example 58 with TemporaryQueue

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

the class DuplexNetworkTest method testTempQueues.

@Test
public void testTempQueues() throws Exception {
    TemporaryQueue temp = localSession.createTemporaryQueue();
    MessageProducer producer = localSession.createProducer(temp);
    producer.send(localSession.createTextMessage("test"));
    Thread.sleep(100);
    assertEquals("Destination not created", 1, remoteBroker.getAdminView().getTemporaryQueues().length);
    temp.delete();
    assertTrue("Destination not deleted", Wait.waitFor(new Wait.Condition() {

        @Override
        public boolean isSatisified() throws Exception {
            return 0 == remoteBroker.getAdminView().getTemporaryQueues().length;
        }
    }));
}
Also used : TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Test(org.junit.Test)

Example 59 with TemporaryQueue

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

the class AdvisoryTempDestinationTests method testTempMessageConsumedAdvisory.

public void testTempMessageConsumedAdvisory() throws Exception {
    Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue queue = s.createTemporaryQueue();
    MessageConsumer consumer = s.createConsumer(queue);
    Topic advisoryTopic = AdvisorySupport.getMessageConsumedAdvisoryTopic((ActiveMQDestination) queue);
    MessageConsumer advisoryConsumer = s.createConsumer(advisoryTopic);
    // start throwing messages at the consumer
    MessageProducer producer = s.createProducer(queue);
    BytesMessage m = s.createBytesMessage();
    m.writeBytes(new byte[1024]);
    producer.send(m);
    String id = m.getJMSMessageID();
    Message msg = consumer.receive(1000);
    assertNotNull(msg);
    msg = advisoryConsumer.receive(1000);
    assertNotNull(msg);
    ActiveMQMessage message = (ActiveMQMessage) msg;
    ActiveMQMessage payload = (ActiveMQMessage) message.getDataStructure();
    String originalId = payload.getJMSMessageID();
    assertEquals(originalId, id);
}
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) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage)

Example 60 with TemporaryQueue

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

the class TempQueueMemoryTest method testLoadRequestReply.

public void testLoadRequestReply() throws Exception {
    for (int i = 0; i < numConsumers; i++) {
        serverSession.createConsumer(serverDestination).setMessageListener(new MessageListener() {

            @Override
            public void onMessage(Message msg) {
                try {
                    Destination replyTo = msg.getJMSReplyTo();
                    MessageProducer producer = serverSession.createProducer(replyTo);
                    producer.send(replyTo, msg);
                    if (serverTransactional) {
                        serverSession.commit();
                    }
                    producer.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    class Producer extends Thread {

        private final int numToSend;

        public Producer(int numToSend) {
            this.numToSend = numToSend;
        }

        @Override
        public void run() {
            try {
                Session session = clientConnection.createSession(clientTransactional, clientTransactional ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(serverDestination);
                for (int i = 0; i < numToSend; i++) {
                    TemporaryQueue replyTo = session.createTemporaryQueue();
                    MessageConsumer consumer = session.createConsumer(replyTo);
                    Message msg = session.createMessage();
                    msg.setJMSReplyTo(replyTo);
                    producer.send(msg);
                    if (clientTransactional) {
                        session.commit();
                    }
                    consumer.receive();
                    if (clientTransactional) {
                        session.commit();
                    }
                    consumer.close();
                    if (deleteTempQueue) {
                        replyTo.delete();
                    } else {
                    // temp queue will be cleaned up on clientConnection.close
                    }
                }
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
    Vector<Thread> threads = new Vector<>(numProducers);
    for (int i = 0; i < numProducers; i++) {
        threads.add(new Producer(messagesToSend / numProducers));
    }
    startAndJoinThreads(threads);
    clientSession.close();
    serverSession.close();
    clientConnection.close();
    serverConnection.close();
    AdvisoryBroker ab = (AdvisoryBroker) broker.getBroker().getAdaptor(AdvisoryBroker.class);
    // The server destination will be left
    assertTrue(ab.getAdvisoryDestinations().size() == 1);
    assertTrue("should be zero but is " + ab.getAdvisoryConsumers().size(), ab.getAdvisoryConsumers().size() == 0);
    assertTrue("should be zero but is " + ab.getAdvisoryProducers().size(), ab.getAdvisoryProducers().size() == 0);
    RegionBroker rb = (RegionBroker) broker.getBroker().getAdaptor(RegionBroker.class);
    assertTrue(rb.getDestinationMap().size() >= 6);
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) RegionBroker(org.apache.activemq.broker.region.RegionBroker) MessageListener(javax.jms.MessageListener) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Vector(java.util.Vector) 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