Java Code Examples for javax.jms.TopicSession#close()

The following examples show how to use javax.jms.TopicSession#close() . 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: NetworkRemovesSubscriptionsTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testWithSessionCloseOutsideTheLoop() throws Exception {

      TopicConnection connection = connectionFactory.createTopicConnection();
      connection.start();
      TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      for (int i = 0; i < 100; i++) {

         TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
         DummyMessageListener listener = new DummyMessageListener();
         subscriber.setMessageListener(listener);
         subscriber.close();
      }
      subscriberSession.close();
      connection.close();
      Thread.sleep(1000);
      Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
      assertNotNull(dest);
      assertTrue(dest.getConsumers().isEmpty());

   }
 
Example 2
Source File: NetworkRemovesSubscriptionsTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testWithSessionAndSubsciberClose() throws Exception {

      TopicConnection connection = connectionFactory.createTopicConnection();
      connection.start();

      for (int i = 0; i < 100; i++) {
         TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
         TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
         DummyMessageListener listener = new DummyMessageListener();
         subscriber.setMessageListener(listener);
         subscriber.close();
         subscriberSession.close();
      }
      connection.close();
      Thread.sleep(1000);
      Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
      assertNotNull(dest);
      assertTrue(dest.getConsumers().isEmpty());
   }
 
Example 3
Source File: NetworkRemovesSubscriptionsTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public void testWithOneSubscriber() throws Exception {

      TopicConnection connection = connectionFactory.createTopicConnection();
      connection.start();
      TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

      TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
      DummyMessageListener listener = new DummyMessageListener();
      subscriber.setMessageListener(listener);
      subscriber.close();
      subscriberSession.close();
      connection.close();
      Thread.sleep(1000);
      Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
      assertNotNull(dest);
      assertTrue(dest.getConsumers().isEmpty());
   }
 
Example 4
Source File: NetworkRemovesSubscriptionsTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Running this test you can produce a leak of only 2 ConsumerInfo on BE
 * broker, NOT 200 as in other cases!
 */
public void testWithoutSessionAndSubsciberClosePlayAround() throws Exception {

   TopicConnection connection = connectionFactory.createTopicConnection();
   connection.start();

   for (int i = 0; i < 100; i++) {
      TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
      DummyMessageListener listener = new DummyMessageListener();
      subscriber.setMessageListener(listener);
      if (i != 50) {
         subscriber.close();
         subscriberSession.close();
      }
   }

   connection.close();
   Thread.sleep(1000);
   Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
   assertNotNull(dest);
   assertTrue(dest.getConsumers().isEmpty());
}
 
Example 5
Source File: TopicSubscriberTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
@Parameters({ "broker-port"})
@Test
public void testSubscriberPublisher(String port) throws Exception {
    String topicName = "MyTopic1";
    int numberOfMessages = 100;

    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder("admin", "admin", "localhost", port)
            .withTopic(topicName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    // Initialize subscriber
    TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic subscriberDestination = (Topic) initialContext.lookup(topicName);
    TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination);

    // publish 100 messages
    TopicSession producerSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    TopicPublisher producer = producerSession.createPublisher(subscriberDestination);

    for (int i = 0; i < numberOfMessages; i++) {
        producer.publish(producerSession.createTextMessage("Test message " + i));
    }

    producerSession.close();

    for (int i = 0; i < numberOfMessages; i++) {
        Message message = subscriber.receive(1000);
        Assert.assertNotNull(message, "Message #" + i + " was not received");
    }

    connection.close();
}
 
Example 6
Source File: JMSTopicConsumerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testTemporarySubscriptionDeleted() throws Exception {
   Connection connection = createConnection();

   try {
      TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      Topic topic = session.createTopic(getTopicName());
      TopicSubscriber myNonDurSub = session.createSubscriber(topic);
      assertNotNull(myNonDurSub);

      Bindings bindingsForAddress = server.getPostOffice().getBindingsForAddress(new SimpleString(getTopicName()));
      Assert.assertEquals(2, bindingsForAddress.getBindings().size());
      session.close();

      final CountDownLatch latch = new CountDownLatch(1);
      server.getRemotingService().getConnections().iterator().next().addCloseListener(new CloseListener() {
         @Override
         public void connectionClosed() {
            latch.countDown();
         }
      });

      connection.close();
      latch.await(5, TimeUnit.SECONDS);
      bindingsForAddress = server.getPostOffice().getBindingsForAddress(new SimpleString(getTopicName()));
      Assert.assertEquals(1, bindingsForAddress.getBindings().size());
   } finally {
      connection.close();
   }
}
 
