mBean 查看ActiveMQ队列中数据数目

wulathink 2014-04-24 04:38:59
我正在用JAVA 写一个读取ActiveMQ队列中数据数目的程序。经过寻找后再网上找到一段这样的程序
beans.xml如下:
<beans>
<bean class="org.springframework.jmx.support.MBeanServerConnectionFactoryBean" id="mbeanServerConnection">
<property name="serviceUrl" value="service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi">
</property></bean>

<bean class="com.sample.QueueSizeCounter" id="queueCounter">
<property name="mBeanServerConnection" ref="mbeanServerConnection">
</property></bean>

</beans>

主要的功能实现程序如下
// A class using the MBeanServerConnection to fetch the attribute from the MBean 
package com.sample;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import org.apache.log4j.Logger;

public class QueueSizeCounter {

private MBeanServerConnection mBeanServerConnection;

private Logger logger = Logger.getLogger(QueueSizeCounter.class);

public Long getQueueSize(String queueName) {
Long queueSize = null;
try {

ObjectName objectNameRequest = new ObjectName(
"org.apache.activemq:BrokerName=localhost,Type=Queue,Destination=" + queueName);

queueSize = (Long) mBeanServerConnection.getAttribute(objectNameRequest, "QueueSize");

return queueSize;
}
catch (Exception e) {
logger.error(e.getMessage());
}
return queueSize;
}

public void setmBeanServerConnection(MBeanServerConnection mBeanServerConnection) {
this.mBeanServerConnection = mBeanServerConnection;
}

}

主程序如下
// Running the Queue Counter 
package com.sample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");// —— POS1——

QueueSizeCounter queueCounter = (QueueSizeCounter) ctx.getBean("queueCounter");

String queueName = "Queue1";
long queueSize = queueCounter.getQueueSize(queueName);

System.out.println("Size of " + queueName + " : " + queueSize);
}
}



但是程序总会报出各种莫名其妙的错误,比如在POS1总会说:
引用
the type org.springframework.beans.BeansException can not be resolved. It is indirectly referenced from required .class files


在网上查找后,有人给出答案说这个的原因是类A的实现需要用类B,但是我只import了类A没有importy类B。但是我把这一句改成
ApplicationContext ctx = new ClassPathXmlApplicationContext();
后这一句就没有这样的错误了,错误又跑到其他地方去了……

在spring官网上看ClassPathXmlApplicationContext的构造函数就是这样写的啊:
ClassPathXmlApplicationContext(String A)

请问是哪里的原因呢?
...全文
74 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