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

The following examples show how to use javax.jms.TopicPublisher#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: JmsPoolTopicPublisherTest.java    From pooled-jms with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTopic() throws JMSException {
    JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
    TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = session.createTemporaryTopic();
    TopicPublisher publisher = session.createPublisher(topic);

    assertNotNull(publisher.getTopic());
    assertSame(topic, publisher.getTopic());

    publisher.close();

    try {
        publisher.getTopic();
        fail("Cannot read topic on closed publisher");
    } catch (IllegalStateException ise) {}
}
 
Example 2
Source File: JmsTopicPublisherTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 10000)
public void testMultipleCloseCallsNoErrors() throws Exception {
    Topic topic = session.createTopic(getTestName());
    TopicPublisher publisher = session.createPublisher(topic);
    publisher.close();
    publisher.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: 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 5
Source File: JMSFacade.java    From iaf with Apache License 2.0 4 votes vote down vote up
protected String sendByTopic(TopicSession session, Topic destination, javax.jms.Message message) throws NamingException, JMSException {
	TopicPublisher tps = session.createPublisher(destination);
	tps.publish(message);
	tps.close();
	return message.getJMSMessageID();
}