Java Code Examples for javax.jms.TopicSubscriber#receive()
The following examples show how to use
javax.jms.TopicSubscriber#receive() .
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: AbstractPersistentStatTestSupport.java From activemq-artemis with Apache License 2.0 | 6 votes |
protected void consumeDurableTestMessages(Connection connection, String sub, int size, String topicName, AtomicLong publishedMessageSize) throws Exception { Session session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(topicName); try { TopicSubscriber consumer = session.createDurableSubscriber(topic, sub); for (int i = 0; i < size; i++) { ActiveMQMessage message = (ActiveMQMessage) consumer.receive(); if (publishedMessageSize != null) { publishedMessageSize.addAndGet(-message.getCoreMessage().getEncodeSize()); } } } finally { session.close(); } }
Example 2
Source File: TopicSubscriberTest.java From qpid-broker-j with Apache License 2.0 | 5 votes |
@Test public void messageDeliveredToAllSubscribers() throws Exception { Topic topic = createTopic(getTestName()); final TopicConnection connection = getTopicConnection(); try { final TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); final TopicPublisher producer = session.createPublisher(topic); final TopicSubscriber subscriber1 = session.createSubscriber(topic); assertEquals("Unexpected subscriber1 topic", topic.getTopicName(), subscriber1.getTopic().getTopicName()); final TopicSubscriber subscriber2 = session.createSubscriber(topic); assertEquals("Unexpected subscriber2 topic", topic.getTopicName(), subscriber2.getTopic().getTopicName()); connection.start(); String messageText = "Test Message"; producer.send(session.createTextMessage(messageText)); final Message subscriber1Message = subscriber1.receive(getReceiveTimeout()); final Message subscriber2Message = subscriber2.receive(getReceiveTimeout()); assertTrue("TextMessage should be received by subscriber1", subscriber1Message instanceof TextMessage); assertEquals(messageText, ((TextMessage) subscriber1Message).getText()); assertTrue("TextMessage should be received by subscriber2", subscriber2Message instanceof TextMessage); assertEquals(messageText, ((TextMessage) subscriber2Message).getText()); } finally { connection.close(); } }
Example 3
Source File: DurableSubscriptionReactivationTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
public void testReactivateKeepaliveSubscription() throws Exception { Connection connection = createConnection(); connection.setClientID("cliID"); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber subscriber = session.createDurableSubscriber((Topic) createDestination(), "subName"); subscriber.close(); connection.close(); connection = createConnection(); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(createDestination()); producer.send(session.createMessage()); connection.close(); connection = createConnection(); connection.setClientID("cliID"); connection.start(); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); subscriber = session.createDurableSubscriber((Topic) createDestination(), "subName"); Message message = subscriber.receive(1 * 1000); subscriber.close(); connection.close(); assertNotNull("Message not received.", message); }
Example 4
Source File: TopicWildcardTest.java From ballerina-message-broker with Apache License 2.0 | 5 votes |
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 5
Source File: TopicWildcardTest.java From ballerina-message-broker with Apache License 2.0 | 5 votes |
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 6
Source File: TopicDistributedTransactionTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@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 7
Source File: DurableSubscriptionTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
@Test public void testDurableSubscriptionWithPeriodsInName() throws Exception { Connection conn = createConnection(); conn.setClientID(".client.id.with.periods."); Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber subscriber = s.createDurableSubscriber(ActiveMQServerTestCase.topic1, ".subscription.name.with.periods."); s.createProducer(ActiveMQServerTestCase.topic1).send(s.createTextMessage("Subscription test")); conn.start(); Message m = subscriber.receive(1000L); ProxyAssertSupport.assertNotNull(m); ProxyAssertSupport.assertTrue(m instanceof TextMessage); subscriber.close(); s.unsubscribe(".subscription.name.with.periods."); }
Example 8
Source File: DurableSubscribtionTest.java From qpid-broker-j with Apache License 2.0 | 4 votes |
@Test public void testUnsubscribe() throws Exception { Topic topic = createTopic(getTestName()); String subscriptionName = getTestName() + "_sub"; String clientId = "clientId"; int numberOfQueuesBeforeTest = getQueueCount(); Connection connection = getConnectionBuilder().setClientId(clientId).build(); try { Session durableSubscriberSession = connection.createSession(true, Session.SESSION_TRANSACTED); Session nonDurableSubscriberSession = connection.createSession(true, Session.SESSION_TRANSACTED); Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer subscriber = nonDurableSubscriberSession.createConsumer(topic); MessageProducer producer = producerSession.createProducer(topic); TopicSubscriber durableSubscriber = durableSubscriberSession.createDurableSubscriber(topic, subscriptionName); connection.start(); producer.send(nonDurableSubscriberSession.createTextMessage("A")); Message message = subscriber.receive(getReceiveTimeout()); assertTrue(message instanceof TextMessage); assertEquals("A", ((TextMessage) message).getText()); message = durableSubscriber.receive(getReceiveTimeout()); assertTrue(message instanceof TextMessage); assertEquals("A", ((TextMessage) message).getText()); nonDurableSubscriberSession.commit(); durableSubscriberSession.commit(); durableSubscriber.close(); durableSubscriberSession.unsubscribe(subscriptionName); producer.send(nonDurableSubscriberSession.createTextMessage("B")); Session durableSubscriberSession2 = connection.createSession(true, Session.SESSION_TRANSACTED); TopicSubscriber durableSubscriber2 = durableSubscriberSession2.createDurableSubscriber(topic, subscriptionName); producer.send(nonDurableSubscriberSession.createTextMessage("C")); message = subscriber.receive(getReceiveTimeout()); assertTrue(message instanceof TextMessage); assertEquals("B", ((TextMessage) message).getText()); message = subscriber.receive(getReceiveTimeout()); assertTrue(message instanceof TextMessage); assertEquals("C", ((TextMessage) message).getText()); message = durableSubscriber2.receive(getReceiveTimeout()); assertTrue(message instanceof TextMessage); assertEquals("C", ((TextMessage) message).getText()); nonDurableSubscriberSession.commit(); durableSubscriberSession2.commit(); assertEquals("Message count should be 0", 0, getTotalDepthOfQueuesMessages()); durableSubscriber2.close(); durableSubscriberSession2.unsubscribe(subscriptionName); } finally { connection.close(); } int numberOfQueuesAfterTest = getQueueCount(); assertEquals("Unexpected number of queues", numberOfQueuesBeforeTest, numberOfQueuesAfterTest); }
Example 9
Source File: BDBUpgradeTest.java From qpid-broker-j with Apache License 2.0 | 4 votes |
/** * 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 10
Source File: TopicLocalTransactionCommitTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@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 11
Source File: TopicDistributedTransactionTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@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 12
Source File: TopicDistributedTransactionTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@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: DurableTopicMessagesOrderTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@Parameters({"broker-port", "admin-username", "admin-password", "broker-hostname"}) @Test public void test1966DurableTopicMessagesOrderSingleSubscriber(String port, String adminUsername, String adminPassword, String brokerHostname) throws NamingException, JMSException { String topicName = "test1966DurableTopicMessagesOrderSingleSubscriber"; List<String> subscriberOneMessages = new ArrayList<>(); int numberOfMessages = 1966; 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.createDurableSubscriber(subscriberDestination, "1966_1"); // publish 1966 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(5000); 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."); }
Example 14
Source File: JMSSelectorTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@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 15
Source File: JMSSelectorTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@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 16
Source File: TopicLocalTransactionRollbackTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@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 17
Source File: DurableSubscribtionTest.java From qpid-broker-j with Apache License 2.0 | 4 votes |
@Test public void testResubscribeWithChangedNoLocal() throws Exception { assumeThat("QPID-8068", getProtocol(), is(equalTo(Protocol.AMQP_1_0))); String subscriptionName = getTestName() + "_sub"; Topic topic = createTopic(getTestName()); String clientId = "testClientId"; Connection connection = getConnectionBuilder().setClientId(clientId).build(); try { Session session = connection.createSession(true, Session.SESSION_TRANSACTED); TopicSubscriber durableSubscriber = session.createDurableSubscriber(topic, subscriptionName, null, false); MessageProducer producer = session.createProducer(topic); producer.send(session.createTextMessage("A")); producer.send(session.createTextMessage("B")); session.commit(); connection.start(); Message receivedMessage = durableSubscriber.receive(getReceiveTimeout()); assertTrue("TextMessage should be received", receivedMessage instanceof TextMessage); assertEquals("Unexpected message received", "A", ((TextMessage)receivedMessage).getText()); session.commit(); } finally { connection.close(); } connection = getConnectionBuilder().setClientId(clientId).build(); try { connection.start(); Session session2 = connection.createSession(true, Session.SESSION_TRANSACTED); TopicSubscriber noLocalSubscriber2 = session2.createDurableSubscriber(topic, subscriptionName, null, true); Connection secondConnection = getConnectionBuilder().setClientId("secondConnection").build(); try { Session secondSession = secondConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer secondProducer = secondSession.createProducer(topic); secondProducer.send(secondSession.createTextMessage("C")); } finally { secondConnection.close(); } Message noLocalSubscriberMessage = noLocalSubscriber2.receive(getReceiveTimeout()); assertTrue("TextMessage should be received", noLocalSubscriberMessage instanceof TextMessage); assertEquals("Unexpected message received", "C", ((TextMessage)noLocalSubscriberMessage).getText()); } finally { connection.close(); } }
Example 18
Source File: TopicLocalTransactionRollbackTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@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 19
Source File: DurableSubscribtionTest.java From qpid-broker-j with Apache License 2.0 | 4 votes |
/** * Tests that messages are delivered normally to a subscriber on a separate connection despite * the use of durable subscriber with no-local on the first connection. */ @Test public void testNoLocalSubscriberAndSubscriberOnSeparateConnection() throws Exception { String noLocalSubscriptionName = getTestName() + "_no_local_sub"; String subscriobtionName = getTestName() + "_sub"; Topic topic = createTopic(getTestName()); final String clientId = "clientId"; Connection noLocalConnection = getConnectionBuilder().setClientId(clientId).build(); try { Connection connection = getConnection(); try { Session noLocalSession = noLocalConnection.createSession(true, Session.SESSION_TRANSACTED); Session session = connection.createSession(true, Session.SESSION_TRANSACTED); MessageProducer noLocalSessionProducer = noLocalSession.createProducer(topic); MessageProducer sessionProducer = session.createProducer(topic); try { TopicSubscriber noLocalSubscriber = noLocalSession.createDurableSubscriber(topic, noLocalSubscriptionName, null, true); TopicSubscriber subscriber = session.createDurableSubscriber(topic, subscriobtionName, null, false); noLocalConnection.start(); connection.start(); noLocalSessionProducer.send(noLocalSession.createTextMessage("Message1")); noLocalSession.commit(); sessionProducer.send(session.createTextMessage("Message2")); sessionProducer.send(session.createTextMessage("Message3")); session.commit(); Message durableSubscriberMessage = noLocalSubscriber.receive(getReceiveTimeout()); assertTrue(durableSubscriberMessage instanceof TextMessage); assertEquals("Unexpected local message received", "Message2", ((TextMessage) durableSubscriberMessage).getText()); noLocalSession.commit(); Message nonDurableSubscriberMessage = subscriber.receive(getReceiveTimeout()); assertTrue(nonDurableSubscriberMessage instanceof TextMessage); assertEquals("Unexpected message received", "Message1", ((TextMessage) nonDurableSubscriberMessage).getText()); session.commit(); noLocalSubscriber.close(); subscriber.close(); } finally { noLocalSession.unsubscribe(noLocalSubscriptionName); session.unsubscribe(subscriobtionName); } } finally { connection.close(); } } finally { noLocalConnection.close(); } }
Example 20
Source File: DivertTest.java From activemq-artemis with Apache License 2.0 | 3 votes |
@Test public void testDivertedNotificationMessagePropertiesOpenWire() throws Exception { final String testAddress = ActiveMQDefaultConfiguration.getDefaultManagementNotificationAddress().toString(); final String forwardAddress = "forwardAddress"; DivertConfiguration divertConf = new DivertConfiguration().setName("divert1").setRoutingName("divert1").setAddress(testAddress).setForwardingAddress(forwardAddress).setFilterString("_AMQ_NotifType = 'CONSUMER_CREATED' OR _AMQ_NotifType = 'CONSUMER_CLOSED'"); Configuration config = createDefaultNettyConfig().addDivertConfiguration(divertConf); ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, false)); server.start(); ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); connectionFactory.setClientID("myClientID"); Topic forwardTopic = new ActiveMQTopic(forwardAddress); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber subscriber = session.createDurableSubscriber(forwardTopic, "mySubscriptionName"); javax.jms.Message message = subscriber.receive(DivertTest.TIMEOUT); connection.close(); Assert.assertNotNull(message); Assert.assertEquals("CONSUMER_CREATED", message.getStringProperty("_AMQ_NotifType")); }