Java Code Examples for javax.jms.Session#createTemporaryQueue()

The following examples show how to use javax.jms.Session#createTemporaryQueue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TemporaryQueuePrefixTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyPrefix() throws Exception
{
    updateGlobalAddressDomains("[]");

    Connection connection = getConnection();
    try
    {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        TemporaryQueue queue = session.createTemporaryQueue();

        assertTrue(queue.getQueueName() + " does not start with \"TempQueue\".",
                   queue.getQueueName().startsWith("TempQueue"));
    }
    finally
    {
        connection.close();
    }
}
 
Example 2
Source File: JmsPoolMessageConsumerTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetMessageSelector() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createQueueConnection();
    Session session = connection.createSession();
    Queue queue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(queue, "Color = Red");

    assertNotNull(consumer.getMessageSelector());
    assertEquals("Color = Red", consumer.getMessageSelector());

    consumer.close();

    try {
        consumer.getMessageSelector();
        fail("Should not be able to interact with closed consumer");
    } catch (IllegalStateException ise) {}
}
 
Example 3
Source File: BrokerStatisticsPluginTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testDestinationStats() throws Exception {
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue replyTo = session.createTemporaryQueue();
   MessageConsumer consumer = session.createConsumer(replyTo);
   Queue testQueue = session.createQueue("Test.Queue");
   MessageProducer producer = session.createProducer(null);
   Queue query = session.createQueue(StatisticsBroker.STATS_DESTINATION_PREFIX + testQueue.getQueueName());
   Message msg = session.createMessage();

   producer.send(testQueue, msg);

   msg.setJMSReplyTo(replyTo);
   producer.send(query, msg);
   MapMessage reply = (MapMessage) consumer.receive(10 * 1000);
   assertNotNull(reply);
   assertTrue(reply.getMapNames().hasMoreElements());
   assertTrue(reply.getJMSTimestamp() > 0);
   assertEquals(Message.DEFAULT_PRIORITY, reply.getJMSPriority());
     /*
     for (Enumeration e = reply.getMapNames();e.hasMoreElements();) {
         String name = e.nextElement().toString();
         System.err.println(name+"="+reply.getObject(name));
     }
     */
}
 
Example 4
Source File: SessionIntegrationTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testCreateAndDeleteTemporaryQueue() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        connection.start();

        testPeer.expectBegin();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        String dynamicAddress = "myTempQueueAddress";
        testPeer.expectTempQueueCreationAttach(dynamicAddress);
        TemporaryQueue tempQueue = session.createTemporaryQueue();

        // Deleting the TemporaryQueue will be achieved by closing its creating link.
        testPeer.expectDetach(true, true, true);
        tempQueue.delete();

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 5
Source File: JmsPoolSessionTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testClose() throws Exception {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    assertEquals(0, connection.getNumtIdleSessions());
    session.close();
    assertEquals(1, connection.getNumtIdleSessions());

    try {
        session.close();
    } catch (JMSException ex) {
        fail("Shouldn't fail on second close call.");
    }

    try {
        session.createTemporaryQueue();
        fail("Session should be closed.");
    } catch (IllegalStateException ise) {}
}
 
Example 6
Source File: JmsTemporaryQueueTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testCantDeleteTemporaryQueueWithConsumers() throws Exception {
    connection = createAmqpConnection();
    connection.start();

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    TemporaryQueue tempQueue = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(tempQueue);

    try {
        tempQueue.delete();
        fail("should not be able to delete temporary queue with active consumers");
    } catch (IllegalStateException ide) {
        // expected
    }

    consumer.close();

    // Now it should be allowed
    tempQueue.delete();
}
 
