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

The following examples show how to use javax.jms.Session#createMessage() . 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: MessageProducerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoDefaultDestination() throws JMSException {
   Session session = conn.createSession();
   try {
      MessageProducer producer = session.createProducer(null);
      Message m = session.createMessage();
      try {
         producer.send(m);
         Assert.fail("must not be reached");
      } catch (UnsupportedOperationException cause) {
         // expected
      }
   } finally {
      session.close();
   }
}
 
Example 2
Source File: SortedQueueTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
private void sendMessages(final Queue queue) throws JMSException, NamingException
{
    final Connection producerConnection = getConnection();
    try
    {
        final Session producerSession = producerConnection.createSession(true, Session.SESSION_TRANSACTED);
        final MessageProducer producer = producerSession.createProducer(queue);

        for (String value : VALUES)
        {
            final Message msg = producerSession.createMessage();
            msg.setStringProperty(TEST_SORT_KEY, value);
            producer.send(msg);
        }

        producerSession.commit();
        producer.close();
    }
    finally
    {
        producerConnection.close();
    }
}
 
Example 3
Source File: JobSchedulerManagementTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveNotScheduled() throws Exception {
   Connection connection = createConnection();

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

   // Create the Browse Destination and the Reply To location
   Destination management = session.createTopic(ScheduledMessage.AMQ_SCHEDULER_MANAGEMENT_DESTINATION);

   MessageProducer producer = session.createProducer(management);

   try {

      // Send the remove request
      Message remove = session.createMessage();
      remove.setStringProperty(ScheduledMessage.AMQ_SCHEDULER_ACTION, ScheduledMessage.AMQ_SCHEDULER_ACTION_REMOVEALL);
      remove.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_ID, new IdGenerator().generateId());
      producer.send(remove);
   } catch (Exception e) {
      fail("Caught unexpected exception during remove of unscheduled message.");
   }
}
 
Example 4
Source File: GeneralInteropTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void sendMessageUsingCoreJms(String queueName) throws Exception {
   Connection jmsConn = null;
   try {
      jmsConn = coreCf.createConnection();
      Session session = jmsConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      javax.jms.Message message = session.createMessage();

      message.setBooleanProperty("booleanProperty", false);
      message.setLongProperty("longProperty", 99999L);
      message.setByteProperty("byteProperty", (byte) 5);
      message.setIntProperty("intProperty", 979);
      message.setShortProperty("shortProperty", (short) 1099);
      message.setStringProperty("stringProperty", "HelloMessage");

      Queue queue = session.createQueue(queueName);
      MessageProducer producer = session.createProducer(queue);

      producer.send(message);
   } finally {
      if (jmsConn != null) {
         jmsConn.close();
      }
   }

}
 
Example 5
Source File: GeneralInteropTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void sendMessageUsingOpenWire(String queueName) throws Exception {
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);

   final ActiveMQMessageProducer producer = (ActiveMQMessageProducer) session.createProducer(destination);

   javax.jms.Message message = session.createMessage();

   message.setBooleanProperty("booleanProperty", false);
   message.setLongProperty("longProperty", 99999L);
   message.setByteProperty("byteProperty", (byte) 5);
   message.setIntProperty("intProperty", 979);
   message.setShortProperty("shortProperty", (short) 1099);
   message.setStringProperty("stringProperty", "HelloMessage");

   producer.send(message);
}
 
