简体   繁体   中英

Browse Jms Queue in a multiThreading way

I have a spring-batch which browses Queue messages, this queue supposed to contain a huge number of messages. then it takes a lot of time to treat all of them. therefore i thought about multi-Threading to deal with the issue, but it's not clear for me yet.

Here is an example of browsing a Queue without multithreading:


import java.net.URISyntaxException;
import java.util.Enumeration;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class JmsQueueBrowseExample {
    public static void main(String[] args) throws URISyntaxException, Exception {
        Connection connection = null;
        try {
            // Producer
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
                    "tcp://localhost:61616");
            connection = connectionFactory.createConnection();
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("browseQueue");
            MessageConsumer consumer = session.createConsumer(queue);
            connection.start();

            System.out.println("Browse through the elements in queue");
            QueueBrowser browser = session.createBrowser(queue);
            Enumeration e = browser.getEnumeration();
            //Multithreading here
            while (e.hasMoreElements()) {
                TextMessage message = (TextMessage) e.nextElement();
                System.out.println("Browse [" + message.getText() + "]");
            }
            System.out.println("Done");
            browser.close();

            session.close();
        } finally {
            if (connection != null) {
                connection.close();
            }
        }
    }

}

Thank you

Other than the close methods the JMS API Resources such as Session, MessageConsumer, QueueBrowser etc are not meant to be used by more than a single thread of control and as such trying to concurrently iterate over the messages returned from a QueueBrowser enumeration is likely to result is errors.

The JMS specification adds some insight into concurrency with session resources.

There are no restrictions on the number of threads that can use a Session object or those it creates. The restriction is that the resources of a Session should not be used concurrently by multiple threads. It is up to the user to insure that this concurrency restriction is met. The simplest way to do this is to use one thread. In the case of asynchronous delivery, use one thread for setup in stopped mode and then start asynchronous delivery. In more complex cases the user must provide explicit synchronization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM