7 Replies Latest reply on Sep 27, 2012 10:19 AM by Andy Taylor

    Using the same Queue to send different data types

    halfpad Newbie

      Hi

       

      I'm evaluating HornetQ as a possible messaging system for my application. Currently I'm reading http://www.eaipatterns.com/ to learn more about implementing a messaging system. In the books it mentions that different channels should be created for each object type.

       

      Unfortunately I have quite a few different types of data I want to send to multiple clients using their own queues. This ads up to quite a few queues if I follow that design pattern.

       

      Is it realy necessary to create different queues for each object type using HornetQ? And if it isn't, what is the best way to determine the type of the object once I've converted the ByteStream to an Object?

       

      Thanks!

        • 1. Re: Using the same Queue to send different data types
          Andy Taylor Master

          it makes no difference which message type you use its all handled the same, persisted as bytes, so you can send them all to the same queue. Bear in mind tho that using Object Message will slow things down as its dependant on java serialization.

          • 2. Re: Using the same Queue to send different data types
            halfpad Newbie

            Hi Andy

             

            Thank you for your reply.

             

            I was hoping to find out why the design pattern exists, and what alternatives there are for my situation where I don't want to have that many queues.

             

            I'm using the BodyBuffer to write the Bytes of the Object I'm sending. The problem I was having was when receiving the message and constructing an Object from the ByteArrayInputStream, I did not want to have a bunch of "instanceof" checks to dermine which Object I received. But I assume the only alternative is to have separate queues for each object type?

            • 3. Re: Using the same Queue to send different data types
              Andy Taylor Master

              I was hoping to find out why the design pattern exists, and what alternatives there are for my situation where I don't want to have that many queues.

              you dont have to have that many queues, you can use 1

               

              I'm using the BodyBuffer to write the Bytes of the Object I'm sending. The problem I was having was when receiving the message and constructing an Object from the ByteArrayInputStream, I did not want to have a bunch of "instanceof" checks to dermine which Object I received. But I assume the only alternative is to have separate queues for each object type?

              well thats really a design choice for your application, if your application is sending lots of different types of message being handled by the same consumer maybe your design is flawed

              • 4. Re: Using the same Queue to send different data types
                Justin Bertram Master

                I assume you're talking about the "Datatype Channel" pattern outlined here.  This pattern is addressing the question, "How can the application send a data item such that the receiver will know how to process it?"  Using a typed channel is one way to do it.  Using a bunch of instanceof checks would be another way to do it.  Why are you opposed to a bunch of instanceof checks?  The expressions are simple and readable.

                • 5. Re: Using the same Queue to send different data types
                  halfpad Newbie

                  Thank you for your reply Justin.

                   

                  You are indeed correct, the "Datatype Channel" was the one I was looking at.

                   

                  I must confess that I was following the all-instanceof-checks-are-bad crowd, and was hoping someone would suggest an alternative pattern.

                   

                  Until someone suggests another solution, I'll stick to the "instanceOf" or "getClass" options.

                  • 6. Re: Using the same Queue to send different data types
                    Justin Bertram Master

                    If you really wanted to avoid the instanceof checks then you could establish a simple property protocol between the sender and consumer.  For example, set a string property named "type" on each message sent and assign it a value which the consumer can then take and use to cast the object in the message.

                    • 7. Re: Using the same Queue to send different data types
                      Andy Taylor Master

                      If you really wanted to avoid the instanceof checks then you could establish a simple property protocol between the sender and consumer.  For example, set a string property named "type" on each message sent and assign it a value which the consumer can then take and use to cast the object in the message.

                      Or even better, filter on the property and have a consumer deal with each type of message (which is how it should be done, you wouldnt write a method that did 10 different things and have an if statement you would abstract it out)