Example 6
Source File: JMSMessageTypesTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void testMessageSendReceive(Connection producerConnection, Connection consumerConnection) throws Throwable {
   long time = System.currentTimeMillis();

   Session session = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue queue = session.createQueue(getQueueName());

   byte[] bytes = new byte[0xf + 1];
   for (int i = 0; i <= 0xf; i++) {
      bytes[i] = (byte) i;
   }

   MessageProducer producer = session.createProducer(queue);
   for (int i = 0; i < NUM_MESSAGES; i++) {
      instanceLog.debug("Sending " + i);
      Message message = session.createMessage();

      message.setIntProperty("count", i);
      producer.send(message);
   }

   Session sessionConsumer = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   Queue consumerQueue = sessionConsumer.createQueue(getQueueName());
   final MessageConsumer consumer = sessionConsumer.createConsumer(consumerQueue);

   for (int i = 0; i < NUM_MESSAGES; i++) {
      Message m = consumer.receive(5000);
      Assert.assertNotNull("Could not receive message count=" + i + " on consumer", m);
   }

   long taken = (System.currentTimeMillis() - time) / 1000;
   instanceLog.debug("taken = " + taken);
}
 
Example 7
Source File: SelectorTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void runtimeSelectorError() throws Exception
{
    Connection connection = getConnection();
    Queue queue = createQueue(getTestName());
    try
    {
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(queue , "testproperty % 5 = 1");
        MessageProducer producer = session.createProducer(queue);

        Message message = session.createMessage();
        message.setIntProperty("testproperty", 1); // 1 % 5
        producer.send(message);

        connection.start();

        Message receivedMessage = consumer.receive(getReceiveTimeout());
        assertNotNull("Message matching selector should be received", receivedMessage);

        message.setStringProperty("testproperty", "hello"); // "hello" % 5 would cause a runtime error
        producer.send(message);
        receivedMessage = consumer.receive(getReceiveTimeout());
        assertNull("Message causing runtime selector error should not be received", receivedMessage);

        MessageConsumer consumerWithoutSelector = session.createConsumer(queue);
        receivedMessage = consumerWithoutSelector.receive(getReceiveTimeout());
        assertNotNull("Message that previously caused a runtime error should be consumable by another consumer", receivedMessage);
    }
    finally
    {
        connection.close();
    }
}
 
Example 8
Source File: RejectValidatedUserTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testAcceptJMSException() throws Exception {
   ActiveMQConnectionFactory connectionFactory = ActiveMQJMSClient.createConnectionFactory("vm://0", "0");
   Connection connection = connectionFactory.createConnection();
   Session session = connection.createSession();
   Queue queue = session.createQueue(ADDRESS.toString());
   MessageProducer producer = session.createProducer(queue);
   Message message = session.createMessage();
   message.setStringProperty("JMSXUserID", "testuser");
   producer.send(message);
}
 
Example 9
Source File: JmsAnonymousProducerTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
public void doTestAnonymousProducerSendToMultipleDests(int numDestinations, int numIterations) throws Exception {
    connection = createAmqpConnection();
    assertNotNull(connection);
    connection.start();

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

    List<Queue> queues = new ArrayList<Queue>(numDestinations);
    for (int i = 0; i < numDestinations; ++i) {
        queues.add(session.createQueue(name.getMethodName() + i));
    }

    assertNotNull(session);
    MessageProducer producer = session.createProducer(null);

    for (int iteration = 1; iteration <= numIterations; ++iteration) {
        Message message = session.createMessage();
        for (int i = 0; i < numDestinations; ++i) {
            producer.send(queues.get(i), message);
        }

        for (int i = 0; i < numDestinations; ++i) {
            QueueViewMBean proxy = getProxyToQueue(queues.get(i).getQueueName());
            assertEquals(iteration, proxy.getQueueSize());
        }
    }
}
 
