Search in sources :

Example 21 with TemporaryQueue

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

the class TemporaryQueueExample method main.

public static void main(final String[] args) throws Exception {
    Connection connection = null;
    InitialContext initialContext = null;
    try {
        // Step 1. Create an initial context to perform the JNDI lookup.
        initialContext = new InitialContext();
        // Step 2. Look-up the JMS connection factory
        ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 3. Create a JMS Connection
        connection = cf.createConnection();
        // Step 4. Start the connection
        connection.start();
        // Step 5. Create a JMS session with AUTO_ACKNOWLEDGE mode
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 6. Create a Temporary Queue
        TemporaryQueue tempQueue = session.createTemporaryQueue();
        System.out.println("Temporary queue is created: " + tempQueue);
        // Step 7. Create a JMS message producer
        MessageProducer messageProducer = session.createProducer(tempQueue);
        // Step 8. Create a text message
        TextMessage message = session.createTextMessage("This is a text message");
        // Step 9. Send the text message to the queue
        messageProducer.send(message);
        System.out.println("Sent message: " + message.getText());
        // Step 11. Create a message consumer
        MessageConsumer messageConsumer = session.createConsumer(tempQueue);
        // Step 12. Receive the message from the queue
        message = (TextMessage) messageConsumer.receive(5000);
        System.out.println("Received message: " + message.getText());
        // Step 13. Close the consumer and producer
        messageConsumer.close();
        messageProducer.close();
        // Step 14. Delete the temporary queue
        tempQueue.delete();
        // Step 15. Create another temporary queue.
        TemporaryQueue tempQueue2 = session.createTemporaryQueue();
        System.out.println("Another temporary queue is created: " + tempQueue2);
        // Step 16. Close the connection.
        connection.close();
        // Step 17. Create a new connection.
        connection = cf.createConnection();
        // Step 18. Create a new session.
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 19. Try to access the tempQueue2 outside its lifetime
        try {
            messageConsumer = session.createConsumer(tempQueue2);
            throw new Exception("Temporary queue cannot be accessed outside its lifecycle!");
        } catch (JMSException e) {
            System.out.println("Exception got when trying to access a temp queue outside its scope: " + e);
        }
    } finally {
        if (connection != null) {
            // Step 20. Be sure to close our JMS resources!
            connection.close();
        }
        if (initialContext != null) {
            // Step 21. Also close the initialContext!
            initialContext.close();
        }
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) JMSException(javax.jms.JMSException) MessageProducer(javax.jms.MessageProducer) InitialContext(javax.naming.InitialContext) TextMessage(javax.jms.TextMessage) JMSException(javax.jms.JMSException) Session(javax.jms.Session)

Example 22 with TemporaryQueue

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

the class DynamicallyIncludedDestinationsDuplexNetworkTest 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();
    Thread.sleep(100);
    assertEquals("Destination not deleted", 0, remoteBroker.getAdminView().getTemporaryQueues().length);
}
Also used : TemporaryQueue(javax.jms.TemporaryQueue) MessageProducer(javax.jms.MessageProducer) Test(org.junit.Test)

Example 23 with TemporaryQueue

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

the class BasicSecurityTest method testCreateTempDestinationAuthorization.

@Test
public void testCreateTempDestinationAuthorization() throws Exception {
    Connection conn1 = null;
    Connection conn2 = null;
    // Sender
    try {
        conn1 = factory.createConnection("openwireGuest", "GuEsT");
        conn1.start();
        conn2 = factory.createConnection("openwireDestinationManager", "DeStInAtIoN");
        conn2.start();
        Session session1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
        try {
            session1.createTemporaryQueue();
            fail("user shouldn't be able to create temp queue");
        } catch (JMSSecurityException e) {
        // expected
        }
        Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue q = session2.createTemporaryQueue();
        assertNotNull(q);
    } finally {
        if (conn1 != null) {
            conn1.close();
        }
        if (conn2 != null) {
            conn2.close();
        }
    }
}
Also used : JMSSecurityException(javax.jms.JMSSecurityException) Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) Session(javax.jms.Session) Test(org.junit.Test)

Example 24 with TemporaryQueue

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

the class SimpleOpenWireTest method testSimpleTempQueue.

@Test
public void testSimpleTempQueue() throws Exception {
    AddressSettings addressSetting = new AddressSettings();
    addressSetting.setAutoCreateQueues(true);
    addressSetting.setAutoCreateAddresses(true);
    String address = "#";
    server.getAddressSettingsRepository().addMatch(address, addressSetting);
    connection.start();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    System.out.println("creating temp queue");
    TemporaryQueue tempQueue = session.createTemporaryQueue();
    System.out.println("create consumer 1");
    MessageConsumer consumer1 = session.createConsumer(tempQueue);
    System.out.println("create producer");
    MessageProducer producer = session.createProducer(tempQueue);
    System.out.println("sending messages");
    final int num = 1;
    final String msgBase = "MfromAMQ-";
    for (int i = 0; i < num; i++) {
        TextMessage msg = session.createTextMessage("MfromAMQ-" + i);
        producer.send(msg);
        System.out.println("Sent a message");
    }
    // receive
    System.out.println("receiving messages...");
    for (int i = 0; i < num; i++) {
        TextMessage msg = (TextMessage) consumer1.receive(5000);
        System.out.println("received: " + msg);
        String content = msg.getText();
        assertEquals(msgBase + i, content);
    }
    assertNull(consumer1.receive(500));
    session.close();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) MessageConsumer(javax.jms.MessageConsumer) TemporaryQueue(javax.jms.TemporaryQueue) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.ActiveMQSession) TopicSession(javax.jms.TopicSession) QueueSession(javax.jms.QueueSession) Test(org.junit.Test)

Example 25 with TemporaryQueue

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

the class ThreeBrokerTempQueueNetworkTest method testTempQueueRecovery.

// this actually uses 4 brokers ...
public void testTempQueueRecovery() throws Exception {
    // Setup broker networks
    bridgeBrokers("BrokerA", "BrokerB", false, 3);
    bridgeBrokers("BrokerB", "BrokerC", false, 3);
    startAllBrokers();
    BrokerItem brokerItem = brokers.get("BrokerC");
    Connection conn = brokerItem.createConnection();
    conn.start();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue tempQ = sess.createTemporaryQueue();
    Thread.sleep(5000);
    for (Iterator<BrokerItem> i = brokers.values().iterator(); i.hasNext(); ) {
        BrokerItem bi = i.next();
        assertEquals("No queues on broker " + bi.broker.getBrokerName(), 1, bi.broker.getAdminView().getTemporaryQueues().length);
    }
    createBroker(new URI("broker:(tcp://localhost:61619)/BrokerD?persistent=false&useJmx=true"));
    bridgeBrokers("BrokerD", "BrokerA", false, 3);
    BrokerItem newBroker = brokers.get("BrokerD");
    newBroker.broker.start();
    Thread.sleep(1000);
    assertEquals("No queues on broker D", 1, newBroker.broker.getAdminView().getTemporaryQueues().length);
    tempQ.delete();
    Thread.sleep(2000);
    for (Iterator<BrokerItem> i = brokers.values().iterator(); i.hasNext(); ) {
        BrokerItem bi = i.next();
        assertEquals("Temp queue left behind on broker " + bi.broker.getBrokerName(), 0, bi.broker.getAdminView().getTemporaryQueues().length);
    }
}
Also used : Connection(javax.jms.Connection) TemporaryQueue(javax.jms.TemporaryQueue) URI(java.net.URI) 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