6 Replies Latest reply on Dec 9, 2011 7:03 PM by shergill

    Transform + Divert usage question

    shergill

      I was looking forward to using a divert on HornetQ to divert messages on a given address to many other addresses. What I would like to do is add some additional properties on the message ONCE it gets diverted. It seems like I can use a transformer class to do just that. My only question is that I would like to add custom properties based on the divert that the message came on. I am not sure how to get that info.

      Example: address.order -> divert it to address.electronicorder, address.bookorder, address.clothorder.

      Now once it gets diverted; I would like to add a property (like "ORDER TYPE") on the message in the transformer.. but I am not sure how I can get the divert that the message came on..

      Any tips?

        • 1. Re: Transform + Divert usage question
          clebert.suconic

          It seems like you need multiple Diverts and one Transformer per target address?

          • 2. Re: Transform + Divert usage question
            jdahlbom

            Clebert has it right, below is my implementation:

            AbstractTransformer

            import org.hornetq.core.server.ServerMessage;

            import org.hornetq.core.server.cluster.Transformer;

             

            public abstract class AbstractTransformer implements Transformer {

             

                protected abstract String getMessageType();

             

                @Override

                public ServerMessage transform(ServerMessage message) {

                    message.putStringProperty(TransformationConstants.IDENTIFYING_PARAMETER, getMessageType());

                    return message;

                }

             

            }

             

            RequestEmailTransformer:

            public class RequestEmailTransformer extends AbstractTransformer {

             

                @Override

                protected String getMessageType() {

                    return TransformationConstants.TYPE_REQUEST_EMAIL;

                }

            }

            TransformationConstants:

            public final class TransformationConstants {

                public static final String IDENTIFYING_PARAMETER = "messageType";

                public static final String TYPE_REQUEST_EMAIL = "requestEmail";

            }

             

            I bundle all the transformers into a jar and deploy that separately.

             

            hornetq-configuration.xml on the sending side of the bridge, which also applies the transformations:

               <diverts>

                    <divert name="email-divert">

                            <address>jms.queue.requestEmail</address>

                            <forwarding-address>jms.queue.storeAndForward</forwarding-address>

                            <transformer-class-name>

                                    my.package.RequestEmailTransformer

                            </transformer-class-name>

                            <exclusive>true</exclusive>

                    </divert>

              </diverts>

             

            hornetq-configuration.xml on the receiving side of the bridge (in jboss 6.1):

               <diverts>

                    <divert name="email-divert">

                            <address>jms.queue.receiverQueue</address>

                            <forwarding-address>jms.queue.requestEmail</forwarding-address>

                            <filter string="messageType='requestEmail'"/>

                            <exclusive>true</exclusive>

                    </divert>

               </diverts>

             

            EDIT: Added diverts with transformations.

            • 3. Re: Transform + Divert usage question
              shergill

              Where are you specifying the transform? I dont see it in your divert configuration. If you were to do it there; do you use the 'Abstract Transformer'?

              • 4. Re: Transform + Divert usage question
                shergill

                My other option is to rebuild HornetQ from source and add this line in the DivertBindings.java

                 

                 

                 

                   public void route(final ServerMessage message, final RoutingContext context) throws Exception

                   {

                      // add this line

                      message.setProperty("DIVERTNAME", routingName);

                      divert.route(message, context);

                   }

                • 5. Re: Transform + Divert usage question
                  jdahlbom

                  Oops, looks like I pasted the wrong end of the bridge configuration.

                   

                  Below is the correct one - also added to my original reply.

                   

                  hornetq-configuration.xml on the sending side of the bridge, which also applies the transformations:

                     <diverts>

                          <divert name="email-divert">

                                  <address>jms.queue.requestEmail</address>

                                  <forwarding-address>jms.queue.storeAndForward</forwarding-address>

                                  <transformer-class-name>

                                          my.package.RequestEmailTransformer

                                  </transformer-class-name>

                                  <exclusive>true</exclusive>

                          </divert>

                    </diverts>

                   

                  AbstractTransformer is not directly used anywhere (obviously, it being abstract), it merely exists to avoid duplicating attribute adding code in my several near-identical transformers (one for every queue).

                   

                  Adding your own diverts to HornetQ binaries is wrong, not to mention unmaintainable.

                  • 6. Re: Transform + Divert usage question
                    shergill

                    Can someone tell me if there is a problem with doing what i'm doing here.

                     

                    I've made this change in the 2.2.5 source (from SVN) and compiled a build. It does exactly what I wanted to do.. but I would like to get some feedback from the developers if this is ok or not.

                     

                    The change is in 'DivertBinding.java'.

                     

                     

                      public void route(final ServerMessage message, final RoutingContext context) throws Exception

                       {

                          message.putStringProperty(SimpleString.toSimpleString("ROUTINGNAME"), getRoutingName());

                          divert.route(message, context);

                       }