Example 10
Source File: SessionIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testCreateAnonymousProducerWhenAnonymousRelayNodeIsNotSupported() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {

        // DO NOT add capability to indicate server support for ANONYMOUS-RELAY

        // Configure for a known state such that no fallback producers are cached.
        Connection connection = testFixture.establishConnecton(testPeer,
            "?amqp.anonymousFallbackCacheSize=0&amqp.anonymousFallbackCacheTimeout=0");

        connection.start();

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

        String topicName = "myTopic";
        Topic dest = session.createTopic(topicName);

        // Expect no AMQP traffic when we create the anonymous producer, as it will wait
        // for an actual send to occur on the producer before anything occurs on the wire

        // Create an anonymous producer
        MessageProducer producer = session.createProducer(null);
        assertNotNull("Producer object was null", producer);

        // Expect a new message sent by the above producer to cause creation of a new
        // sender link to the given destination, then closing the link after the message is sent.
        TargetMatcher targetMatcher = new TargetMatcher();
        targetMatcher.withAddress(equalTo(topicName));
        targetMatcher.withDynamic(equalTo(false));
        targetMatcher.withDurable(equalTo(TerminusDurability.NONE));

        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true);
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);

        testPeer.expectSenderAttach(targetMatcher, false, false);
        testPeer.expectTransfer(messageMatcher);
        testPeer.expectDetach(true, true, true);

        Message message = session.createMessage();
        producer.send(dest, message);

        // Repeat the send and observe another attach->transfer->detach.
        testPeer.expectSenderAttach(targetMatcher, false, false);
        testPeer.expectTransfer(messageMatcher);
        testPeer.expectDetach(true, true, true);

        producer.send(dest, message);
        producer.close();

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

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 11
Source File: AnonymousFallbackProducerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Repeat(repetitions = 1)
@Test(timeout = 20000)
public void testFailureOfOneCacheProducerCloseOnPropagatedToMainProducerClose() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {

        final int CACHE_SIZE = 3;

        JmsConnection connection = (JmsConnection) testFixture.establishConnecton(testPeer,
            "?amqp.anonymousFallbackCacheSize=" + CACHE_SIZE + "&amqp.anonymousFallbackCacheTimeout=0");

        connection.start();

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

        String topicName = "myTopic";

        // Expect no AMQP traffic when we create the anonymous producer, as it will wait
        // for an actual send to occur on the producer before anything occurs on the wire

        // Create an anonymous producer
        MessageProducer producer = session.createProducer(null);
        assertNotNull("Producer object was null", producer);

        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true);
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);

        // First round of sends should open and cache sender links
        for (int i = 1; i <= CACHE_SIZE; ++i) {
            Topic dest = session.createTopic(topicName + i);

            // Expect a new message sent by the above producer to cause creation of a new
            // sender link to the given destination.
            TargetMatcher targetMatcher = new TargetMatcher();
            targetMatcher.withAddress(equalTo(dest.getTopicName()));
            targetMatcher.withDynamic(equalTo(false));
            targetMatcher.withDurable(equalTo(TerminusDurability.NONE));

            Message message = session.createMessage();

            testPeer.expectSenderAttach(targetMatcher, false, false);
            testPeer.expectTransfer(messageMatcher);

            producer.send(dest, message);
        }

        // The current cached senders should all close when the producer is closed.
        for (int i = 1; i < CACHE_SIZE; ++i) {
            testPeer.expectDetach(true, true, true);
        }

        // Last one carries error but since we asked for close it should be ignored as we got what we wanted.
        testPeer.expectDetach(true, true, true, AmqpError.RESOURCE_LOCKED, "Some error on detach");

        try {
            producer.close();
        } catch (JMSException ex) {
            LOG.trace("Caught unexpected error: ", ex);
            fail("Should not have thrown an error as close was requested so errors are ignored.");
        }

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

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 12
Source File: MessageConsumerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testReceiveOnTopicConnectionStopped() throws Exception {
   Connection producerConnection = null;

   Connection consumerConnection = null;

   try {
      producerConnection = createConnection();

      consumerConnection = createConnection();

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

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

      final MessageProducer topicProducer = producerSession.createProducer(ActiveMQServerTestCase.topic1);

      MessageConsumer topicConsumer = consumerSession.createConsumer(ActiveMQServerTestCase.topic1);

      final Message m = producerSession.createMessage();
      new Thread(new Runnable() {
         @Override
         public void run() {
            try {
               // this is needed to make sure the main thread has enough time to block
               Thread.sleep(1000);
               topicProducer.send(m);
            } catch (Exception e) {
               log.error(e);
            }
         }
      }, "Producer").start();

      ProxyAssertSupport.assertNull(topicConsumer.receiveNoWait());
   } finally {
      if (producerConnection != null) {
         producerConnection.close();
      }
      if (consumerConnection != null) {
         consumerConnection.close();
      }
   }
}
 
Example 13
Source File: TimeStampTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testTimestamp() throws Exception {
   // Create a Session
   connection.start();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   // Create the destination Queue
   Destination destination = session.createQueue("TEST.FOO");
   this.makeSureCoreQueueExist("TEST.FOO");

   // Create a MessageProducer from the Session to the Topic or Queue
   MessageProducer producer = session.createProducer(destination);
   producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

   // Create a messages
   Message sentMessage = session.createMessage();

   // Tell the producer to send the message
   long beforeSend = System.currentTimeMillis();
   producer.send(sentMessage);
   long afterSend = System.currentTimeMillis();

   // assert message timestamp is in window
   assertTrue(beforeSend <= sentMessage.getJMSTimestamp() && sentMessage.getJMSTimestamp() <= afterSend);

   // Create a MessageConsumer from the Session to the Topic or Queue
   MessageConsumer consumer = session.createConsumer(destination);

   // Wait for a message
   Message receivedMessage = consumer.receive(1000);

   // assert we got the same message ID we sent
   assertEquals(sentMessage.getJMSMessageID(), receivedMessage.getJMSMessageID());

   // assert message timestamp is in window
   assertTrue("JMS Message Timestamp should be set during the send method: \n" + "        beforeSend = " + beforeSend + "\n" + "   getJMSTimestamp = " + receivedMessage.getJMSTimestamp() + "\n" + "         afterSend = " + afterSend + "\n", beforeSend <= receivedMessage.getJMSTimestamp() && receivedMessage.getJMSTimestamp() <= afterSend);

   // assert message timestamp is unchanged
   assertEquals("JMS Message Timestamp of received message should be the same as the sent message\n        ", sentMessage.getJMSTimestamp(), receivedMessage.getJMSTimestamp());

   // Clean up
   producer.close();
   consumer.close();
   session.close();
   connection.close();
}
 
Example 14
Source File: JMSHeadersAndPropertiesTest.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
@Test
public void headers() throws Exception
{
    final Queue queue = createQueue(getTestName());
    final Destination replyTo = createQueue(getTestName() + "_replyTo");
    final Connection consumerConnection = getConnection();
    try
    {
        final Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageConsumer consumer = consumerSession.createConsumer(queue);

        final String correlationId = "testCorrelationId";
        final String jmsType = "testJmsType";

        final int priority = 1;
        final long timeToLive = 30 * 60 * 1000;
        final Connection producerConnection = getConnection();
        try
        {
            final Session producerSession = producerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            final MessageProducer producer = producerSession.createProducer(queue);

            final Message message = producerSession.createMessage();
            message.setJMSCorrelationID(correlationId);
            message.setJMSType(jmsType);
            message.setJMSReplyTo(replyTo);

            long currentTime = System.currentTimeMillis();
            producer.send(message, DeliveryMode.NON_PERSISTENT, priority, timeToLive);

            consumerConnection.start();

            Message receivedMessage = consumer.receive(getReceiveTimeout());
            assertNotNull(receivedMessage);

            assertEquals("JMSCorrelationID mismatch", correlationId, receivedMessage.getJMSCorrelationID());
            assertEquals("JMSType mismatch", message.getJMSType(), receivedMessage.getJMSType());
            assertEquals("JMSReply To mismatch", message.getJMSReplyTo(), receivedMessage.getJMSReplyTo());
            assertTrue("JMSMessageID does not start 'ID:'", receivedMessage.getJMSMessageID().startsWith("ID:"));
            assertEquals("JMSPriority mismatch", priority, receivedMessage.getJMSPriority());
            assertTrue(String.format(
                    "Unexpected JMSExpiration: got '%d', but expected value equals or greater than '%d'",
                    receivedMessage.getJMSExpiration(),
                    currentTime + timeToLive),

                       receivedMessage.getJMSExpiration() >= currentTime + timeToLive
                       && receivedMessage.getJMSExpiration() <= System.currentTimeMillis() + timeToLive);
        }
        finally
        {
            producerConnection.close();
        }
    }
    finally
    {
        consumerConnection.close();
    }
}
 
