Tuesday, October 11, 2011

JMX Remote example

In order to inspect / retrieve a JMX bean, when you dont know its name, you can do the following:


JMXServiceURL url = new JMXServiceURL(jmxUrl);
JMXConnector connection = JMXConnectorFactory.connect(url);
MBeanServer mBeanServer = connection.getMBeanServerConnection();

// And there you are:

Set mbeanInstances = mBeanServer.queryMBeans(null, null);

Hashtable> topicStatTable = new Hashtable>();
topicStatTable.put(BEAN_FIELD.InFlightCount, new ArrayList());
topicStatTable.put(BEAN_FIELD.QueueSize, new ArrayList());
Pattern compiledPattern = Pattern.compile("org.apache.activemq:BrokerName=" + brokerName
+ ",Type=Topic,Destination=(.*)");

String separator = " <==> ";

for (ObjectInstance mbeanInstance : mbeanInstances) {

Matcher matcher = compiledPattern.matcher(mbeanInstance.getObjectName().toString());
if (matcher.matches()) {
StringBuilder buffer = new StringBuilder();
buffer.append(matcher.group(1));
buffer.append(separator);
MBeanInfo mbeanInfo = mBeanServer.getMBeanInfo(mbeanInstance.getObjectName());
for (MBeanAttributeInfo attrInfo : mbeanInfo.getAttributes()) {
buffer.append(attrInfo.getName() + "="
+ mBeanServer.getAttribute(mbeanInstance.getObjectName(), attrInfo.getName()));
buffer.append(separator);
}
System.out.println(buffer.toString());
}
}




For using the bean, you can look here:






Below is the full example, as given to me by Ophir Radnitz (ex-alphaCSP)
package com.cpu;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import com.cpu.JmxScanner.BEAN_FIELD;
import com.cpu.JmxScanner.TopicStat;
import com.sampullara.cli.Args;
import com.sampullara.cli.Argument;

public class JmxBeanSnapshot {

public static void main(String[] args) throws Exception {
TestCommand tc = new TestCommand();
Args.usage(tc);

Args.parse(tc, args);

String brokerName = tc.brokerName;
String jmxHost = tc.jmxHost;
int jmxPort = tc.jmxPort;

String jmxUrl = "service:jmx:rmi://" + jmxHost + ":" + jmxPort + "/jndi/rmi://" + jmxHost + ":" + jmxPort
+ "/jmxrmi";

System.out.println("jmxHost : " + jmxHost);
System.out.println("jmxPort : " + jmxPort);
System.out.println("brokerNAme : " + brokerName);
System.out.println("jmxUrl = " + jmxUrl);

JmxBeanSnapshot jmxBeanSnapshot = new JmxBeanSnapshot(jmxHost, jmxPort, brokerName);
jmxBeanSnapshot.analyzeTopic();
}

private void analyzeTopic() throws Exception {
Set mbeanInstances = mBeanServer.queryMBeans(null, null);

Hashtable> topicStatTable = new Hashtable>();
topicStatTable.put(BEAN_FIELD.InFlightCount, new ArrayList());
topicStatTable.put(BEAN_FIELD.QueueSize, new ArrayList());
Pattern compiledPattern = Pattern.compile("org.apache.activemq:BrokerName=" + brokerName
+ ",Type=Topic,Destination=(.*)");

String separator = " <==> ";

for (ObjectInstance mbeanInstance : mbeanInstances) {

Matcher matcher = compiledPattern.matcher(mbeanInstance.getObjectName().toString());
if (matcher.matches()) {
StringBuilder buffer = new StringBuilder();
buffer.append(matcher.group(1));
buffer.append(separator);
MBeanInfo mbeanInfo = mBeanServer.getMBeanInfo(mbeanInstance.getObjectName());
for (MBeanAttributeInfo attrInfo : mbeanInfo.getAttributes()) {
buffer.append(attrInfo.getName() + "="
+ mBeanServer.getAttribute(mbeanInstance.getObjectName(), attrInfo.getName()));
buffer.append(separator);
}
System.out.println(buffer.toString());
}
}

}

private MBeanServerConnection mBeanServer;
private String brokerName;

public JmxBeanSnapshot(String jmxHost, int jmxPort, String brokerName) throws Exception {
String jmxUrl = "service:jmx:rmi://" + jmxHost + ":" + jmxPort + "/jndi/rmi://" + jmxHost + ":" + jmxPort
+ "/jmxrmi";

this.brokerName = brokerName;
JMXServiceURL url = new JMXServiceURL(jmxUrl);
JMXConnector connection = JMXConnectorFactory.connect(url);

mBeanServer = connection.getMBeanServerConnection();

}

public static class TestCommand {
@Argument(value = "jmxHost", description = "JMX server host", required = false)
private String jmxHost = "michael";

@Argument(value = "jmxPort", description = "JMX Server Port", required = false)
private Integer jmxPort = 10099;

@Argument(value = "brokerName", description = "Name of the broker")
private String brokerName = "mich_production";

@Argument(value = "action", description = "Action to be taken: queueSize, connection (optional)")
private String action = "queueSize";

}

}


