Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Throttling JMS Ques in Spring

Posted on 2008-06-12
4
Medium Priority
?
1,500 Views
Last Modified: 2008-06-17
I am developing an app that uses JMS ques in Spring. The app runs on a JBoss server. The ques are asynchronous and I am placing several messages on the que. The problem is I only want to process a couple of messages at a time. Does anyone know how I can set the que/listener so that only a few messages get sent to the listener, those messages get processed, then another couple of messages get sent to the listener and so on until Im out of messages?
0
Comment
Question by:texastemplar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
manuel_m earned 1500 total points
ID: 21773068
If you are using a listener to get the messages the listener will receive as many messages as he can get.
You have to receive the messages by your own.

manuel
final String YOUR_QUEUE = "queue/testQueue";
    Context         ctx      = null;
    QueueConnection connect  = null;
    QueueSession    session  = null;
    Queue           queue    = null;
    QueueReceiver   receiver = null;
    try {
      ctx = new InitialContext();
      QueueConnectionFactory fact = (QueueConnectionFactory)
                                    ctx.lookup( "ConnectionFactory" );
      connect = fact.createQueueConnection();
      session = connect.createQueueSession( false, Session.AUTO_ACKNOWLEDGE );
      try {
        queue = (Queue) ctx.lookup( YOUR_QUEUE );
      } catch( NameNotFoundException ex ) {
        queue = session.createQueue( YOUR_QUEUE );
        ctx.bind( YOUR_QUEUE, queue );
      }
      receiver = session.createReceiver( queue );
      connect.start();
      // get only 10 messages
      for( int i=0; i<10; i++ ) {
        // set timeout to 1000 ms per message
        TextMessage msg = (TextMessage) receiver.receive( 1000 );
        if( null != msg ) {
          System.out.println( msg.getText() );
          // acknowledge the message so it is out of the queue
          msg.acknowledge();
        }
      }        
    } finally {
      try { if( null != receiver ) receiver.close(); } catch( Exception ex ) {}
      try { if( null != session  ) session.close();  } catch( Exception ex ) {}
      try { if( null != connect  ) connect.close();  } catch( Exception ex ) {}
      try { if( null != ctx      ) ctx.close();      } catch( Exception ex ) {}
    }

Open in new window

0
 

Author Comment

by:texastemplar
ID: 21780665
Im trying to slow down the queue so it doesnt send all of the messages to the receivers at once. Possibly a pause between each message or have the queue pass a certain number of messages, then pause, then pass more messages. Im also using Spring to interact with the queues. Can you throttle the queue using Spring? Im thinking that maybe there is a parameter I can place in the spring.xml config file to limit the speed that the queue passes messages. Perhaps there is some spring code I can use to slow things down?
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 21781725
The only thing I can point to are these websites. There is a configuration example in "19.6 JMS Namespace Support" before "Table 19.3. Attributes of the JMS <jca-listener-container/> element" where you can set a prefetch parameter. With this parameter the consumer prefetches the given number of messages and waits for acknowlege (if a TransactionManager is set).

http://static.springframework.org/spring/docs/2.5.x/reference/jms.html#jms-jca-message-endpoint-manager
http://static.springframework.org/spring/docs/2.5.x/reference/jms.html#jms-namespace

I hope this helps. Otherwise I see no possibility to slow things down with spring.

manuel
0
 

Author Comment

by:texastemplar
ID: 21806858
I solved my problem. There is a parameter you can set in the messagelistener container defined in the spring xml file called concurrentConsumers. This limits the number of consumers. Then in each consumer use thread.sleep to slow its consumption of messages.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question