Example 15
Source File: SelectorTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * Test case for http://jira.jboss.org/jira/browse/JBMESSAGING-105
 * <br>
 * Two Messages are sent to a queue. There is one receiver on the queue. The receiver only
 * receives one of the messages due to a message selector only matching one of them. The receiver
 * is then closed. A new receiver is now attached to the queue. Redelivery of the remaining
 * message is now attempted. The message should be redelivered.
 */
@Test
public void testSelectiveClosingConsumer() throws Exception {
   Connection conn = null;

   try {
      conn = getConnectionFactory().createConnection();
      conn.start();

      Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer prod = session.createProducer(queue1);
      prod.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

      String selector = "color = 'red'";
      MessageConsumer redConsumer = session.createConsumer(queue1, selector);
      conn.start();

      Message redMessage = session.createMessage();
      redMessage.setStringProperty("color", "red");

      Message blueMessage = session.createMessage();
      blueMessage.setStringProperty("color", "blue");

      prod.send(redMessage);
      prod.send(blueMessage);

      log.debug("sent message");

      Message rec = redConsumer.receive();
      ProxyAssertSupport.assertEquals(redMessage.getJMSMessageID(), rec.getJMSMessageID());
      ProxyAssertSupport.assertEquals("red", rec.getStringProperty("color"));

      ProxyAssertSupport.assertNull(redConsumer.receiveNoWait());

      redConsumer.close();

      log.debug("closed first consumer");

      MessageConsumer universalConsumer = session.createConsumer(queue1);

      rec = universalConsumer.receive(2000);

      ProxyAssertSupport.assertEquals(rec.getJMSMessageID(), blueMessage.getJMSMessageID());
      ProxyAssertSupport.assertEquals("blue", rec.getStringProperty("color"));
   } finally {
      if (conn != null) {
         conn.close();
      }
   }
}
 