Example 7
Source File: TopicWildcardTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
private void assertNullWithPublishSubscribeForTopics(String publishTopicName,
                                                     String subscribeTopicName) throws Exception {

    int numberOfMessages = 100;

    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder("admin", "admin", "localhost", port)
            .withTopic(publishTopicName)
            .withTopic(subscribeTopicName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic subscriberDestination = (Topic) initialContext.lookup(subscribeTopicName);
    TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination);

    TopicSession publisherSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic publisherDestination = (Topic) initialContext.lookup(publishTopicName);
    TopicPublisher publisher = publisherSession.createPublisher(publisherDestination);

    for (int i = 0; i < numberOfMessages; i++) {
        publisher.publish(publisherSession.createTextMessage("Test message " + i));
    }

    publisherSession.close();

    Message message = subscriber.receive(1000);
    Assert.assertNull(message, "A message was received where no message was expected");

    subscriberSession.close();
    connection.close();
}
 
Example 8
Source File: TopicWildcardTest.java    From ballerina-message-broker with Apache License 2.0 5 votes vote down vote up
private void assertNotNullWithPublishSubscribeForTopics(String publishTopicName,
                                                        String subscribeTopicName) throws Exception {

    int numberOfMessages = 100;

    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder("admin", "admin", "localhost", port)
            .withTopic(publishTopicName)
            .withTopic(subscribeTopicName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic subscriberDestination = (Topic) initialContext.lookup(subscribeTopicName);
    TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination);

    TopicSession publisherSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic publisherDestination = (Topic) initialContext.lookup(publishTopicName);
    TopicPublisher publisher = publisherSession.createPublisher(publisherDestination);

    for (int i = 0; i < numberOfMessages; i++) {
        publisher.publish(publisherSession.createTextMessage("Test message " + i));
    }

    publisherSession.close();

    for (int i = 0; i < numberOfMessages; i++) {
        Message message = subscriber.receive(1000);
        Assert.assertNotNull(message, "Message #" + i + " was not received");
    }

    subscriberSession.close();
    connection.close();
}
 