Example 7
Source File: PooledSessionTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testMessageProducersAreAllTheSame() throws Exception {
    JmsPoolConnection connection = (JmsPoolConnection) pooledFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Queue queue1 = session.createTemporaryQueue();
    Queue queue2 = session.createTemporaryQueue();

    JmsPoolMessageProducer producer1 = (JmsPoolMessageProducer) session.createProducer(queue1);
    JmsPoolMessageProducer producer2 = (JmsPoolMessageProducer) session.createProducer(queue2);

    assertSame(producer1.getMessageProducer(), producer2.getMessageProducer());

    connection.close();
}
 
Example 8
Source File: TwoBrokerTempQueueAdvisoryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void sendReceiveTempQueueMessage(String broker) throws Exception {

      ConnectionFactory factory = getConnectionFactory(broker);
      Connection conn = factory.createConnection();
      Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      session.createTemporaryQueue();
      conn.close();
   }
 
Example 9
Source File: JmsTemplate.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Send a request message to the given {@link Destination} and block until
 * a reply has been received on a temporary queue created on-the-fly.
 * <p>Return the response message or {@code null} if no message has
 * @throws JMSException if thrown by JMS API methods
 */
@Nullable
protected Message doSendAndReceive(Session session, Destination destination, MessageCreator messageCreator)
		throws JMSException {

	Assert.notNull(messageCreator, "MessageCreator must not be null");
	TemporaryQueue responseQueue = null;
	MessageProducer producer = null;
	MessageConsumer consumer = null;
	try {
		Message requestMessage = messageCreator.createMessage(session);
		responseQueue = session.createTemporaryQueue();
		producer = session.createProducer(destination);
		consumer = session.createConsumer(responseQueue);
		requestMessage.setJMSReplyTo(responseQueue);
		if (logger.isDebugEnabled()) {
			logger.debug("Sending created message: " + requestMessage);
		}
		doSend(producer, requestMessage);
		return receiveFromConsumer(consumer, getReceiveTimeout());
	}
	finally {
		JmsUtils.closeMessageConsumer(consumer);
		JmsUtils.closeMessageProducer(producer);
		if (responseQueue != null) {
			responseQueue.delete();
		}
	}
}
 
Example 10
Source File: MessagingACLTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTemporaryQueueFailed() throws Exception
{
    assumeThat("QPID-7919",
               getProtocol(),
               is(not(equalTo(Protocol.AMQP_1_0))));

    configureACL(String.format("ACL ALLOW-LOG %s ACCESS VIRTUALHOST", USER1),
                 String.format("ACL DENY-LOG %s CREATE QUEUE temporary=\"true\"", USER1));

    Connection connection = getConnectionBuilder().setUsername(USER1).setPassword(USER1_PASSWORD).build();
    try
    {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        connection.start();

        try
        {
            session.createTemporaryQueue();
            fail("Test failed as creation succeeded.");
        }
        catch (JMSException e)
        {
            assertJMSExceptionMessageContains(e, "Permission CREATE is denied for : Queue");
        }
    }
    finally
    {
        connection.close();
    }
}
 
Example 11
Source File: PooledSessionNoPublisherCachingTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testMessageProducersAreUnique() throws Exception {
    JmsPoolConnection connection = (JmsPoolConnection) pooledFactory.createConnection();
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Queue queue1 = session.createTemporaryQueue();
    Queue queue2 = session.createTemporaryQueue();

    JmsPoolMessageProducer producer1 = (JmsPoolMessageProducer) session.createProducer(queue1);
    JmsPoolMessageProducer producer2 = (JmsPoolMessageProducer) session.createProducer(queue2);

    assertNotSame(producer1.getMessageProducer(), producer2.getMessageProducer());
}
 
Example 12
Source File: TemporaryQueueTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testClosingSessionDoesNotDeleteQueue() throws Exception
{
    final Connection connection = getConnection();
    try
    {
        final Session session1 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final Session session2 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final TemporaryQueue queue = session1.createTemporaryQueue();
        assertNotNull("Temporary queue cannot be null", queue);

        MessageProducer producer = session1.createProducer(queue);
        String messageText = "Hello World!";
        producer.send(session1.createTextMessage(messageText));

        session1.close();

        connection.start();
        MessageConsumer consumer = session2.createConsumer(queue);
        Message message = consumer.receive(getReceiveTimeout());
        assertTrue("Received message not a text message", message instanceof TextMessage);
        assertEquals("Incorrect message text", messageText, ((TextMessage) message).getText());
    }
    finally
    {
        connection.close();
    }
}
 
