//////////////////////////////////////begin/////////////////////////////////////////////////////
public class JmxInvocationClient {
private static String queueName = "CND_00_21_CC_C3_06_20_";
private String strHostAndPortOfJmxUrl = "localhost:1099";
private String strJmxUrl = null;
private String strMbeanName = null;
private static boolean enableDebug = false;
private static String strJmxUrlFromSystem = null;
private static final String LOCAL_BROKER_NAME = "LOCAL_BROKER_NAME";
private static final String LOCAL_JMX_URL = "LOCAL_JMX_URL";
private static String jmxUser;
private static String jmxPassword;
private JMXServiceURL jmxServiceUrl;
private boolean jmxUseLocal = false;
private JMXConnector jmxConnector;
private MBeanServerConnection jmxConnection;
public static Log logger = LogFactory.getLog(JmxInvocationClient.class);
static {
enableDebug = Boolean.parseBoolean(System.getProperty("IS_DEBUG"));
strJmxUrlFromSystem = System.getProperty(LOCAL_JMX_URL);
queueName = queueName + System.getProperty(LOCAL_BROKER_NAME);
jmxUser = System.getProperty("emq.jmx.user");
jmxPassword = System.getProperty("emq.jmx.password");
}
public JmxInvocationClient() {
}
public JmxInvocationClient(final String paramHostAndPortOfJmxUrl) {
this.strHostAndPortOfJmxUrl = paramHostAndPortOfJmxUrl;
}
private void initParam() {
// 构造jmx url
StringBuffer sb = new StringBuffer(512);
sb.append("service:jmx:rmi:///jndi/rmi://");
sb.append(strHostAndPortOfJmxUrl);
sb.append("/jmxrmi");
this.strJmxUrl = sb.toString();
sb = null;
// 构造mbean的过滤名称
StringBuffer sbForMbean = new StringBuffer(512);
sbForMbean.append(",Type=Queue,Destination=");
sbForMbean.append(queueName);
this.strMbeanName = sbForMbean.toString();
sbForMbean = null;
}
private void initConn(final String paramJmxUrl) {
try {
this.jmxServiceUrl = new JMXServiceURL(paramJmxUrl);
} catch (MalformedURLException mue) {
mue.printStackTrace();
}
}
/**
* @return JMX service url
*/
private JMXServiceURL getJmxServiceUrl() {
return jmxServiceUrl;
}
/**
* Create a JMX connector
*
* @return created JMX connector
* @throws IOException
*/
private JMXConnector createJmxConnector() throws IOException {
// Reuse the previous connection
if (jmxConnector != null) {
jmxConnector.connect();
return jmxConnector;
}
// Create a new JMX connector
// Decide whether need security authentication or not
if (jmxUser != null && jmxPassword != null) {
Map
props.put(JMXConnector.CREDENTIALS, new String[] { jmxUser,
jmxPassword });
jmxConnector = JMXConnectorFactory.connect(this.getJmxServiceUrl(),
props);
} else {
jmxConnector = JMXConnectorFactory.connect(this.getJmxServiceUrl());
}
return jmxConnector;
}
/**
* Close the current JMX connector
*/
private void closeJmxConnection() {
try {
if (jmxConnector != null) {
jmxConnector.close();
jmxConnector = null;
}
} catch (IOException e) {
logger.error("An exception occurs when closing the JMX connector:");
e.printStackTrace();
}
}
/**
* Create a JMX connection
*/
private MBeanServerConnection createJmxConnection() throws IOException {
if (jmxConnection == null) {
if (isJmxUseLocal()) {
jmxConnection = ManagementFactory.getPlatformMBeanServer();
} else {
jmxConnection = createJmxConnector().getMBeanServerConnection();
}
}
return jmxConnection;
}
/**
* @param paramMsgId
* message id
* @return 0:no operate;1:success;2:failure;3:error;
*/
public int deleteMsgFromQueue(final String paramMsgId,
final boolean isJmxFromSystem) {
int flag = 0;
if (paramMsgId == null || paramMsgId.trim().length() == 0) {
return flag;
}
this.initParam();
this.initConn(isJmxFromSystem ? strJmxUrlFromSystem : this.strJmxUrl);
try {
this.createJmxConnection();
// 1.动态获取mbean的objectname
String strObjectName = "";
Iterator iter = this.jmxConnection.queryMBeans(null, null)
.iterator();
for (; iter != null && iter.hasNext();) {
ObjectInstance oi = (ObjectInstance) iter.next();
if (enableDebug) {
System.out.println("=Mbean ObjectName is:="
+ oi.getObjectName() + "=end=");
}
if (oi.getObjectName() != null
&& oi.getObjectName().toString().indexOf(
this.strMbeanName) >= 0) {
strObjectName = oi.getObjectName().toString();
break;
}
}
// 2.构造mbean对象
ObjectName mbeanName = new ObjectName(strObjectName);
// 3.得到proxy代理后直接调用的方式
QueueViewMBean proxy = (QueueViewMBean) MBeanServerInvocationHandler
.newProxyInstance(this.jmxConnection, mbeanName,
QueueViewMBean.class, false);
try {
if (proxy.removeMessage(paramMsgId)) {
flag = 1;
} else {
flag = 2;
}
} catch (Exception e) {
flag = 3;
// e.printStackTrace();
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (MalformedObjectNameException mone) {
mone.printStackTrace();
}
this.closeJmxConnection();
return flag;
}
public static void main(String[] args) {
JmxInvocationClient test = new JmxInvocationClient();
if (false) {
String strMsgId = "111";
int result = test.deleteMsgFromQueue(strMsgId, false);
System.out.println("=result is:=" + result + "=end=");
}
}
public boolean isJmxUseLocal() {
return jmxUseLocal;
}
public void setJmxUseLocal(boolean jmxUseLocal) {
this.jmxUseLocal = jmxUseLocal;
}
}
//////////////////////////////////////end/////////////////////////////////////////////////////