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

The following examples show how to use javax.jms.Session#createDurableConsumer() . 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: AutoCreateJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCreateOnDurableSubscribeToTopic() throws Exception {
   Connection connection = cf.createConnection();
   connection.setClientID("myClientID");
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Topic topic = ActiveMQJMSClient.createTopic(QUEUE_NAME);

   MessageConsumer consumer = session.createDurableConsumer(topic, "myDurableSub");
   MessageProducer producer = session.createProducer(topic);
   producer.send(session.createTextMessage("msg"));
   connection.start();
   assertNotNull(consumer.receive(500));

   connection.close();

   assertNotNull(server.getManagementService().getResource(ResourceNames.ADDRESS + "test"));

   assertNotNull(server.locateQueue(SimpleString.toSimpleString("myClientID.myDurableSub")));
}
 
Example 2
Source File: ActiveMQRASession.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
public MessageConsumer createDurableConsumer(final Topic topic, final String name) throws JMSException {
   lock();
   try {
      Session session = getSessionInternal();

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("createSharedConsumer " + session + " topic=" + topic + ", name=" + name);
      }

      MessageConsumer result = session.createDurableConsumer(topic, name);
      result = new ActiveMQRAMessageConsumer(result, this);

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("createdConsumer " + session + " consumer=" + result);
      }

      addConsumer(result);

      return result;
   } finally {
      unlock();
   }
}
 
Example 3
Source File: SessionIntegrationTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testCreateDurableConsumer() 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 topicName = "myTopic";
        Topic dest = session.createTopic(topicName);
        String subscriptionName = "mySubscription";

        testPeer.expectDurableSubscriberAttach(topicName, subscriptionName);
        testPeer.expectLinkFlow();

        MessageConsumer consumer = session.createDurableConsumer(dest, subscriptionName);
        assertNotNull("MessageConsumer object was null", consumer);
        assertNull("MessageConsumer should not have a selector", consumer.getMessageSelector());

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

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 4
Source File: JMSConsumer.java    From nifi with Apache License 2.0 6 votes vote down vote up
private MessageConsumer createMessageConsumer(final Session session, final String destinationName, final boolean durable, final boolean shared, final String subscriberName) throws JMSException {
    final boolean isPubSub = JMSConsumer.this.jmsTemplate.isPubSubDomain();
    final Destination destination = JMSConsumer.this.jmsTemplate.getDestinationResolver().resolveDestinationName(session, destinationName, isPubSub);

    if (isPubSub) {
        if (shared) {
            try {
                if (durable) {
                    return session.createSharedDurableConsumer((Topic) destination, subscriberName);
                } else {
                    return session.createSharedConsumer((Topic) destination, subscriberName);
                }
            } catch (AbstractMethodError e) {
                throw new ProcessException("Failed to create a shared consumer. Make sure the target broker is JMS 2.0 compliant.", e);
            }
        } else {
            if (durable) {
                return session.createDurableConsumer((Topic) destination, subscriberName, null, JMSConsumer.this.jmsTemplate.isPubSubDomain());
            } else {
                return session.createConsumer(destination, null, JMSConsumer.this.jmsTemplate.isPubSubDomain());
            }
        }
    } else {
        return session.createConsumer(destination, null, JMSConsumer.this.jmsTemplate.isPubSubDomain());
    }
}
 
Example 5
Source File: AutoDeleteJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoDeleteTopicDurableSubscriber() throws Exception {
   Connection connection = cf.createConnection();
   connection.setClientID("myClientID");
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Topic topic = ActiveMQJMSClient.createTopic("test");

   MessageConsumer messageConsumer = session.createDurableConsumer(topic, "mySub");
   MessageProducer producer = session.createProducer(topic);

   final int numMessages = 100;

   for (int i = 0; i < numMessages; i++) {
      TextMessage mess = session.createTextMessage("msg" + i);
      producer.send(mess);
   }

   producer.close();
   connection.start();

   for (int i = 0; i < numMessages; i++) {
      Message m = messageConsumer.receive(5000);
      Assert.assertNotNull(m);
   }

   messageConsumer.close();
   session.unsubscribe("mySub");

   connection.close();

   // ensure the topic was removed
   Assert.assertNull(server.locateQueue(new SimpleString("test")));

   // make sure the JMX control was removed for the JMS topic
   assertNull(server.getManagementService().getResource("test"));
}
 
