Created
May 20, 2011 21:25
-
-
Save rponte/983841 to your computer and use it in GitHub Desktop.
ActiveMQJMXUtils - trying to purge a fucking queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package base.jms; | |
import org.apache.activemq.broker.BrokerService; | |
import org.apache.activemq.broker.jmx.QueueViewMBean; | |
import org.apache.activemq.web.BrokerFacadeSupport; | |
import org.apache.activemq.web.LocalBrokerFacade; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class ActiveMQJMXUtils { | |
BrokerFacadeSupport facade; | |
@Autowired | |
public ActiveMQJMXUtils(BrokerService brokerService) { | |
this.facade = new LocalBrokerFacade(brokerService); | |
} | |
public void cleanUp(String queueName) { | |
try { | |
QueueViewMBean queue = facade.getQueue(queueName); | |
if (queue != null) { | |
queue.purge(); | |
long size = queue.getQueueSize(); | |
if (size > 0) | |
throw new IllegalStateException("Fila " + queueName + " não foi limpa!"); | |
} | |
} catch (Exception e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
public long getQueueSize(String queueName) { | |
try { | |
QueueViewMBean queue = facade.getQueue(queueName); | |
return (queue != null ? queue.getQueueSize() : 0); | |
} catch (Exception e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.syspdv.fidelizacao.jms; | |
import javax.management.MBeanServerConnection; | |
import javax.management.MBeanServerInvocationHandler; | |
import javax.management.MalformedObjectNameException; | |
import javax.management.ObjectName; | |
import javax.management.remote.JMXConnector; | |
import javax.management.remote.JMXConnectorFactory; | |
import javax.management.remote.JMXServiceURL; | |
import org.apache.activemq.broker.Broker; | |
import org.apache.activemq.broker.BrokerService; | |
import org.apache.activemq.broker.jmx.BrokerViewMBean; | |
import org.apache.activemq.broker.jmx.QueueViewMBean; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Component; | |
@Component | |
public class ActiveMQJMXUtils { | |
@Autowired | |
BrokerService brokerService; | |
// BUG? https://issues.apache.org/jira/browse/AMQ-2513 | |
public void purge() { | |
try { | |
// // metodo 1 | |
ObjectName queueName = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=syspdvweb.fidelizacao"); | |
QueueViewMBean queueMbean = (QueueViewMBean) brokerService.getManagementContext().newProxyInstance(queueName, QueueViewMBean.class, true); | |
queueMbean.purge(); | |
if (queueMbean.getQueueSize() > 0) | |
throw new RuntimeException("Fila " + queueMbean.getName() + " não foi limpa!"); | |
// metodo 2 | |
ObjectName brokerName = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker"); | |
BrokerViewMBean brokerMbean = (BrokerViewMBean) brokerService.getManagementContext().newProxyInstance(brokerName, BrokerViewMBean.class, true); | |
for (ObjectName name : brokerMbean.getQueues()) { | |
QueueViewMBean bean = (QueueViewMBean) | |
brokerService.getManagementContext().newProxyInstance(name, QueueViewMBean.class, true); | |
bean.purge(); | |
if (bean.getQueueSize() > 0) | |
throw new RuntimeException("Fila " + bean.getName() + " não foi limpa!"); | |
} | |
// metodo 3 | |
brokerMbean.removeQueue("syspdvweb.fidelizacao"); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static void cleanUpAll() { | |
try { | |
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"); | |
JMXConnector jmxc = JMXConnectorFactory.connect(url); | |
MBeanServerConnection conn = jmxc.getMBeanServerConnection(); | |
// ObjectName activeMQ = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Broker"); | |
// BrokerViewMBean mbean = (BrokerViewMBean) MBeanServerInvocationHandler.newProxyInstance(conn, activeMQ,BrokerViewMBean.class, true); | |
// | |
// String operationName="removeQueue"; | |
// String parameter="syspdvweb.fidelizacao"; | |
// if(parameter != null) { | |
// Object[] params = {parameter}; | |
// String[] sig = {"java.lang.String"}; | |
// conn.invoke(activeMQ, operationName, params, sig); | |
// } else { | |
// conn.invoke(activeMQ, operationName,null,null); | |
// } | |
ObjectName name = new ObjectName("org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=syspdvweb.fidelizacao"); | |
QueueViewMBean queueMbean = (QueueViewMBean) | |
MBeanServerInvocationHandler.newProxyInstance(conn, name, QueueViewMBean.class, true); | |
queueMbean.purge(); | |
// for (ObjectName name : mbean.getQueues()) { | |
// QueueViewMBean queueMbean = (QueueViewMBean) | |
// MBeanServerInvocationHandler.newProxyInstance(conn, name, QueueViewMBean.class, true); | |
// | |
// if (queueMbean.getName().equals("syspdvweb.fidelizacao")) { | |
// queueMbean.purge(); | |
// } | |
// } | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Just a tip: you can use the class RemoteJMXBrokerFacade for remote access.
Hi , Can i have a brokerservice instance for the remote broker instead of local host ? I was just trying to get the existing broker service object from the running remote broker , please share me your thoughts on this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's necessary to add
activemq-web-5.x.jar
to classpath!