Example 13
Source File: AmqpManagementFacade.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
public Map<String, Object> createEntityAndAssertResponse(final String name,
                                        final String type,
                                        final Map<String, Object> attributes,
                                        final Session session)
        throws JMSException
{
    Destination replyToDestination;
    Destination replyConsumerDestination;
    if (_protocol == Protocol.AMQP_1_0)
    {
        replyToDestination = session.createTemporaryQueue();
        replyConsumerDestination = replyToDestination;
    }
    else
    {
        replyToDestination = session.createQueue(AMQP_0_X_REPLY_TO_DESTINATION);
        replyConsumerDestination = session.createQueue(AMQP_0_X_CONSUMER_REPLY_DESTINATION);
    }

    MessageConsumer consumer = session.createConsumer(replyConsumerDestination);

    MessageProducer producer = session.createProducer(session.createQueue(_managementAddress));

    MapMessage createMessage = session.createMapMessage();
    createMessage.setStringProperty("type", type);
    createMessage.setStringProperty("operation", "CREATE");
    createMessage.setString("name", name);
    createMessage.setString("object-path", name);
    createMessage.setJMSReplyTo(replyToDestination);
    for (Map.Entry<String, Object> entry : attributes.entrySet())
    {
        createMessage.setObject(entry.getKey(), entry.getValue());
    }
    producer.send(createMessage);
    if (session.getTransacted())
    {
        session.commit();
    }
    producer.close();

    return receiveManagementResponse(consumer, replyToDestination, 201);
}
 
Example 14
Source File: JMSReconnectTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testNoReconnectCloseAfterFailToReconnectWithTempQueue() throws Exception {
   ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));

   jbcf.setReconnectAttempts(0);

   Connection conn = jbcf.createConnection();

   Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   sess.createTemporaryQueue();

   Thread.sleep(2000);

   this.server.stop();

   this.server.start();

   sess.close();

   conn.close();
}
 
Example 15
Source File: TwoBrokerTempQueueAdvisoryTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public void testSendToRemovedTemp() throws Exception {

      ActiveMQQueue requestReplyDest = new ActiveMQQueue("RequestReply");

      NetworkConnector nc = bridgeBrokers("BrokerA", "BrokerB");
      if (useDuplex) {
         nc.setDuplex(true);
      } else {
         bridgeBrokers("BrokerB", "BrokerA");
      }

      // destination advisory can loose the race with message dispatch, so we need to allow replies on network broker
      // to work in the absence of an advisory, the destination will be cleaned up in the normal
      // way
      if (!useDuplex) {
         brokers.get("BrokerB").broker.setAllowTempAutoCreationOnSend(true);
      }

      TransportConnector forClient = brokers.get("BrokerA").broker.addConnector("tcp://localhost:0");
      startAllBrokers();
      waitForBridgeFormation();
      waitForMinTopicRegionConsumerCount("BrokerB", 1);
      waitForMinTopicRegionConsumerCount("BrokerA", 1);

      ConnectionFactory factory = new ActiveMQConnectionFactory(forClient.getConnectUri());
      ActiveMQConnection conn = (ActiveMQConnection) factory.createConnection();
      conn.setWatchTopicAdvisories(false);
      conn.start();
      Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      ConnectionFactory replyFactory = getConnectionFactory("BrokerB");
      for (int i = 0; i < 500; i++) {
         TemporaryQueue tempDest = session.createTemporaryQueue();
         MessageProducer producer = session.createProducer(requestReplyDest);
         javax.jms.Message message = session.createTextMessage("req-" + i);
         message.setJMSReplyTo(tempDest);

         ActiveMQMessageConsumer consumer = (ActiveMQMessageConsumer) session.createConsumer(tempDest);
         producer.send(message);

         ActiveMQConnection replyConnection = (ActiveMQConnection) replyFactory.createConnection();
         replyConnection.setWatchTopicAdvisories(false);
         replyConnection.start();
         Session replySession = replyConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         ActiveMQMessageConsumer replyConsumer = (ActiveMQMessageConsumer) replySession.createConsumer(requestReplyDest);
         javax.jms.Message msg = replyConsumer.receive(10000);
         assertNotNull("request message not null: " + i, msg);
         MessageProducer replyProducer = replySession.createProducer(msg.getJMSReplyTo());
         replyProducer.send(session.createTextMessage("reply-" + i));
         replyConnection.close();

         javax.jms.Message reply = consumer.receive(10000);
         assertNotNull("reply message : " + i + ", to: " + tempDest + ", by consumer:" + consumer.getConsumerId(), reply);
         consumer.close();
         tempDest.delete();
      }
   }
 