Example 6
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
private void testAddressRemovalWithWithConsumers(String topic1, String topic2) throws Exception {
   server.addAddressInfo(new AddressInfo(SimpleString.toSimpleString(topic1), RoutingType.MULTICAST));
   server.addAddressInfo(new AddressInfo(SimpleString.toSimpleString(topic2), RoutingType.MULTICAST));

   conn = cf.createConnection();
   conn.setClientID("clientId");
   conn.start();
   Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   MessageConsumer c1 = sess.createDurableConsumer(sess.createTopic(topic1), "sub1");
   c1.close();

   // Make sure topic2 address can be removed and the bindings still exist for topic1
   server.removeAddressInfo(SimpleString.toSimpleString(topic2), null);
   assertEquals(1, server.getPostOffice().getBindingsForAddress(SimpleString.toSimpleString(topic1))
         .getBindings().size());

   // Re-create address by creating a consumer on the topic and make sure the
   // wildcard and the direct consumer still receive the messages
   c1 = sess.createDurableConsumer(sess.createTopic(topic1), "sub1");
   MessageConsumer c2 = sess.createDurableConsumer(sess.createTopic(topic2), "sub2");
   MessageProducer p1 = sess.createProducer(sess.createTopic("durable.test"));
   p1.send(sess.createTextMessage("test"));

   assertNotNull(c1.receive(1000));
   assertNotNull(c2.receive(1000));
   sess.close();
}
 
Example 7
Source File: ActiveMQRASession.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public MessageConsumer createDurableConsumer(Topic topic,
                                             String name,
                                             String messageSelector,
                                             boolean noLocal) throws JMSException {
   lock();
   try {
      Session session = getSessionInternal();

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("createDurableConsumer " + session + " topic=" + topic + ", name=" + name +
                                          ", messageSelector=" + messageSelector + ", noLocal=" + noLocal);
      }

      MessageConsumer result = session.createDurableConsumer(topic, name, messageSelector, noLocal);
      result = new ActiveMQRAMessageConsumer(result, this);

      if (ActiveMQRALogger.LOGGER.isTraceEnabled()) {
         ActiveMQRALogger.LOGGER.trace("createdConsumer " + session + " consumer=" + result);
      }

      addConsumer(result);

      return result;
   } finally {
      unlock();
   }
}
 
Example 8
Source File: SessionIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
private void doCloseWithWithUnackedClientAckMessagesTestImpl(boolean closeSession) throws JMSException, Exception, IOException {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer, false, "?jms.clientID=myClientId", null, null, false);
        connection.start();

        testPeer.expectBegin();

        Session session = connection.createSession(Session.CLIENT_ACKNOWLEDGE);

        String subscriptionName = "mySubName";
        String topicName = "myTopic";
        Topic topic = session.createTopic(topicName);

        int msgCount = 2;
        testPeer.expectDurableSubscriberAttach(topicName, subscriptionName);
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, new AmqpValueDescribedType("content"), msgCount, false, false,
                Matchers.greaterThanOrEqualTo(UnsignedInteger.valueOf(msgCount)), 1, false, true);

        MessageConsumer subscriber = session.createDurableConsumer(topic, subscriptionName);

        TextMessage receivedTextMessage = null;
        assertNotNull("Expected a message", receivedTextMessage = (TextMessage) subscriber.receive(3000));
        assertEquals("Unexpected delivery number", 1,  receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1);
        assertNotNull("Expected a message", receivedTextMessage = (TextMessage) subscriber.receive(3000));
        assertEquals("Unexpected delivery number", 2,  receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1);

        testPeer.expectDisposition(true, new ModifiedMatcher().withDeliveryFailed(equalTo(true)), 1, 1);
        testPeer.expectDisposition(true, new ModifiedMatcher().withDeliveryFailed(equalTo(true)), 2, 2);

        if(closeSession) {
            testPeer.expectEnd();

            session.close();
        }

        testPeer.expectClose();

        connection.close();
    }
}
 
