11 Replies Latest reply on Feb 26, 2014 10:14 AM by clebert.suconic

    user-defined filter class instead of just filter expression

    ulf.schroeter

      Hi, I am new to HornetQ, so maybe a dump question: Is there a way to use a user-defined java class as filter for HornetQ queues, bridges and diverts (comparable to transformer class for bridges) ?

       

      In the documentation I just found availability of filter expressions, but for our use-case this is unfortunately not sufficient as our filter criterias require more complex or dynamic processing.

       

      If such a feature is not available yet, I would like to propose addition of such a feature (e.g. via some tag <filter-class-name> in addition to the existing tag <filter>) as this would allow very powerful and flexible filter customizations. In best case optional user-defined filter class can both be used combined with or without <filter> expression condition.

       

      I would expect that required implementation efforts are quite minimal (check for filter expression, if defined normal filter processing + check, if user filter class is defined. If yes, invoke filter class with message, check filter return value and drop message if false. If no, do nothing).  

       

      Thanks

      Ulf    

        • 1. Re: user-defined filter class instead of just filter expression
          jbertram

          Filters currently can only be implemented via expressions.  Filter classes are not supported.

           

          Can you elaborate on your use-case a bit more?  What specifically are you trying to filter that can't be accomplished with an expression?  I assume you want to filter based on the message content rather than the headers or properties.  If so, you might be able to implement what you're looking for with remoting interceptors which can be quite powerful.

          1 of 1 people found this helpful
          • 2. Re: user-defined filter class instead of just filter expression
            ulf.schroeter

            Justin, thanks for the quick reply ! Just as an example we would like to implement server-side geo-referenced message filtering for moving message consumers (e.g. vehicles, ships).

             

            The central server handles very huge amounts of geo-referenced (e.g. via message properties longitude and latitude) messages 'covering' the whole world.

             

            Each moving message consumer is only interested in messages within a specific radius R (lets say 10 km) around its current geographic location. The current location will be send by all moving message consumers to the server e.g. each minute and will be stored within some singleton service (e.g. location manager).

             

            Each moving message consumer has its own specific message queue on the server. Based on the dynamic message consumer locations the server should then automatically filter all incomming world-wide messages on consumer-specific queue insert.

             

            The consumer-specific message filtering requires dynamic calculation of the geographic distance between geo-reference of each message and current consumer location.

             

            Therefore latest consumer latitude/longitude information has to be gathered dynamically from the above mentioned location manager on each filter invocation. The both message and consumer latitude/longitude coordinates have to be converted to cartesian coordinates, doing of some vector math for distance calculation and finally testing the distance against the configured consumer radius-of-interest. (so nothing which can be done easily with some filter expression, but can be coded straight-forward within a user-defiend filter class)

             

            I hope this illustrates one use-case requiring more complex and flexible filtering, there are for sure endless others. User-defined filter classes would be a very elegant and powerful solution to this problem and should fit nicely into the already generalized HornetQ filter concept for queues, bridges and diverts.

            • 3. Re: Re: user-defined filter class instead of just filter expression
              jbertram

              Thanks for the explanation.  It makes sense, and I certainly think the feature request is viable.  Please raise a JIRA at HornetQ - JBoss Issue Tracker and assign it to me.  Unfortunately I can't provide an ETA for this feature as I currently have other pressing work on the code-base related to cloud use-cases.  We always welcome contributions, though.  Feel free to send a PR to https://github.com/hornetq/hornetq.

               

              Aside from a dedicated feature for this functionality, I think you might be able to improvise what you want using remoting interceptors.  For example, I think something like this would probably work:

               

                private class MyInterceptor implements Interceptor
                {
                    public boolean intercept(final Packet packet, final RemotingConnection connection) throws HornetQException
                    {
                      if (packet.getType() == PacketImpl.SESS_SEND)
                      {
                          SessionSendMessage p = (SessionSendMessage) packet;
                          ServerMessage sm = (ServerMessage) p.getMessage();
                          String address = sm.getAddress().toString();
              
                          if (address.equals("red"))
                          {
                            // arbitrary logic
                          }
                          else if (address.equals("green"))
                          {
                            // more arbitrary logic
                          }           
                      }
                      return true;
                    }
                }
              

               

              If you want to "filter out" the message (i.e. prevent it from going to the queue) simply return false.  Otherwise return true.  Read more about remoting interceptors at Chapter 47. Intercepting Operations.

              • 4. Re: user-defined filter class instead of just filter expression
                ataylor

                moved to correct forum

                • 5. Re: user-defined filter class instead of just filter expression
                  ulf.schroeter

                  Ok, will add a feature request. Thank you very much for the excellent support !

                  • 6. Re: user-defined filter class instead of just filter expression
                    jbertram

                    I'm curious to know if you're going to try implementing what you need using remoting interceptors.  It will probably be awhile before I can implement this feature.

                    • 7. Re: user-defined filter class instead of just filter expression
                      ulf.schroeter

                      Thought to follow your invitation for extending the HornetQ source on my own and providing a Git PR. Already downloaded the source and realized that some parts of HornetQ come from the old JBoss 3.x messaging code base where I was already engaged in JBoss open-source development (can remember that we implemented the initial version of the message counter feature 10 years ago...). At the moment I am still struggeling a little bit with Git handling (used to CVS/SVN) and HornetQ project setup in Eclipse, but hopefully I will get it up and runnig.         

                      • 8. Re: user-defined filter class instead of just filter expression
                        ulf.schroeter

                        Is there somewhere a tutorial how to import HornetQ maven project to Eclipse Kepler ? Tried to import HornetQ root directory (as downloaded from forked user-specific HornetQ on GitHub) via "File->Import...->Existing Maven Project", but there are errors on import and imported projects finally show all kinds of compile errors within Eclipse package explorer...   

                        • 9. Re: user-defined filter class instead of just filter expression
                          jbertram

                          There is no such tutorial.  All of the core developers use IntelliJ IDEA.  I haven't used Eclipse regularly in a few years now.

                           

                          As far as Maven goes, everything builds just as it should so I would expect Eclipse would be able to import everything appropriately.  I do fresh imports of HornetQ branches all the time using IntelliJ IDEA and everything works great.

                          • 10. Re: user-defined filter class instead of just filter expression
                            clebert.suconic

                            You usually have to build it first before importing the project.

                            • 11. Re: user-defined filter class instead of just filter expression
                              clebert.suconic

                              Right, you are the author of MessageCounter for instance!

                               

                              Ulf Schroeter wrote:

                               

                              Thought to follow your invitation for extending the HornetQ source on my own and providing a Git PR. Already downloaded the source and realized that some parts of HornetQ come from the old JBoss 3.x messaging code base where I was already engaged in JBoss open-source development (can remember that we implemented the initial version of the message counter feature 10 years ago...). At the moment I am still struggeling a little bit with Git handling (used to CVS/SVN) and HornetQ project setup in Eclipse, but hopefully I will get it up and runnig.