Example 16
Source File: SelectorTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testWithSelector() throws Exception {
   String selector1 = "beatle = 'john'";
   String selector2 = "beatle = 'paul'";
   String selector3 = "beatle = 'george'";
   String selector4 = "beatle = 'ringo'";
   String selector5 = "beatle = 'jesus'";

   Connection conn = null;

   try {
      conn = getConnectionFactory().createConnection();
      conn.start();
      Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageConsumer cons1 = sess.createConsumer(ActiveMQServerTestCase.topic1, selector1);
      MessageConsumer cons2 = sess.createConsumer(ActiveMQServerTestCase.topic1, selector2);
      MessageConsumer cons3 = sess.createConsumer(ActiveMQServerTestCase.topic1, selector3);
      MessageConsumer cons4 = sess.createConsumer(ActiveMQServerTestCase.topic1, selector4);
      MessageConsumer cons5 = sess.createConsumer(ActiveMQServerTestCase.topic1, selector5);

      Message m1 = sess.createMessage();
      m1.setStringProperty("beatle", "john");

      Message m2 = sess.createMessage();
      m2.setStringProperty("beatle", "paul");

      Message m3 = sess.createMessage();
      m3.setStringProperty("beatle", "george");

      Message m4 = sess.createMessage();
      m4.setStringProperty("beatle", "ringo");

      Message m5 = sess.createMessage();
      m5.setStringProperty("beatle", "jesus");

      MessageProducer prod = sess.createProducer(ActiveMQServerTestCase.topic1);

      prod.send(m1);
      prod.send(m2);
      prod.send(m3);
      prod.send(m4);
      prod.send(m5);

      Message r1 = cons1.receive(500);
      ProxyAssertSupport.assertNotNull(r1);
      Message n = cons1.receiveNoWait();
      ProxyAssertSupport.assertNull(n);

      Message r2 = cons2.receive(500);
      ProxyAssertSupport.assertNotNull(r2);
      n = cons2.receiveNoWait();
      ProxyAssertSupport.assertNull(n);

      Message r3 = cons3.receive(500);
      ProxyAssertSupport.assertNotNull(r3);
      n = cons3.receiveNoWait();
      ProxyAssertSupport.assertNull(n);

      Message r4 = cons4.receive(500);
      ProxyAssertSupport.assertNotNull(r4);
      n = cons4.receiveNoWait();
      ProxyAssertSupport.assertNull(n);

      Message r5 = cons5.receive(500);
      ProxyAssertSupport.assertNotNull(r5);
      n = cons5.receiveNoWait();
      ProxyAssertSupport.assertNull(n);

      ProxyAssertSupport.assertEquals("john", r1.getStringProperty("beatle"));
      ProxyAssertSupport.assertEquals("paul", r2.getStringProperty("beatle"));
      ProxyAssertSupport.assertEquals("george", r3.getStringProperty("beatle"));
      ProxyAssertSupport.assertEquals("ringo", r4.getStringProperty("beatle"));
      ProxyAssertSupport.assertEquals("jesus", r5.getStringProperty("beatle"));
   } finally {
      if (conn != null) {
         conn.close();
      }
   }
}
 
