Java Code Examples for javax.jms.TopicPublisher#send()

The following examples show how to use javax.jms.TopicPublisher#send() . 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: PooledConnectionTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000)
public void testTopicMessageSend() throws Exception {
    cf.setMaxConnections(1);

    TopicConnection connection = cf.createTopicConnection();

    try {
        TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = topicSession.createTopic(getTestName());

        TopicPublisher topicPublisher = topicSession.createPublisher(topic);
        topicPublisher.send(topicSession.createMessage());
        assertEquals(1, cf.getNumConnections());
    } finally {
        connection.close();
        cf.stop();
    }
}
 
Example 2
Source File: TopicSubscriberTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@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: MultipleTopicSubscriberTestCase.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 testMultipleTopicSubscribersOnSameSession(String port,
                                                      String adminUsername,
                                                      String adminPassword,
                                                      String brokerHostname)
        throws NamingException, JMSException, InterruptedException {
    String queueName = "testMultipleTopicSubscribersOnSameSession";
    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.CLIENT_ACKNOWLEDGE);
    Topic topic = (Topic) initialContext.lookup(queueName);

    int numberOfConsumers = 3;
    int messagesPerConsumer = 1000;
    int maxNumberOfMessages = numberOfConsumers * messagesPerConsumer;
    LinkedBlockingQueue<MessageResult> receiveQueue =  new LinkedBlockingQueue<>(maxNumberOfMessages);

    TopicSubscriber consumers[] = new TopicSubscriber[numberOfConsumers];
    int messageCount[] = new int[numberOfConsumers];

    for (int consumerIndex = 0; consumerIndex < numberOfConsumers; consumerIndex++) {
        consumers[consumerIndex] = subscriberSession.createSubscriber(topic);
        int finalConsumerIndex = consumerIndex;
        consumers[consumerIndex].setMessageListener(message -> {
            messageCount[finalConsumerIndex]++;
            try {
                message.acknowledge();
            } catch (JMSException e) {
                LOGGER.error("Message acknowledging failed.", e);
            }
            receiveQueue.offer(new MessageResult(message, finalConsumerIndex));
        });
    }

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

    TextMessage textMessage;
    String consumerMessage = "testMessage";
    for (int i = 0; i < messagesPerConsumer; i++) {
        textMessage = producerSession.createTextMessage(consumerMessage);
        producer.send(textMessage);
    }


    for (int i = 0; i < maxNumberOfMessages; i++) {
        MessageResult result = receiveQueue.poll(5, TimeUnit.SECONDS);
        if (result == null) {
            StringBuilder countSummary = new StringBuilder();
            for (int consumerIndex = 0; consumerIndex < numberOfConsumers; consumerIndex++) {
                countSummary.append("Consumer ")
                            .append(consumerIndex)
                            .append(" received ")
                            .append(messageCount[consumerIndex])
                            .append(" messages, ");
            }

            Assert.fail("Messages stopped receiving after " + i + " iterations. " + countSummary.toString());
        } else {
            TextMessage textMessage1 = (TextMessage) result.getMessage();
            Assert.assertEquals(textMessage1.getText(),
                                consumerMessage,
                                "Incorrect message received for consumer " + result.getConsumerId());
        }

    }

    for (int consumerIndex = 0; consumerIndex < numberOfConsumers; consumerIndex++) {
        Assert.assertEquals(messageCount[consumerIndex],
                            messagesPerConsumer,
                            "Message " + messageCount[consumerIndex]
                                    + " received for consumer " + consumerIndex + ".");
    }

    producer.close();
    for (int consumerIndex = 0; consumerIndex < numberOfConsumers; consumerIndex++) {
        consumers[consumerIndex].close();
    }

    connection.close();
}
 
Example 4
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 5
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 6
Source File: ObjectMessageDeliveryTest.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
/**
 *
 */
@Test
public void testTopic() throws Exception {
   TopicConnection conn = getTopicConnectionFactory().createTopicConnection();

   try {
      TopicSession s = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
      TopicPublisher publisher = s.createPublisher(ActiveMQServerTestCase.topic1);
      TopicSubscriber sub = s.createSubscriber(ActiveMQServerTestCase.topic1);
      conn.start();

      // Create 3 object messages with different bodies

      TestObject to1 = new TestObject();
      to1.text = "hello1";

      TestObject to2 = new TestObject();
      to1.text = "hello2";

      TestObject to3 = new TestObject();
      to1.text = "hello3";

      ObjectMessage om1 = s.createObjectMessage();
      om1.setObject(to1);

      ObjectMessage om2 = s.createObjectMessage();
      om2.setObject(to2);

      ObjectMessage om3 = s.createObjectMessage();
      om3.setObject(to3);

      // send to topic
      publisher.send(om1);

      publisher.send(om2);

      publisher.send(om3);

      ObjectMessage rm1 = (ObjectMessage) sub.receive(ActiveMQServerTestCase.MAX_TIMEOUT);

      ObjectMessage rm2 = (ObjectMessage) sub.receive(ActiveMQServerTestCase.MAX_TIMEOUT);

      ObjectMessage rm3 = (ObjectMessage) sub.receive(ActiveMQServerTestCase.MAX_TIMEOUT);

      ProxyAssertSupport.assertNotNull(rm1);

      TestObject ro1 = (TestObject) rm1.getObject();

      ProxyAssertSupport.assertEquals(to1.text, ro1.text);
      ProxyAssertSupport.assertNotNull(rm1);

      TestObject ro2 = (TestObject) rm2.getObject();

      ProxyAssertSupport.assertEquals(to2.text, ro2.text);

      ProxyAssertSupport.assertNotNull(rm2);

      TestObject ro3 = (TestObject) rm3.getObject();

      ProxyAssertSupport.assertEquals(to3.text, ro3.text);
   } finally {
      if (conn != null) {
         conn.close();
      }
   }
}