7 Replies Latest reply on Feb 7, 2011 12:30 PM by jcstaff

    How do you empty a queue?

    mrbeagle

      during testing or setting up new jobs I'll typically go through some testing or integration test rounds. Afterwhich I'll want to clear the queues to restart when I'm ready for production? What's the easiest way to empty a queue in hornetq without having to write a client that dequeues all records individualy?

       

      thanks

        • 1. Re: How do you empty a queue?
          hughbragg

          If I were you I'd try to keep development and production seperate. What if you want to carry out further tests after you go live?

           

          There are at least 3 ways to clear the queue

           

          1. I keep a seperate data directory and you couldn just delete it assuming nothing is running and nothing important is in it. The next time you start everything is lost.

          2. Consume everything. Problem is it doesn't reset any counters you are using.

          3. use the jmx console operations to clear the queue. You can use jdk jconsole to access them if you're not using jboss.

          • 2. Re: How do you empty a queue?
            jcstaff

            If all I was doing was moving from test to production, then the removal of the data directory would likely be the easiest way to coldstart (solution #1 above). However, I ran into that same issue within my testing where I needed durable subscriptions dropped when moving from one integration test to the next. Some of the tests know enough and have the classpaths to clean things up themselves (solution #2 above) -- some do not. Since I am running within JBoss, the jmx-console provides a usable way to manage the queues and topics through the UI (solution #3 above). However, that is too manual.

             

            To automate, I wrote a small JMX client with a few HornetQ cleanup commands and integrated that into my Maven pom using the Ant plugin. Once you get one JMX command working, the rest are extremely easy to solve. By integrating the cleanup outside of my tests I was able to provide custom classpaths that did not impact the test's environment.

            • 3. Re: How do you empty a queue?
              mrbeagle

              thanks Jim, any chance you could post some code samples?

              • 4. Re: How do you empty a queue?
                jcstaff

                1. Get a remote interface to the JMX Server

                 

                import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

                import javax.management.ObjectName;

                ...

                private static final String jndiName =
                        System.getProperty("jmx.invoker","jmx/invoker/RMIAdaptor");
                ...

                remote = (RMIAdaptor) jndi.lookup(jndiName);

                 

                2.  Look at the org.hornet page in the jmx-console and determine which bean you want to invoke. Build an ObjectName for that bean. I chose to define HORNETQ_MBEANS, Module, and Type as a static String and enums for the various items.

                 

                protected ObjectName getObjectName(Module module, Type type, String name)
                  throws Exception {
                  return new ObjectName(new StringBuilder(HORNETQ_MBEANS)
                     .append(":module=")
                     .append(module.name())
                     .append(",type=")
                     .append(type.name())
                     .append(",name=\"")
                     .append(name)
                     .append("\"")
                     .toString());

                 

                3. Invoke the adapter with your object, method, and parameters of choice. My Module and Operation below are just enums I created of the various operations I used.

                 

                  ObjectName object = getObjectName(Module.JMS, type, name);
                  Object result = remote.invoke(object,operation.name(), params, signature);

                 

                4. Process the results that come back. They are usually either an Object, Object[], etc. type.

                 

                5. I wrapped the above in a main and invoked from Ant

                 

                <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>${maven-antrun-plugin.version}</version>
                        <executions>
                          <execution>
                            <id>cleanup-durables</id>
                            <phase>post-integration-test</phase>
                            <configuration>
                              <tasks>
                               <echo>************* cleaning up durable subscriptions *************</echo>
                               <java classname="my.mbean.helper.Classname" fork="true">
                                <classpath>
                                <path refid="maven.plugin.classpath"/>
                                </classpath>
                                <arg value="dropAllDurableSubscriptions"/>
                               </java>
                              </tasks>
                            </configuration>
                            <goals>
                              <goal>run</goal>
                            </goals>
                          </execution>
                        </executions>
                            <dependencies>
                ...

                • 5. Re: How do you empty a queue?
                  timfox

                  +1.

                   

                  Or you can send a management message to the management address. Both methods are described in the user manual.

                  • 6. Re: How do you empty a queue?
                    hughbragg

                    @Jim. Will this work for a stand alone hornetq with jndi enabled?

                    • 7. Re: How do you empty a queue?
                      jcstaff

                      The JBoss JMX approach would not (JBoss isn't there). The JMS Management message technique would (guess).

                       

                      Since I had the JBoss JMX approach working first, I did not try the JMS Management message approach. However, the classpath brought in through the standard jboss-as-server clients poms is huge. In hind-sight, I wish I went with the message approach.