Wednesday 25 December 2013

Passing JVM options in Mule ESB

After a long and hard search, I finally figured out a way to pass parameters to JVM in Mule ESB. Thought of sharing this for the next restless soul trying to develop things in Mule.
Lets start with a simple introduction of mule, Mule is an ESB (Enterprise Service Bus) meaning it helps connect different components which don’t ordinarily speak the same language and are very difficult to adjust/reprogram.
Whenever we deal with Java and its related products, we often end-up fidgeting with the amount of memory which needs to be allocated to the VM.
Mule being different as it it, has a very different way of passing arguments to the underlying VM. It uses a configuration file found in$MULE_HOME/conf/wrapper.conf
this file contains keys matching the pattern
wrapper.java.additional.<n>
replace the <n> with an integer (ensure to not skip a number in the sequence), the default mule installation comes with 3 configurations, so its safe to start with wrapper.java.additional.4
so the config file needs to be appended with
wrapper.java.additional.4=<<your config>>
Please leave your comments and suggestions to improve this post..

Thursday 26 April 2012

How to get CPU load info from Java

Hi,

I was recently faced with the need to find the CPU load on the server, after a lot of googling I was able to come up with something which i think will be helpful to a lot of people..

public class PerformanceMonitor {
    private int  availableProcessors = getOperatingSystemMXBean().getAvailableProcessors();
    private long lastSystemTime      = 0;
    private long lastProcessCpuTime  = 0;

    public synchronized double getCpuUsage()
    {
        if ( lastSystemTime == 0 )
        {
            baselineCounters();
            return;
        }

        long systemTime     = System.nanoTime();
        long processCpuTime = 0;

        if ( getOperatingSystemMXBean() instanceof OperatingSystemMXBean )
        {
            processCpuTime = ( (OperatingSystemMXBean) getOperatingSystemMXBean() ).getProcessCpuTime();
        }

        double cpuUsage = (double) ( processCpuTime - lastProcessCpuTime ) / ( systemTime - lastSystemTime );

        lastSystemTime     = systemTime;
        lastProcessCpuTime = processCpuTime;

        return cpuUsage / availableProcessors;
    }

    private void baselineCounters()
    {
        lastSystemTime = System.nanoTime();

        if ( getOperatingSystemMXBean() instanceof OperatingSystemMXBean )
        {
            lastProcessCpuTime = ( (OperatingSystemMXBean) getOperatingSystemMXBean() ).getProcessCpuTime();
        }
    }
}