Wednesday, August 31, 2011

Oracle time/date format

Date or time in Oracle are not always displayed in the format you want.
In order to change this:


ALTER SESSION SET NLS_DATE_FORMAT = 'DD/MON/YYYY HH24:MI:SS';
select sysdate from dual;

From then, your session will display the date in the desired format.
For more details on the format syntax:


Here's an example of select dealing with Date format, for select with from and to date


select * from OPBI.prodapp_gen_machine_stats where 
hostname = 'sew3'  and collection_timestamp between to_date('2011/10/02 08:00:00' ,'yyyy/mm/dd hh24:mi:ss') and to_date('2011/10/05 20:00:00','yyyy/mm/dd hh24:mi:ss') order by collection_timestamp


Sunday, November 7, 2010

Adding XML to Javadoc comments

Copied from http://blog.smartkey.co.uk/2008/08/adding-xml-to-javadoc-comments/


Problems with adding comments

Inserting code snippets into Javadoc comments that read well in both the source code and the HTML files produced by Javadoc can be a very fiddly process. Inserting XML snippets into your documentation can make this trickier still. Read on to discover a simple technique that works equally well with both Java and XML code snippets in your Javadocs.


....

/**
* <p>This class should be used as follows:</p>
* <pre>
* {@code
* <bean>
* <property name="propName" value="propValue"/>
* </bean>
* }
* </pre>
*/
public class MockObject {
...

}

Wednesday, January 13, 2010

Very good tutorial introduction to GridGain

Good introduction to Cloud Computing using GridGain.
Very clean, clear ...

From Nikita Ivanov:

"Grid Application in 15 Minutes"
- GridGain - Cloud Development Platform > Screencasts (wyƛwietl w Google Sidewiki)

Wednesday, July 11, 2007

autoexec.bat example

Instead of defining those variables in the MyComputer/Properties/Advanced/EnvironmentVariable menu, just define them in the C:/autoexec.bat file .


set JAVA_HOME=c:\sdk\jdk_1.6.0
set CYGWIN_HOME=C:\dev\frag\bin\win32\cygwin\
REM set CYGWIN_HOME=C:\sdk\cygwin\
set HOME=c:/home/michaelz
set ANT_HOME=c:\sdk\apache-ant-1.6.5\

REM for some reason, I could split this line in several lines ...
REM set PATH=%CYGWIN_HOME%\bin;%PATH%
REM set PATH=%YOUR_KIT%;%PATH%
Publish Post
PATH=%JAVA_HOME%\bin;c:\sdk\cmd;%CYGWIN_HOME%\bin;%ANT_HOME%\bin

Tuesday, July 3, 2007

Read User Input

Here's a simple way to read user input from the standard input (stdin) console:


/**
* Display the given prompt and return the text types by the user (after his first Enter)

* @param prompt text to write in the stdout. Generaly used for asking the user to type something.
* @return userInput - text given by user in the console (stdout)
*/
public static String readUserInput(String prompt) {
System.out.println(prompt + " ( Type to confirm.) ");

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String userInput = "";
try {
userInput = bufferedReader.readLine();
if(log.isInfoEnabled()) {
log.info("Thanks ! ");
}
} catch (IOException ioe) {
log.error("Unable to read user input, Exception : " + ioe.getMessage());
}
return userInput;
}

JVM arguments

  1. JConsole
    Enable jconsole by adding the following:
    -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=7091 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

    The main argument is
    -Dcom.sun.management.jmxremote. It's not required anymore in JDK 1.6 . The other arguments are used for specifying explicitely the port number (7091). Useful for monitoring a JVM from a remote computer.
  2. YourKit
    For enabling YourKIT Java profiler, add
    -agentlib:yjpagent to your JVM argument.
    In addition, the YourKit/bin directory should be added to the path
  3. Remote debug
    For enabling remote debugger, add the following argument to the JVM:
    -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044
  4. JPS for getting all the monitoreable JVM