Example 16
Source File: MessagingACLTest.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Test
public void testRequestResponseSuccess() throws Exception
{
    String queueName = getTestName();
    Queue queue = createQueue(queueName);
    String groupName = "messaging-users";
    createGroupProvider(groupName, USER1, USER2);

    configureACL(String.format("ACL ALLOW-LOG %s ACCESS VIRTUALHOST", groupName),
                 String.format("ACL ALLOW-LOG %s CONSUME QUEUE name=\"%s\"", USER1, queueName),
                 String.format("ACL ALLOW-LOG %s CONSUME QUEUE temporary=true", USER2),
                 String.format("ACL ALLOW-LOG %s CREATE QUEUE temporary=true", USER2),
                 isLegacyClient() ?
                         String.format("ACL ALLOW-LOG %s BIND EXCHANGE name=\"amq.direct\" temporary=true", USER2) :
                         String.format("ACL ALLOW-LOG %s PUBLISH EXCHANGE name=\"\" routingKey=\"TempQueue*\"", USER1),
                 isLegacyClient() ?
                         String.format("ACL ALLOW-LOG %s PUBLISH EXCHANGE name=\"amq.direct\" routingKey=\"%s\"", USER2, queueName) :
                         String.format("ACL ALLOW-LOG %s PUBLISH EXCHANGE name=\"\" routingKey=\"%s\"", USER2, queueName),
                 isLegacyClient() ? String.format("ACL ALLOW-LOG %s CREATE QUEUE name=\"%s\"", USER1, queueName) : "",
                 isLegacyClient() ? String.format("ACL ALLOW-LOG %s BIND EXCHANGE", USER1) : "",
                 isLegacyClient() ? String.format("ACL ALLOW-LOG %s PUBLISH EXCHANGE name=\"amq.direct\" routingKey=\"TempQueue*\"", USER1) : ""
                 );

    Connection responderConnection = getConnectionBuilder().setUsername(USER1).setPassword(USER1_PASSWORD).build();
    try
    {
        Session responderSession = responderConnection.createSession(true, Session.SESSION_TRANSACTED);
        MessageConsumer requestConsumer = responderSession.createConsumer(queue);
        responderConnection.start();

        Connection requesterConnection = getConnectionBuilder().setUsername(USER2).setPassword(USER2_PASSWORD).build();
        try
        {
            Session requesterSession = requesterConnection.createSession(true, Session.SESSION_TRANSACTED);
            Queue responseQueue = requesterSession.createTemporaryQueue();
            MessageConsumer responseConsumer = requesterSession.createConsumer(responseQueue);
            requesterConnection.start();

            Message request = requesterSession.createTextMessage("Request");
            request.setJMSReplyTo(responseQueue);

            requesterSession.createProducer(queue).send(request);
            requesterSession.commit();

            Message receivedRequest = requestConsumer.receive(getReceiveTimeout());
            assertNotNull("Request is not received", receivedRequest);
            assertNotNull("Request should have Reply-To", receivedRequest.getJMSReplyTo());

            MessageProducer responder = responderSession.createProducer(receivedRequest.getJMSReplyTo());
            responder.send(responderSession.createTextMessage("Response"));
            responderSession.commit();

            Message receivedResponse = responseConsumer.receive(getReceiveTimeout());
            requesterSession.commit();
            assertNotNull("Response is not received", receivedResponse);
            assertEquals("Unexpected response is received", "Response", ((TextMessage) receivedResponse).getText());
        }
        finally
        {
             requesterConnection.close();
        }
    }
    finally
    {
        responderConnection.close();
    }

}
 