Example 9
Source File: SessionIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testRecoveredClientAckSessionWithDurableSubscriber() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer, false, "?jms.clientID=myClientId", null, null, false);
        connection.start();

        testPeer.expectBegin();

        Session session = connection.createSession(Session.CLIENT_ACKNOWLEDGE);

        String subscriptionName = "mySubName";
        String topicName = "myTopic";
        Topic topic = session.createTopic(topicName);

        int msgCount = 3;
        testPeer.expectDurableSubscriberAttach(topicName, subscriptionName);
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, new AmqpValueDescribedType("content"), msgCount, false, false,
                Matchers.greaterThanOrEqualTo(UnsignedInteger.valueOf(msgCount)), 1, false, true);

        MessageConsumer subscriber = session.createDurableConsumer(topic, subscriptionName);

        TextMessage receivedTextMessage = null;
        assertNotNull("Expected a message", receivedTextMessage = (TextMessage) subscriber.receive(3000));
        assertEquals("Unexpected delivery number", 1,  receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1);
        assertNotNull("Expected a message", receivedTextMessage = (TextMessage) subscriber.receive(3000));
        assertEquals("Unexpected delivery number", 2,  receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1);

        session.recover();

        assertNotNull("Expected a message", receivedTextMessage = (TextMessage) subscriber.receive(3000));
        int deliveryNumber = receivedTextMessage.getIntProperty(TestAmqpPeer.MESSAGE_NUMBER) + 1;
        assertEquals("Unexpected delivery number", 1,  deliveryNumber);

        testPeer.expectDisposition(true, new AcceptedMatcher(), 1, 1);

        receivedTextMessage.acknowledge();

        testPeer.expectDisposition(true, new ModifiedMatcher().withDeliveryFailed(equalTo(true)), 2, 2);
        testPeer.expectDetach(false, true, false);
        testPeer.expectDisposition(true, new ReleasedMatcher(), 3, 3);

        subscriber.close();

        testPeer.waitForAllHandlersToComplete(1000);

        testPeer.expectDurableSubUnsubscribeNullSourceLookup(false, false, subscriptionName, topicName, true);
        testPeer.expectDetach(true, true, true);

        session.unsubscribe(subscriptionName);

        testPeer.expectClose();

        connection.close();
    }
}
 
Example 10
Source File: SubscriptionsIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
private void doSharedAndExclusiveDurableSubsCantCoexistTestImpl(boolean sharedFirst) throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        // Add server connection capability to indicate support for shared-subs
        Symbol[] serverCapabilities = new Symbol[]{SHARED_SUBS};

        // Establish connection
        Connection connection = testFixture.establishConnecton(testPeer, serverCapabilities);
        connection.start();

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

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

        if(sharedFirst) {
            // Attach the durable shared receiver
            Matcher<?> durableLinkNameMatcher = equalTo(subscriptionName);
            testPeer.expectSharedDurableSubscriberAttach(topicName, subscriptionName, durableLinkNameMatcher, true);
            testPeer.expectLinkFlow();

            session.createSharedDurableConsumer(dest, subscriptionName);
        } else {
            // Attach the durable exclusive receiver
            testPeer.expectDurableSubscriberAttach(topicName, subscriptionName);
            testPeer.expectLinkFlow();

            session.createDurableConsumer(dest, subscriptionName);
        }

        testPeer.expectClose();

        try {
            if (sharedFirst) {
                // Now try to attach a durable exclusive receiver
                session.createDurableConsumer(dest, subscriptionName);
            } else {
                // Now try to attach a durable shared receiver
                session.createSharedDurableConsumer(dest, subscriptionName);
            }
            fail("Expected to fail due to concurrent shared + non-shared durable sub attempt");
        } catch (JMSException jmse) {
            // Expected
        }

        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 11
Source File: SubscriptionsIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testExclusiveDurableSubCanOnlyBeActiveOnceAtATime() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        // Establish connection
        Connection connection = testFixture.establishConnecton(testPeer);
        connection.start();

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

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

        // Attach the durable exclusive receiver
        testPeer.expectDurableSubscriberAttach(topicName, subscriptionName);
        testPeer.expectLinkFlow();

        MessageConsumer subscriber1 = session.createDurableConsumer(dest, subscriptionName);

        try {
            // Now try to attach a second active durable exclusive receiver
            session.createDurableConsumer(dest, subscriptionName);
            fail("Expected to fail due to concurrent active subscriber attempt");
        } catch (JMSException jmse) {
            // Expected
        }

        testPeer.expectDetach(false, true, false);
        subscriber1.close();

        // Now try to attach a new active durable exclusive receiver
        testPeer.expectDurableSubscriberAttach(topicName, subscriptionName);
        testPeer.expectLinkFlow();

        MessageConsumer subscriber2 = session.createDurableConsumer(dest, subscriptionName);
        assertNotNull(subscriber2);

        testPeer.expectClose();

        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}