Example 17
Source File: AnonymousFallbackProducerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Repeat(repetitions = 1)
@Test(timeout = 20000)
public void testFallbackProducerRecoversFromRefusalOfSenderOpenOnNextSend() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {

        JmsConnection connection = (JmsConnection) testFixture.establishConnecton(testPeer,
            "?amqp.anonymousFallbackCacheSize=1&amqp.anonymousFallbackCacheTimeout=0");

        connection.start();

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

        String topicName = "myTopic";

        // Expect no AMQP traffic when we create the anonymous producer, as it will wait
        // for an actual send to occur on the producer before anything occurs on the wire

        // Create an anonymous producer
        MessageProducer producer = session.createProducer(null);
        assertNotNull("Producer object was null", producer);

        // Expect a new message sent by the above producer to cause creation of a new
        // sender link to the given destination.
        TargetMatcher targetMatcher = new TargetMatcher();
        targetMatcher.withAddress(equalTo(topicName));
        targetMatcher.withDynamic(equalTo(false));
        targetMatcher.withDurable(equalTo(TerminusDurability.NONE));

        Topic dest = session.createTopic(topicName);

        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true);
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);

        Message message = session.createMessage();

        // Refuse the attach which should result in fallback producer detaching the refused
        // link attach and the send should then fail.
        testPeer.expectSenderAttach(targetMatcher, true, false);
        testPeer.expectDetach(true, false, false);

        try {
            producer.send(dest, message);
            fail("Send should have failed because sender link was refused.");
        } catch (JMSException ex) {
            LOG.trace("Caught expected exception: ", ex);
        }

        testPeer.expectSenderAttach(targetMatcher, false, false);
        testPeer.expectTransfer(messageMatcher);
        testPeer.expectDetach(true, true, true);

        producer.send(dest, message);
        producer.close();

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

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 18
Source File: SessionIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private void doCreateAnonymousProducerTargetContainsCapabilityWhenAnonymousRelayNodeIsNotSupportedTestImpl(Class<? extends Destination> destType) throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {

        //DO NOT add capability to indicate server support for ANONYMOUS-RELAY

        Connection connection = testFixture.establishConnecton(testPeer);
        connection.start();

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

        String destName = "myDest";
        Symbol nodeTypeCapability = null;

        Destination dest = null;
        if (destType == Queue.class) {
            dest = session.createQueue(destName);
            nodeTypeCapability = AmqpDestinationHelper.QUEUE_CAPABILITY;
        } else if (destType == Topic.class) {
            dest = session.createTopic(destName);
            nodeTypeCapability = AmqpDestinationHelper.TOPIC_CAPABILITY;
        } else if (destType == TemporaryQueue.class) {
            testPeer.expectTempQueueCreationAttach(destName);
            dest = session.createTemporaryQueue();
            nodeTypeCapability = AmqpDestinationHelper.TEMP_QUEUE_CAPABILITY;
        } else if (destType == TemporaryTopic.class) {
            testPeer.expectTempTopicCreationAttach(destName);
            dest = session.createTemporaryTopic();
            nodeTypeCapability = AmqpDestinationHelper.TEMP_TOPIC_CAPABILITY;
        } else {
            fail("unexpected type");
        }

        // Expect no AMQP traffic when we create the anonymous producer, as it will wait
        // for an actual send to occur on the producer before anything occurs on the wire

        //Create an anonymous producer
        MessageProducer producer = session.createProducer(null);
        assertNotNull("Producer object was null", producer);

        //Expect a new message sent by the above producer to cause creation of a new
        //sender link to the given destination, then closing the link after the message is sent.
        TargetMatcher targetMatcher = new TargetMatcher();
        targetMatcher.withAddress(equalTo(destName));
        targetMatcher.withDynamic(equalTo(false));
        targetMatcher.withDurable(equalTo(TerminusDurability.NONE));
        targetMatcher.withCapabilities(arrayContaining(nodeTypeCapability));

        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true);
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);

        testPeer.expectSenderAttach(targetMatcher, false, false);
        testPeer.expectTransfer(messageMatcher);
        testPeer.expectDetach(true, true, true);
        testPeer.expectClose();

        Message message = session.createMessage();
        producer.send(dest, message);
        producer.close();

        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 19
Source File: MiscellaneousTest.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
@Test
public void testGetDeliveriesForSession() throws Exception {
   Connection conn = null;

   try {
      conn = createConnection();

      Session session1 = conn.createSession(true, Session.SESSION_TRANSACTED);

      Session session2 = conn.createSession(true, Session.SESSION_TRANSACTED);

      MessageProducer prod = session2.createProducer(queue1);

      Message msg = session2.createMessage();

      prod.send(msg);

      session1.close();

      session2.commit();
   } finally {
      if (conn != null) {
         conn.close();
      }

      removeAllMessages(queue1.getQueueName(), true);
   }
}
 
Example 20
Source File: MessageConsumerTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
@Test
public void testSendMessageAndCloseConsumer1() throws Exception {
   Connection producerConnection = null;

   Connection consumerConnection = null;

   try {
      producerConnection = createConnection();

      consumerConnection = createConnection();

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

      Session consumerSession = consumerConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

      MessageProducer queueProducer = producerSession.createProducer(queue1);

      MessageConsumer queueConsumer = consumerSession.createConsumer(queue1);

      Message m = producerSession.createMessage();

      queueProducer.send(m);

      queueConsumer.close();

      // since no message was received, we expect the message back in the queue

      queueConsumer = consumerSession.createConsumer(queue1);

      consumerConnection.start();

      Message r = queueConsumer.receive(2000);

      ProxyAssertSupport.assertEquals(m.getJMSMessageID(), r.getJMSMessageID());
   } finally {
      if (producerConnection != null) {
         producerConnection.close();
      }
      if (consumerConnection != null) {
         consumerConnection.close();
      }
   }

   removeAllMessages(queue1.getQueueName(), true);
}