2 Replies Latest reply on Sep 18, 2009 9:40 AM by Chris Baird

    Producer/Consuer design questions

    Chris Baird Newbie

      I'm new to JBoss messaging and messaging in general, so I have some questions about design and implementation.

      My first use of messaging will help me provide reliable, throttled delivery to an external service. The throttling is necessary because of contractual agreements. My throttling is done using the consumer max rate.

      My plan has been as follows:

      1. Use an external instance (not launched from my current app) of the jboss messaging service. I want this to be external because I plan to use it later for more than what I need now.
      2. In my application create what I'm calling a producer handler. This creates a session, a producer object and starts a connection. I get a reference to this handler and send messages to the queue.
      3. Within the same application, I create a consumer that implements Runnable. This consumer has a session and connection that is started. The run method uses a while (true) loop which has consumer.receive in it. From here, I can contact the external service.

      ** Note - the producer and the consumer being in the same application is done to negate other systems from having to change. Those systems need not know I'm using a queue (by writing to the queue themselves). My application can handle the load, so I'm not worried about this being inefficient for my needs.

      So, here are my questions
      1. Reading the section on avoiding anti-patterns, I'm trying to limit the creation of sessions and connections such that I have one set of each for the producer and consumer. Is this right?
      2. I start a connection, but never close it. I could close it on shutdown of the application. Should I be closing connections after every "production" and "consumption"?
      3. As I mentioned, I'm using a Runnable class as my consumer and it is in a constant loop to receive messages (consumer.receive()). Am I misunderstanding how to use a consumer? Is a separate thread used in this way necessary?

      Thanks for any help you can provide
      -Chris

        • 1. Re: Producer/Consuer design questions
          Tim Fox Master

           

          "chrisabaird" wrote:
          I'm new to JBoss messaging and messaging in general, so I have some questions about design and implementation.

          My first use of messaging will help me provide reliable, throttled delivery to an external service. The throttling is necessary because of contractual agreements. My throttling is done using the consumer max rate.

          My plan has been as follows:

          1. Use an external instance (not launched from my current app) of the jboss messaging service. I want this to be external because I plan to use it later for more than what I need now.
          2. In my application create what I'm calling a producer handler. This creates a session, a producer object and starts a connection. I get a reference to this handler and send messages to the queue.
          3. Within the same application, I create a consumer that implements Runnable. This consumer has a session and connection that is started. The run method uses a while (true) loop which has consumer.receive in it. From here, I can contact the external service.

          ** Note - the producer and the consumer being in the same application is done to negate other systems from having to change. Those systems need not know I'm using a queue (by writing to the queue themselves). My application can handle the load, so I'm not worried about this being inefficient for my needs.

          So, here are my questions
          1. Reading the section on avoiding anti-patterns, I'm trying to limit the creation of sessions and connections such that I have one set of each for the producer and consumer. Is this right?


          That sounds fine


          2. I start a connection, but never close it. I could close it on shutdown of the application. Should I be closing connections after every "production" and "consumption"?


          Connections are long lived objects and should be re-used. Think of it like a database connection - you wouldn't open and close a new database connection to execute each query.


          3. As I mentioned, I'm using a Runnable class as my consumer and it is in a constant loop to receive messages (consumer.receive()). Am I misunderstanding how to use a consumer? Is a separate thread used in this way necessary?


          You could avoid this thread by creating a MessageListener and setting it on the MessageConsumer instance - this gives you asynchronous delivery semantics.


          • 2. Re: Producer/Consuer design questions
            Chris Baird Newbie

             

            "timfox" wrote:
            You could avoid this thread by creating a MessageListener and setting it on the MessageConsumer instance - this gives you asynchronous delivery semantics.


            Tim, thanks for the advice!