Example 9
Source File: JMSSelectorTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test
public void testNegativeJMSSelectorConsumerProducer(String port,
                                                    String adminUsername,
                                                    String adminPassword,
                                                    String brokerHostname) throws NamingException, JMSException {
    String queueName = "testNegativeJMSSelectorConsumerProducer";
    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withTopic(queueName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    TopicSession subscriberSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    Topic topic = (Topic) initialContext.lookup(queueName);

    // Subscribe with a selector
    String propertyName = "MyProperty";
    String propertyValue = "propertyValue";
    String jmsPropertySelector = propertyName + " = '" + propertyValue + "'";
    TopicSubscriber consumer = subscriberSession.createSubscriber(topic, jmsPropertySelector, false);

    // publish messages with property
    TopicSession producerSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    TopicPublisher producer = producerSession.createPublisher(topic);

    // Send messages with a different property value
    int numberOfMessages = 100;
    for (int i = 0; i < numberOfMessages; i++) {
        TextMessage textMessage = producerSession.createTextMessage("Test message " + i);
        textMessage.setStringProperty(propertyName, propertyValue + "-1");
        producer.send(textMessage);
    }

    // consume messages
    Message message = consumer.receive(100);
    Assert.assertNull(message, "Message received. Shouldn't receive any messages.");

    producerSession.close();
    connection.close();
}
 
Example 10
Source File: JMSSelectorTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test
public void testPositiveJMSSelectorConsumerProducer(String port,
                                                    String adminUsername,
                                                    String adminPassword,
                                                    String brokerHostname) throws NamingException, JMSException {
    String queueName = "testPositiveJMSSelectorConsumerProducer";
    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withTopic(queueName)
            .build();


    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    TopicSession subscriberSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    Topic topic = (Topic) initialContext.lookup(queueName);

    // Subscribe with a selector
    String propertyName = "MyProperty";
    String propertyValue = "propertyValue";
    String jmsPropertySelector = propertyName + " = '" + propertyValue + "'";
    TopicSubscriber consumer = subscriberSession.createSubscriber(topic, jmsPropertySelector, false);

    // publish messages with property
    TopicSession producerSession = connection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    TopicPublisher producer = producerSession.createPublisher(topic);

    int numberOfMessages = 100;
    for (int i = 0; i < numberOfMessages; i++) {
        TextMessage textMessage = producerSession.createTextMessage("Test message " + i);
        textMessage.setStringProperty(propertyName, propertyValue);
        producer.send(textMessage);
    }

    // consume messages
    for (int i = 0; i < numberOfMessages; i++) {
        Message message = consumer.receive(1000);
        Assert.assertNotNull(message, "Message #" + i + " was not received");
    }

    producerSession.close();
    connection.close();
}
 
Example 11
Source File: TopicDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Test
public void testPublisherWithCommit() throws Exception {

    String subscriptionId = "sub-testPublisherWithCommit";
    String topicName = "testPublisherWithCommit";
    String testMessage = "testPublisherWithCommit-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory().withTopic(topicName).build();
    Topic topic = (Topic) initialContext.lookup(topicName);

    // Setup XA producer.
    XATopicConnectionFactory xaTopicConnectionFactory =
            (XATopicConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);
    XATopicConnection xaTopicConnection = xaTopicConnectionFactory.createXATopicConnection();
    XATopicSession xaTopicSession = xaTopicConnection.createXATopicSession();
    XAResource xaResource = xaTopicSession.getXAResource();
    MessageProducer producer = xaTopicSession.createProducer(topic);

    // Setup non-transactional consumer.
    TopicSession topicSession = xaTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    TopicSubscriber durableSubscriber = topicSession.createDurableSubscriber(topic, subscriptionId);

    xaTopicConnection.start();

    // Send message within XA transaction.
    XidImpl xid = new XidImpl(0, "branchId".getBytes(), "globalId".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    producer.send(xaTopicSession.createTextMessage(testMessage));
    xaResource.end(xid, XAResource.TMSUCCESS);

    int response = xaResource.prepare(xid);
    Assert.assertEquals(response, XAResource.XA_OK, "Prepare stage failed.");

    xaResource.commit(xid, false);

    TextMessage message = (TextMessage) durableSubscriber.receive(2000);
    Assert.assertNotNull(message, "Didn't receive a message");
    Assert.assertEquals(message.getText(), testMessage, "Received message content didn't match sent message.");

    topicSession.close();
    xaTopicSession.close();
    xaTopicConnection.close();
}
 
Example 12
Source File: TopicDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Test
public void testSubscriberWithCommit() throws Exception {

    String subscriptionId = "sub-testSubscriberWithCommit";
    String topicName = "testSubscriberWithCommit";
    String testMessage = "testSubscriberWithCommit-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory().withTopic(topicName).build();
    Topic topic = (Topic) initialContext.lookup(topicName);

    // Create XA consumer.
    XATopicConnectionFactory xaTopicConnectionFactory =
            (XATopicConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);
    XATopicConnection xaTopicConnection = xaTopicConnectionFactory.createXATopicConnection();
    XATopicSession xaTopicSession = xaTopicConnection.createXATopicSession();
    XAResource xaResource = xaTopicSession.getXAResource();
    TopicSubscriber durableSubscriber = xaTopicSession.createDurableSubscriber(topic, subscriptionId);

    // Create non transactional producer.
    TopicSession topicSession = xaTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    MessageProducer producer = topicSession.createProducer(topic);

    xaTopicConnection.start();

    producer.send(xaTopicSession.createTextMessage(testMessage));

    // Consume message within a XA transaction.
    XidImpl xid = new XidImpl(0, "branchId".getBytes(), "globalId".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    TextMessage message = (TextMessage) durableSubscriber.receive(2000);
    xaResource.end(xid, XAResource.TMSUCCESS);

    int response = xaResource.prepare(xid);
    Assert.assertEquals(response, XAResource.XA_OK, "Prepare stage failed.");

    xaResource.commit(xid, false);

    Assert.assertNotNull(message, "Didn't receive a message");
    Assert.assertEquals(message.getText(), testMessage, "Received message content didn't match sent message.");

    topicSession.close();
    xaTopicSession.close();
    xaTopicConnection.close();

    QueueMetadata queueMetadata = restApiClient.getQueueMetadata("carbon:" + subscriptionId);
    Assert.assertEquals((int) queueMetadata.getSize(), 0, "Queue should be empty.");
}
 
Example 13
Source File: TopicDistributedTransactionTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Test
public void testSubscriberWithRollback() throws Exception {

    String subscriptionId = "sub-testSubscriberWithRollback";
    String topicName = "testSubscriberWithCommit";
    String testMessage = "testSubscriberWithCommit-Message";
    InitialContext initialContext = initialContextBuilder.withXaConnectionFactory().withTopic(topicName).build();
    Topic topic = (Topic) initialContext.lookup(topicName);

    // Setup XA consumer.
    XATopicConnectionFactory xaTopicConnectionFactory =
            (XATopicConnectionFactory) initialContext.lookup(ClientHelper.XA_CONNECTION_FACTORY);
    XATopicConnection xaTopicConnection = xaTopicConnectionFactory.createXATopicConnection();
    XATopicSession xaTopicSession = xaTopicConnection.createXATopicSession();
    XAResource xaResource = xaTopicSession.getXAResource();
    TopicSubscriber durableSubscriber = xaTopicSession.createDurableSubscriber(topic, subscriptionId);

    // Setup non-transactional message publisher.
    TopicSession topicSession = xaTopicConnection.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
    MessageProducer producer = topicSession.createProducer(topic);

    xaTopicConnection.start();

    producer.send(xaTopicSession.createTextMessage(testMessage));

    // Consume messages within a XA transaction.
    XidImpl xid = new XidImpl(0, "branchId".getBytes(), "globalId".getBytes());
    xaResource.start(xid, XAResource.TMNOFLAGS);
    TextMessage message = (TextMessage) durableSubscriber.receive(2000);
    xaResource.end(xid, XAResource.TMSUCCESS);

    int response = xaResource.prepare(xid);
    Assert.assertEquals(response, XAResource.XA_OK, "Prepare stage failed.");

    xaResource.rollback(xid);

    Assert.assertNotNull(message, "Didn't receive a message");
    Assert.assertEquals(message.getText(), testMessage, "Received message content didn't match sent message.");

    topicSession.close();
    xaTopicSession.close();
    xaTopicConnection.close();

    QueueMetadata queueMetadata = restApiClient.getQueueMetadata("carbon:" + subscriptionId);
    Assert.assertEquals((int) queueMetadata.getSize(), 1, "Queue shouldn't be empty.");
}
 
Example 14
Source File: TopicLocalTransactionCommitTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test(expectedExceptions = javax.jms.IllegalStateException.class,
        expectedExceptionsMessageRegExp = ".*Session is not transacted")
public void testCommitOnNonTransactionTopicSession(String port,
        String adminUsername,
        String adminPassword,
        String brokerHostname) throws NamingException, JMSException {
    String topicName = "testCommitOnNonTransactionTopicSession";
    int numberOfMessages = 100;

    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withTopic(topicName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    // initialize subscriber
    TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic subscriberDestination = (Topic) initialContext.lookup(topicName);
    TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination);

    // publish 100 messages
    TopicSession producerSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    TopicPublisher producer = producerSession.createPublisher(subscriberDestination);

    for (int i = 0; i < numberOfMessages; i++) {
        producer.publish(producerSession.createTextMessage("Test message " + i));
    }

    try {
        // commit all publish messages
        producerSession.commit();

        Message message = subscriber.receive(1000);
        Assert.assertNull(message, "Messages should not receive message after calling commit on "
                                   + "non transaction channel");

    } catch (JMSException e) {
        //catch exception and re-throw it since we need the connection to be closed
        throw e;
    } finally {
        producerSession.close();
        subscriberSession.close();
        connection.close();
    }
}
 
Example 15
Source File: BDBUpgradeTest.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
/**
 * Test that the selector applied to the DurableSubscription was successfully
 * transferred to the new store, and functions as expected with continued use
 * by monitoring message count while sending new messages to the topic and then
 * consuming them.
 */
@Test
public void testSelectorDurability() throws Exception
{
    TopicConnection connection = getTopicConnection();
    try
    {
        connection.start();

        TopicSession session = connection.createTopicSession(true, Session.SESSION_TRANSACTED);
        Topic topic = session.createTopic(SELECTOR_TOPIC_NAME);
        TopicPublisher publisher = session.createPublisher(topic);

        int index = ThreadLocalRandom.current().nextInt();
        Message messageA = session.createTextMessage("A");
        messageA.setIntProperty("ID", index);
        messageA.setStringProperty("testprop", "false");
        publisher.publish(messageA);

        Message messageB = session.createTextMessage("B");
        messageB.setIntProperty("ID", index);
        messageB.setStringProperty("testprop", "true");
        publisher.publish(messageB);

        session.commit();

        TopicSubscriber subscriber =
                session.createDurableSubscriber(topic, SELECTOR_SUB_NAME, "testprop='true'", false);
        Message migrated = subscriber.receive(getReceiveTimeout());
        assertThat("Failed to receive migrated message", migrated, is(notNullValue()));

        Message received = subscriber.receive(getReceiveTimeout());
        session.commit();
        assertThat("Failed to receive published message", received, is(notNullValue()));
        assertThat("Message is not Text message", received, is(instanceOf(TextMessage.class)));
        assertThat("Unexpected text", ((TextMessage) received).getText(), is(equalTo("B")));
        assertThat("Unexpected index", received.getIntProperty("ID"), is(equalTo(index)));

        session.close();
    }
    finally
    {
        connection.close();
    }
}
 
Example 16
Source File: BDBUpgradeTest.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
/**
 * Test that the DurableSubscription without selector was successfully
 * transfered to the new store, and functions as expected with continued use.
 */
@Test
public void testDurableSubscriptionWithoutSelector() throws Exception
{
    TopicConnection connection = getTopicConnection();
    try
    {
        connection.start();

        TopicSession session = connection.createTopicSession(true, Session.SESSION_TRANSACTED);

        Topic topic = session.createTopic(TOPIC_NAME);
        TopicPublisher publisher = session.createPublisher(topic);

        int index = ThreadLocalRandom.current().nextInt();
        Message messageA = session.createTextMessage("A");
        messageA.setIntProperty("ID", index);
        messageA.setStringProperty("testprop", "false");
        publisher.publish(messageA);

        Message messageB = session.createTextMessage("B");
        messageB.setIntProperty("ID", index);
        messageB.setStringProperty("testprop", "true");
        publisher.publish(messageB);

        session.commit();

        TopicSubscriber subscriber = session.createDurableSubscriber(topic, SUB_NAME);
        Message migrated = subscriber.receive(getReceiveTimeout());
        assertThat("Failed to receive migrated message", migrated, is(notNullValue()));

        Message receivedA = subscriber.receive(getReceiveTimeout());
        session.commit();
        assertThat("Failed to receive published message A", receivedA, is(notNullValue()));
        assertThat("Message A is not Text message", receivedA, is(instanceOf(TextMessage.class)));
        assertThat("Unexpected text for A", ((TextMessage) receivedA).getText(), is(equalTo("A")));
        assertThat("Unexpected index", receivedA.getIntProperty("ID"), is(equalTo(index)));

        Message receivedB = subscriber.receive(getReceiveTimeout());
        session.commit();
        assertThat("Failed to receive published message B", receivedB, is(notNullValue()));
        assertThat("Message B is not Text message", receivedB, is(instanceOf(TextMessage.class)));
        assertThat("Unexpected text for B", ((TextMessage) receivedB).getText(), is(equalTo("B")));
        assertThat("Unexpected index  for B", receivedB.getIntProperty("ID"), is(equalTo(index)));

        session.commit();
        session.close();
    }
    finally
    {
        connection.close();
    }
}
 
Example 17
Source File: TopicLocalTransactionRollbackTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test
public void testPublisherCloseBeforeRollbackTransaction(String port,
                                                        String adminUsername,
                                                        String adminPassword,
                                                        String brokerHostname)
        throws NamingException, JMSException {
    String topicName = "testPublisherCloseBeforeRollbackTransaction";
    int numberOfMessages = 100;

    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withTopic(topicName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    // initialize subscriber
    TopicSession subscriberSession = connection.createTopicSession(true, Session.SESSION_TRANSACTED);
    Topic subscriberDestination = (Topic) initialContext.lookup(topicName);
    TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination);

    // publish 100 messages
    TopicSession producerSession = connection.createTopicSession(true, Session.SESSION_TRANSACTED);
    TopicPublisher producer = producerSession.createPublisher(subscriberDestination);

    for (int i = 0; i < numberOfMessages; i++) {
        producer.publish(producerSession.createTextMessage("Test message " + i));
    }

    // close publisher before rollback
    producer.close();

    // rollback all publish messages
    producerSession.rollback();

    Message message = subscriber.receive(1000);
    Assert.assertNull(message, "Messages should not receive upon publisher rollback");

    producerSession.close();
    subscriberSession.close();
    connection.close();
}
 
Example 18
Source File: ConnectionTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testTXTypeInvalid() throws Exception {
   conn = cf.createConnection();

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

   assertEquals(Session.AUTO_ACKNOWLEDGE, sess.getAcknowledgeMode());

   sess.close();

   TopicSession tpSess = ((TopicConnection) conn).createTopicSession(false, Session.SESSION_TRANSACTED);

   assertEquals(Session.AUTO_ACKNOWLEDGE, tpSess.getAcknowledgeMode());

   tpSess.close();

   QueueSession qSess = ((QueueConnection) conn).createQueueSession(false, Session.SESSION_TRANSACTED);

   assertEquals(Session.AUTO_ACKNOWLEDGE, qSess.getAcknowledgeMode());

   qSess.close();

}
 
Example 19
Source File: TopicLocalTransactionRollbackTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test
public void testPublisherRollbackTransaction(String port,
                                             String adminUsername,
                                             String adminPassword,
                                             String brokerHostname) throws NamingException, JMSException {
    String topicName = "testPublisherRollbackTransaction";
    int numberOfMessages = 100;

    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withTopic(topicName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    // initialize subscriber
    TopicSession subscriberSession = connection.createTopicSession(true, Session.SESSION_TRANSACTED);
    Topic subscriberDestination = (Topic) initialContext.lookup(topicName);
    TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination);

    // publish 100 messages
    TopicSession producerSession = connection.createTopicSession(true, Session.SESSION_TRANSACTED);
    TopicPublisher producer = producerSession.createPublisher(subscriberDestination);

    for (int i = 0; i < numberOfMessages; i++) {
        producer.publish(producerSession.createTextMessage("Test message " + i));
    }
    // rollback all publish messages
    producerSession.rollback();

    // Consume published messages
    Message message = subscriber.receive(1000);
    Assert.assertNull(message, "Messages should not receive upon publisher rollback");

    producerSession.close();
    subscriberSession.close();
    connection.close();
}
 
Example 20
Source File: TopicMessagesOrderTest.java    From ballerina-message-broker with Apache License 2.0 4 votes vote down vote up
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"})
@Test
public void test1338TopicMessagesOrderSingleSubscriber(String port,
                                                       String adminUsername,
                                                       String adminPassword,
                                                       String brokerHostname) throws NamingException, JMSException {
    String topicName = "test1338TopicMessagesOrderSingleSubscriber";
    List<String> subscriberOneMessages = new ArrayList<>();
    int numberOfMessages = 1338;

    InitialContext initialContext = ClientHelper
            .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port)
            .withTopic(topicName)
            .build();

    TopicConnectionFactory connectionFactory
            = (TopicConnectionFactory) initialContext.lookup(ClientHelper.CONNECTION_FACTORY);
    TopicConnection connection = connectionFactory.createTopicConnection();
    connection.start();

    // Initialize subscriber
    TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic subscriberDestination = (Topic) initialContext.lookup(topicName);
    TopicSubscriber subscriber = subscriberSession.createSubscriber(subscriberDestination);

    // publish 1338 messages
    TopicSession producerSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    TopicPublisher producer = producerSession.createPublisher(subscriberDestination);

    for (int i = 0; i < numberOfMessages; i++) {
        producer.publish(producerSession.createTextMessage(String.valueOf(i)));
    }

    producerSession.close();

    for (int i = 0; i < numberOfMessages; i++) {
        TextMessage message = (TextMessage) subscriber.receive(1000);
        Assert.assertNotNull(message, "Message #" + i + " was not received");
        subscriberOneMessages.add(message.getText());
    }

    subscriberSession.close();

    connection.close();

    // verify order is preserved
    boolean isOrderPreserved = true;
    for (int i = 0; i < numberOfMessages; i++) {
        if (!(i == Integer.parseInt(subscriberOneMessages.get(i)))) {
            isOrderPreserved = false;
            break;
        }
    }

    Assert.assertTrue(isOrderPreserved, "Topic messages order not preserved for single subscriber.");
}