Thursday, January 21, 2010

How to apply a patch

1). Take the svn diff of the changes you made and save it to a file.
$svn diff > ~/Desktop/carbon.patch021

2). Apply the saved patch to the source.
$patch -p 0 < ~/Desktop/carbon.patch021

3). Commit the changes.
$svn commit

Wednesday, January 20, 2010

Access JMX MBean information through RMI

This is a follow up to my earlier post.
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.management.remote.JMXServiceURL;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.*;
import javax.management.openmbean.CompositeData;
import java.lang.management.ManagementFactory;
import java.net.MalformedURLException;
import java.io.IOException;
import java.util.Set;

public class JmxReport {
private static final Log log = LogFactory.getLog(JmxReport.class);

public static void logJmxInfo() {
// Operating System MXBean
log.info("Operating system architecture : " + ManagementFactory.getOperatingSystemMXBean().getArch());
log.info("Available processors : " + ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors());
log.info("Operating system name : " + ManagementFactory.getOperatingSystemMXBean().getName());
log.info("System load average : " + ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage());
log.info("Operating system version : " + ManagementFactory.getOperatingSystemMXBean().getVersion());

// Memory MXBean
log.info("Heap memory usage : " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().toString());
log.info("Non-heap memory usage : " + ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().toString());

// Class loading MXBean
log.info("Loaded class count : " + ManagementFactory.getClassLoadingMXBean().getLoadedClassCount());
log.info("Total loaded class count : " + ManagementFactory.getClassLoadingMXBean().getTotalLoadedClassCount());
log.info("Unloaded class count : " + ManagementFactory.getClassLoadingMXBean().getUnloadedClassCount());
}

public static void getJmxStatisticsViaRmi() {
try {
String jmxAgentName = "org.apache.synapse";
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

JMXConnector c = JMXConnectorFactory.newJMXConnector(createConnectionURL("heshan-laptop", 1099), null/*map*/);
c.connect();
Object o = c.getMBeanServerConnection().getAttribute(new ObjectName("java.lang:type=Memory"), "HeapMemoryUsage");
CompositeData cd = (CompositeData) o;
System.out.println(cd.get("committed"));

Object object2 = c.getMBeanServerConnection().getObjectInstance(new ObjectName("org.apache.synapse:Type=Transport,ConnectorName=nio-http-listener"));
Object lastResetTime = c.getMBeanServerConnection().getAttribute(new ObjectName("org.apache.synapse:Type=Transport,ConnectorName=nio-http-listener"), "LastResetTime");
System.out.println("lastResetTime : " + lastResetTime.toString());

ObjectName objName = new ObjectName("org.apache.synapse:Type=Transport,ConnectorName=nio-http-listener");
Set set2 = mbs.queryNames(objName, null);
for (Object o2 : set2) {
log.info(" :: " + o2.toString());
}
} catch (MalformedObjectNameException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InstanceNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (MalformedURLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ReflectionException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (AttributeNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (MBeanException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}

private static JMXServiceURL createConnectionURL(String host, int port) throws MalformedURLException {
JMXServiceURL serviceURL = new JMXServiceURL("rmi", "", 0, "/jndi/rmi://" + host + ":" + port + "/synapse");
/* To monitor JMX info of Carbon Platform need to enable jmx port @ carbon.xml
The link is service:jmx:rmi:///jndi/rmi://10.100.1.143:9999/server
*/
return serviceURL;
}

public static void main(String args[]) {
getJmxStatisticsViaRmi();
}
}

Programmetically access JMX MBean information


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.lang.management.ManagementFactory;

public class JmxReport {
private static final Log log = LogFactory.getLog(JmxReport.class);

public static void logJmxInfo() {
// Operating System MXBean
log.info("Operating system architecture : " + ManagementFactory.getOperatingSystemMXBean().getArch() );
log.info("Available processors : " + ManagementFactory.getOperatingSystemMXBean().getAvailableProcessors() );
log.info("Operating system name : " + ManagementFactory.getOperatingSystemMXBean().getName() );
log.info("System load average : " + ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage() );
log.info("Operating system version : " + ManagementFactory.getOperatingSystemMXBean().getVersion() );

// Memory MXBean
log.info("Heap memory usage : " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().toString() );
log.info("Non-heap memory usage : " + ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage().toString() );

// Class loading MXBean
log.info("Loaded class count : " + ManagementFactory.getClassLoadingMXBean().getLoadedClassCount() );
log.info("Total loaded class count : " + ManagementFactory.getClassLoadingMXBean().getTotalLoadedClassCount() );
log.info("Unloaded class count : " + ManagementFactory.getClassLoadingMXBean().getUnloadedClassCount() );


}

public static void main(String args[]) {
logJmxInfo();
}
}

Resolving permission issue with Tomcat

When you download and start running catalins.sh, you might get following error.

The BASEDIR environment variable is not defined correctly
This environment variable is needed to run this program


To resolve this;
1) Check $CATALINA_HOME and $JAVA_HOME set properly.

2) Check whether the relevant files in $CATALINA_HOME/bin have necessary permissions required for execution.

$ chmod u+x catalina.sh
$ chmod +x ./setclasspath.sh
$ chmod +x ./startup.sh
$ chmod +x ./shutdown.sh