6 Replies Latest reply on Feb 14, 2012 9:55 AM by Tom Eicher

    Dynamic Routing by code via HttpRouter without ConfigTree po

    Hans Wolffenbuttel Expert

      Hi,

      The JBoss ESB has various routing possibilities, but no dynamicaly configurated HTTPRouter (as far as I know). I need contentbased message routing without having to declare each and every http-endpoint in the jboss-esb.xml.

      There are two options I see here without any ESB changes:
      - Rewrite HttpRouter class and others to accept Properties instead of a ConfigTree.
      - Rewrite ConfigTree class making it possible to programmaticaly add nodes
      - Instantiate the HttpRouter with a hard coded xmlstring (See code example)

       public Message sendAnswer(Message message) throws ActionProcessingException{
       String responseUrl = (String)message.getBody().get("ResponseUrl");
       String responseOperation = (String)message.getBody().get("ResponseOperation");
       String responseSoapAction = (String)message.getBody().get("ResponseSoapAction");
       String xml =
       "<action class=\"nl.gouwit.esb.actions.HttpDynamicRouter\" name=\"SendAnswerAction\" endpointUrl=\"" + responseUrl + "\""
       + " method=\"POST\" responseType=\"STRING\"\">"
       + "<header name=\"SOAPAction\" value=\""+responseSoapAction+"\"/>"
       + "<header name=\"operation\" value=\""+responseOperation +"\"/>"
       + "</action>";
       try {
       ConfigTree config = ConfigTree.fromXml(xml);
       HttpRouter router = new HttpRouter(config );
       router.process(message);
       }
       catch (ConfigurationException e) {
       throw new ActionProcessingException(e);
       }
       catch (SAXException e) {
       throw new ActionProcessingException(e);
       }
       return message;
       }
      


      This code works, but I prefer have a solution where I do not need to create a config by converting XML. I also don't prefer to write my own httprouter because I want to use the ESB functionality as much as possible. Also I don't want to miss out on any newly added functionality to the ESB.


      Regards,

      Hans

        • 1. Re: Dynamic Routing by code via HttpRouter without ConfigTree po
          Chua Khoon Yong Novice

          hi,

           

          I encounter a similar issue.  I wanted to make the endpointurl parameter configurable during runtime.  This is what i did, please comment if there is a better way.

           

          Basically i used another action class to call the httprouter and within the process method, it is up to me to reconfigure the endpointurl.  I haven't fully test it by getting the endpointurl parameter from the database.

           

          Thank you.

           

          ===========

           

          in jboss-esb.xml, i define this:

           

          <action name="action3" class="org.jboss.soa.esb.samples.quickstart.httpgateway.MyHttpAction">
                                 <property name="endpointUrl" value="http://localhost:8080/Quickstart_http_gateway/http/index/XXXX/yyy?a=1,b=2" />
                                 <property name="method" value="POST" />
                                 <property name="responseType" value="STRING" />
          </action>

           

          ===========

           

          In the action class, i did this, just a change of the parameter a and b:

           

          public class MyHttpAction extends AbstractActionPipelineProcessor {

           

            private HttpRouter myhttp;

            private ConfigTree configTree;

           

              public MyHttpAction(ConfigTree _configTree) {
                configTree = _configTree;
              }

           

              public Message process(Message message) throws ActionProcessingException {

           

                  configTree.setAttribute("endpointUrl",  "http://localhost:8080/Quickstart_http_gateway/http/index/XXXX/yyy?a=3,b=4");      
                   configTree.setAttribute("method", "POST");    
                   configTree.setAttribute("responseType", "STRING");   
                 try {
                
                 myhttp = new HttpRouter(configTree);
                        } catch(Exception e) {   
                           System.out.println(e);
                       }

           

                myhttp.process(message);

           

                    return message;  

           

              }
          }

          • 2. Re: Dynamic Routing by code via HttpRouter without ConfigTree po
            Reto Huber Newbie

            Hi guys

             

            Any final conclusion to this issue? What was your experience?

             

            I’m having the same issue right now and planning to implement my own DynamicHttpRouter based on the HttpClient of apache commons. Similar to the existing HttpRouter, which unfortunately seems not to be extendible.

            Additionally my DynamicHttpRouter has to struggle with fail over URLs. So if I get a negative response I have to try another URL.

             

            Regards,

            Reto

            • 3. Re: Dynamic Routing by code via HttpRouter without ConfigTree po
              Hans Wolffenbuttel Expert

              Initially I wanted to be able to configure JBossESB sole from the database. I have wavered from that point of view because of performance. If you let JBossESB cache your services (by defining them in jboss-esb.xml) you will get a more efficient ESB. My sollution was to define per HTTPRouter a service and build my own multicaster action. This way I can get the routing from the database and have the cache advantage of JBossESB.

               

              For your sollution you might consider to make an action which has a list of predefined (httprouter-) services (by catagory/servicename) and use a serviceInvoker to invoke the services one at a time until failover has succeeded.

               

              Regards,

               

              Hans

              • 4. Re: Dynamic Routing by code via HttpRouter without ConfigTree po
                Reto Huber Newbie

                Hi Hans

                 

                Thank you for your hints. I will consider the solution with the predefined httprouter services. The failover part I still have to implement by my own in a separate action. Or do you know an out of the box solution?

                 

                Anyway, before I can start with all this failover stuff I need a solution for my ServiceInvoker issue, or at least a better understanding :-)

                 

                Regards,

                Reto

                • 5. Re: Dynamic Routing by code via HttpRouter without ConfigTree po
                  Hans Wolffenbuttel Expert

                  If I talk about using a serviceInvoker I mean org.jboss.soa.esb.client.ServiceInvoker as used in many quickstarts. You can create your own action and implement this like:

                   

                  ServiceInvoker invoker = new ServiceInvoker(new Service("category", "service"));
                  Message recievedMessage = invoker.deliverSync(messageToDeliver, timeoutMillis);

                   

                  Regards,

                   

                  Hans

                  • 6. Re: Dynamic Routing by code via HttpRouter without ConfigTree po
                    Tom Eicher Newbie

                    Be careful everybody, the original ConfigTree of your PipelineProcessor must not be changed, since it must be reentrant / threadsafe.

                     

                    I did something like

                     

                        ConfigTree configTree = processorsConfigTree.cloneObj();
                        String endpointUrl = ...
                        configTree.setAttribute("endpointUrl", endpointUrl);

                    HttpRouter r = new HttpRouter(configTree);


                    r.process(msg):

                     

                    and this has been working well for a few millions messages in the last years ;-)

                     

                    Cheers, Tom.