-
1. Looking for QueueControl example
clebert.suconic May 2, 2011 2:34 PM (in response to rhinmass)Look at the hornetq distribution: examples/jms/jmx
-
2. Looking for QueueControl example
rhinmass May 2, 2011 2:45 PM (in response to clebert.suconic)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 May 3, 2011 9:09 AM (in response to rhinmass)1 of 1 people found this helpfulIt will be exactly the same except you look up a different class using a different object name
-
4. Looking for QueueControl example
danregan May 9, 2011 7:10 PM (in response to rhinmass)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
* 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 Jun 12, 2011 11:46 AM (in response to danregan)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 Jun 12, 2011 5:56 PM (in response to carl.heymann)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 Jun 13, 2011 1:42 AM (in response to timfox)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 Jun 13, 2011 5:33 AM (in response to carl.heymann)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();
You can then call methods on that to get the actual beans
-
9. Re: Looking for QueueControl example
carl.heymann Jun 13, 2011 6:39 AM (in response to timfox)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 Jun 21, 2011 4:46 PM (in response to timfox)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 Jun 21, 2011 5:14 PM (in response to 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 Jun 21, 2011 10:39 PM (in response to 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 Jun 21, 2011 6:51 PM (in response to rhinmass)Yes... that's a good way to go