13 Replies Latest reply on Jun 21, 2011 6:51 PM by clebert.suconic

    Looking for QueueControl example

    rhinmass

      Hello,

      Sorry for the multiple posts on this topic.  I would really appreciate an example on how to instantiate an instance of

       

      org.hornetq.api.core.management.QueueControl

       

      I am running JBoss 6.0.0.Final with embedded hornetq.

       

      I see that this is an interface and the actual class is org.hornetq.core.management.impl.QueueControlImpl

       

      but I have not been able to figure out how to get or create one.

       

      Thanks!!

        • 1. Looking for QueueControl example
          clebert.suconic

          Look at the hornetq distribution: examples/jms/jmx

          • 2. Looking for QueueControl example
            rhinmass

            Sorry I wasn't clear (again!)  I'm looking for an example of using the core QueueControl, not JMS.

            • 3. Looking for QueueControl example
              ataylor

              It will be exactly the same except you look up a different class using a different object name

              1 of 1 people found this helpful
              • 4. Looking for QueueControl example
                danregan

                There isn't much example code using the core API.  Here's what I have, based on some code I found referred to in these forums somewhere:

                 

                    synchronized private MBeanServerConnection getMBeanServerConnection(int jmxPort) throws IOException {

                        String jmxUrl = "service:jmx:rmi:///jndi/rmi://" + serverHost + ":" + jmxPort + "/jmxrmi";

                        boolean newConnectorNeeded = false;

                 

                        // We want to re-use JMXConnectors as much as possible, otherwise we

                        // either a) have to close them when we're done with them, and this

                        // complicates things for the user, or b) run the risk of leaking

                        // JMX connectors and a new thread is created on the server each time

                        // we ask for a new one.  To avoid this, we only create one

                        // JMXConnector.  If we find that the current connector is not

                        // connected, we create a new one.

                 

                        if (jmxConnector == null) {

                            newConnectorNeeded = true;

                        }

                        else {

                            // check for a broken connection

                            // we could instead register for a notification, but any JMX objects

                            // that were using the broken connection would have to be recreated

                            // anyways; you cannot re-connect a broken connection.

                            try {

                                String connectionId = jmxConnector.getConnectionId();

                            } catch (IOException e) {

                                newConnectorNeeded = true;

                            }

                        }

                        if (newConnectorNeeded) {

                            jmxConnector = JMXConnectorFactory.connect(new JMXServiceURL(jmxUrl), new HashMap<String, Object>());

                        }

                 

                        MBeanServerConnection mbsc = jmxConnector.getMBeanServerConnection();

                        return mbsc;

                    }

                 

                    /**

                     * Returns a QueueControl

                     * ( http://hornetq.sourceforge.net/docs/hornetq-2.1.2.Final/api/org/hornetq/api/core/management/QueueControl.html )

                     * object, which the management object for a queue.

                     *

                     * @param queueName

                     * @param jmxPort

                     * @return

                     * @throws java.lang.Exception

                     */

                    public QueueControl getQueueControl(String queueName, int jmxPort) throws Exception {

                        QueueControl queueControl = null;

                        try {

                            MBeanServerConnection mbsc = getMBeanServerConnection(jmxPort);

                            ObjectNameBuilder builder = ObjectNameBuilder.DEFAULT;

                            ObjectName queueObjName = builder.getQueueObjectName(new SimpleString(queueName), new SimpleString(queueName));

                            queueControl = (QueueControl) MBeanServerInvocationHandler.newProxyInstance(mbsc, queueObjName, QueueControl.class, false);

                        } catch (Exception e) {

                            // tell the user why they're getting an exception before rethrowing it

                            System.out.println("Make sure you start the server JVM with these options:\n"

                                    + "-Dcom.sun.management.jmxremote.port=" + jmxPort + "\n"

                                    + "-Dcom.sun.management.jmxremote.ssl=false\n"

                                    + "-Dcom.sun.management.jmxremote.authenticate=false");

                            if (!serverHost.equals("localhost")) {

                                System.out.println("You may also need this option: -Djava.rmi.server.hostname=<fqdn>\n");

                            }

                            throw e;

                        }

                        return queueControl;

                    }

                • 5. Re: Looking for QueueControl example
                  carl.heymann

                  This may be a bit late, but I've been experimenting with embedded hornetq myself over the weekend, and was looking for a way to delete messages.

                   

                  If you're running hornetq embedded, then it seems to me that you could just get a reference to a org.hornetq.core.server.Queue (https://github.com/bobmcwhirter/hornetq/blob/vendor/hornetq-core/src/main/java/org/hornetq/core/server/Queue.java) by using HornetQServer.locateQueue(queueName). The Queue interface allows you to directly control the queue, in the same way as QueueControl, but it seems to be part of the core implementation, not the public API. It would be nice if there was a QueueControl implementation for embedded use that avoided JMX.

                  • 6. Re: Looking for QueueControl example
                    timfox

                    If you're in the same VM as the JNDI server, then it's trivial to just look up the QueueControl object directly (one line of code)

                    • 7. Re: Looking for QueueControl example
                      carl.heymann

                      Like this?

                       

                         QueueControl control = (QueueControl)server.getManagementService().getResource(ResourceNames.CORE_QUEUE + queueName);

                       

                      When embedding, I'd like to avoid JNDI. The question is, whether the above is "safe" from an API point of view. The "ResourceNames" class is in the api package, but HornetQServer and ManagementService are not. Then again, I am embedding here. If the method to obtain the QueueControl interface changes in the future, it will be easy to deal with.

                      • 8. Re: Looking for QueueControl example
                        timfox

                        Carl Heymann wrote:

                         

                        Like this?

                         

                           QueueControl control = (QueueControl)server.getManagementService().getResource(ResourceNames.CORE_QUEUE + queueName);

                        If you are running embedded you will either:

                         

                        a) Already have an instance of the MBeanServer server which you passed into the constructor when you created the embedded HornetQ server

                        b) If you used a constructor which doesn't take take the MBeanServer, then the default platform one will be used. You can get a reference to this with:

                         

                        mbeanServer = ManagementFactory.getPlatformMBeanServer();

                         

                        http://download.oracle.com/javase/1,5.0/docs/api/java/lang/management/ManagementFactory.html#getPlatformMBeanServer()

                         

                        You can then call methods on that to get the actual beans

                        • 9. Re: Looking for QueueControl example
                          carl.heymann

                          Nothing (simple?) in MBeanServer gives the underlyning QueueControl instance directly. Everything seems designed around reflection/remote invocation. One can register an mbean, but can't get it back out. I can get the "ObjectInstance" for a QueueControl mbean, but this is just a handle, it doesn't expose the underlying mbean. Then I'm back to instantiating a proxy instance as described in message 4 by Dan Regan.

                           

                          Wouldn't it make sense to expose the QueueControl objects more directly for embedded use? All I want to do, is to have a simple way of deleting certain messages from a queue, without moving too far from the official hornetq API.

                          • 10. Re: Looking for QueueControl example
                            rhinmass

                            Can you help me with the "1 line of code"?  I have the mBeanServer obtained from ManagementFactory.getPlatformMBeanServer.

                             

                            One difficulty I'm having is in determining the ObjectName for the QueueControl object that will get me access to the hornetq.

                             

                            Assuming I could get that, I would then call mBeanServer.getObjectInstance(objectName)  right?

                             

                            That would return an org.jboss.mx.server.ServerObjectInstance. 

                             

                            I then need to convert that to a QueueControl, right? Is that just a cast?

                             

                            QueueControl queueControl = (QueueControl) mBeamServer.getObjectInstance(objectName)  ??

                             

                            I really appreciate yours and everyone's time in answering these endless queries.  Please understand that what is trivial to you, is often a big stumbling block for those of us trying to learn, so any help would be so very much appreciated. Thanks!!

                            • 11. Re: Looking for QueueControl example
                              rhinmass

                              Making a little progress but still stuck.

                               

                              I was able to construct an object name corresponding to the queue I want to control.  I called the mBeanServer and got an ObjectInstance.

                               

                              ObjectInstance objInst = mBeanServer.getObjectInstance(on);

                               

                              I think I just need to convert the objInst to a QueueControl or a QueueControlImpl, and this is where I am stuck. 

                               

                              I get:

                               

                                 org.jboss.mx.server.ServerObjectInstance cannot be cast to org.hornetq.api.core.management.QueueControl

                              • 12. Re: Looking for QueueControl example
                                rhinmass

                                This seems to work: (from http://community.jboss.org/wiki/HowCanAnEJBCallAnMBean)

                                 

                                SimpleString queueId = new SimpleString(queueName);

                                ObjectName on = ObjectNameBuilder.DEFAULT.getQueueObjectName(queueId, queueId); 

                                QueueControl queueControl =MBeanServerInvocationHandler.newProxyInstance(mBeanServer, on, QueueControl.class, false);

                                 

                                My question now is - is this the preferred way to do this?  I'm running in the same VM as the JBoss/JNDI/hornetq (JBoss 6.0.0.Final)

                                • 13. Re: Looking for QueueControl example
                                  clebert.suconic

                                  Yes... that's a good way to go