Example 17
Source File: JMSConfiguration.java    From cxf with Apache License 2.0 4 votes vote down vote up
private Destination getReplyDestinationInternal(Session session) throws JMSException {
    return replyDestination == null
        ? session.createTemporaryQueue()
        : destinationResolver.resolveDestinationName(session, replyDestination, replyPubSubDomain);
}
 
Example 18
Source File: TemporaryDestinationTest.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
@Test
public void testTemporaryQueueBasic() throws Exception {
   Connection conn = null;

   try {
      conn = createConnection();

      Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      Session consumerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      TemporaryQueue tempQueue = producerSession.createTemporaryQueue();

      MessageProducer producer = producerSession.createProducer(tempQueue);

      MessageConsumer consumer = consumerSession.createConsumer(tempQueue);

      conn.start();

      final String messageText = "This is a message";

      Message m = producerSession.createTextMessage(messageText);

      producer.send(m);

      TextMessage m2 = (TextMessage) consumer.receive(2000);

      ProxyAssertSupport.assertNotNull(m2);

      ProxyAssertSupport.assertEquals(messageText, m2.getText());
   } finally {
      if (conn != null) {
         conn.close();
      }
   }
}
 
Example 19
Source File: TemporaryDestinationTest.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
@Test
public void testTemporaryResourcesDeletedAfterServerRestart() throws Exception {
   server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setAutoCreateAddresses(false).setAutoCreateQueues(false));

   Connection conn = null;

   try {
      conn = createConnection();

      Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      TemporaryQueue tempQueue = producerSession.createTemporaryQueue();

      assertNotNull(server.getAddressInfo(SimpleString.toSimpleString(tempQueue.getQueueName())));

      server.stop();

      conn.close();

      server.start();

      waitForServerToStart(server);

      assertNull(server.getAddressInfo(SimpleString.toSimpleString(tempQueue.getQueueName())));
   } finally {
      if (conn != null) {
         conn.close();
      }
   }
}
 
Example 20
Source File: TemporaryDestinationTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
@Test
public void testTemporaryQueueDeletedAfterSessionClosed() throws Exception {
   server.getAddressSettingsRepository().addMatch("#", new AddressSettings().setAutoCreateAddresses(false).setAutoCreateQueues(false));

   Connection conn = null;

   try {
      conn = createConnection();

      Session producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      Session consumerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Make sure temporary queue cannot be used after it has been deleted

      TemporaryQueue tempQueue = producerSession.createTemporaryQueue();

      MessageProducer producer = producerSession.createProducer(tempQueue);

      MessageConsumer consumer = consumerSession.createConsumer(tempQueue);

      conn.start();

      final String messageText = "This is a message";

      javax.jms.Message m = producerSession.createTextMessage(messageText);

      producer.send(m);

      TextMessage m2 = (TextMessage) consumer.receive(2000);

      assertNotNull(m2);

      assertEquals(messageText, m2.getText());

      consumer.close();

      consumerSession.close();

      producer.close();

      producerSession.close();

      tempQueue.delete();

      producerSession = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

      try {
         producer = producerSession.createProducer(tempQueue);
         producer.send(m);
         fail();
      } catch (JMSException e) {
      }
   } finally {
      if (conn != null) {
         conn.close();
      }
   }
}