This week I was interested in collecting statistic from my
application. I have never been doing that and I think a few times, it could be
helpful to resolve some problems. Currently there is other motivation -
from it depends my half year premium.
Ok, so I have my Java application and what's next?
I can measure ex. how is used cache, heap memory, CPU and etc.
How can I get this information?
I
can log it to file or other adapter or I can serve it by JMX MBean.
Logging to file takes hard drive memory and it can be weight. JMX works as JMS service but
it's allow to change some application parameters and is light.
Anyway I started from log statistic in file. What I did?
JVM:
Everything about jvm memory and threads is in class ManagementFactory with static methods. Only you have to dump it to log. MBeans are by default registered in JMX Server too.
Anyway I started from log statistic in file. What I did?
JVM:
Everything about jvm memory and threads is in class ManagementFactory with static methods. Only you have to dump it to log. MBeans are by default registered in JMX Server too.
I didn't find counter of blocked threads and not dead locked, so I prepared my own method to update MBean as below.
ThreadInfo[] infos = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
int blocked = 0;
for(ThreadInfo ti : infos){
if(Thread.State.BLOCKED.equals(ti.getThreadState())){
blocked++;
}
}
mbean.setBlocked(blocked);
Hibernate statistic:
I had to add in configuration parameter hibernate.generate_statistics and than I could get Statistics interface and enable collecting statistic in SessionFactory.
If you need JMX you have to registry Statistic object as MBean in JMX Server.
EHCache statistic
To get EHCache statistics it is required add statistics attribute to every cache container definition.
Then it is possible to get statistics from every cache managers.
int blocked = 0;
for(ThreadInfo ti : infos){
if(Thread.State.BLOCKED.equals(ti.getThreadState())){
blocked++;
}
}
mbean.setBlocked(blocked);
Hibernate statistic:
I had to add in configuration parameter hibernate.generate_statistics and than I could get Statistics interface and enable collecting statistic in SessionFactory.
statistics = sessionFactory.getStatistics();
statistics.setStatisticsEnabled(true);All values are available in Statistics objec.
If you need JMX you have to registry Statistic object as MBean in JMX Server.
EHCache statistic
To get EHCache statistics it is required add statistics attribute to every cache container definition.
statistics="true"
Then it is possible to get statistics from every cache managers.
I noticed when there is a few cache factories, it is needed to set them not shared. Only cache shared with hibernate should be shared. Otherwise I couldn't get to cache manager for hibernate. I saw only one which I defined in spring configuration.
When
I had two different configurations, finally two cache managers. I had
to share only one which was based on hibernate ehcache file. Others
shouldn't be shared otherwise I couldn't get reference to hibernate
cache manager.
By the way I will write how to registry your own MBean on your JVM. What you need is to get JMX service and register your MBean. MBean class have to implement interface with postfix MBean. All method included in interface are available from JMX console. Getters presents values, setters change it and other methods can be executed from jconsole. Example code is shown below.
MyHello mbean = new MyHelloMBean();
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.example:type=Hello");
mbs.registerMBean(mbean, name);
No comments:
Post a Comment