Coding Challenges

http://codingbat.com/
https://projecteuler.net/
http://coderbyte.com/

List of Sites
http://codecondo.com/coding-challenges/

https://www.codeeval.com/
http://www.topcoder.com/
http://www.programmr.com/
http://www.codechef.com/

-0-

My suggestion:
Without learning fundamentals, doing all monkey business is useless.

-o-

Java is Good

http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

I thought of learning new Language ….Finally decided to stay back with my Java/PERL.

-o-

Java JMX Client


import java.io.IOException;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

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

public class JMXClient {
public static void main(String[] args) {
try {
HashMap env = new HashMap();

String[] credentials = new String[] { "userid", "password" };
env.put("jmx.remote.credentials", credentials);

JMXServiceURL url = new JMXServiceURL(
"service:jmx:rmi:///jndi/rmi://server:1234/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
String domains[] = mbsc.getDomains();
for (int i = 0; i < domains.length; i++) {
System.out.println("Domain[" + i + "] = " + domains[i]);
}

listMBeans(mbsc, "ABC:type=TYPENAME,name=name2");

jmxc.close();
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* List all MBeans and their attributes.
*/
public static void listMBeans(MBeanServerConnection server, String path)
throws Exception {
javax.management.ObjectName on = new javax.management.ObjectName(path);
final Set names = server.queryNames(on, null);
for (final Iterator i = names.iterator(); i.hasNext();) {
ObjectName name = (ObjectName) i.next();
System.out.println("Got MBean: " + name);

try {
MBeanInfo info = server.getMBeanInfo((ObjectName) name);
MBeanAttributeInfo[] attrs = info.getAttributes();
if (attrs == null)
continue;
for (int j = 0; j < attrs.length; j++) {
if (attrs[j].isReadable()) {
try {

Object o = server.getAttribute(name, attrs[j]
.getName());
System.out.println("==>" + attrs[j].getName()
+ " = " + o);
} catch (Exception x) {
System.err.println("JmxClient failed to get "
+ attrs[j].getName());
}
}
}
} catch (Exception x) {
System.err.println("JmxClient failed to get MBeanInfo: " + x);
// x.printStackTrace(System.err);
}
}
}

public static void listMBeanAttributes_all(MBeanServerConnection server)
throws IOException {
final Set names = server.queryNames(null, null);
for (final Iterator i = names.iterator(); i.hasNext();) {
ObjectName name = (ObjectName) i.next();
System.out.println("Got MBean: " + name);

try {
MBeanInfo info = server.getMBeanInfo((ObjectName) name);
MBeanAttributeInfo[] attrs = info.getAttributes();
if (attrs == null)
continue;
for (int j = 0; j < attrs.length; j++) {
if (attrs[j].isReadable()) {
try {

Object o = server.getAttribute(name, attrs[j]
.getName());
System.out.println("==>" + attrs[j].getName()
+ " = " + o);
} catch (Exception x) {
System.err.println("JmxClient failed to get "
+ attrs[j].getName());
// x.printStackTrace(System.err);
}
}
}
} catch (Exception x) {
System.err.println("JmxClient failed to get MBeanInfo: " + x);
x.printStackTrace(System.err);
}
}
}
}

#java-jmx, #jmx-client

Java – Graphs and Charts

How to draw graphs and charts

1. If you want to generate bmp, jpg formats better to use

http://www.jfree.org/jfreechart/ Better with Swing or to email images.
http://cewolf.sourceforge.net/new/index.html Better for JSP/Servlet integration.

2. For more detailed research, Excel is best. Create template with graphs and fill the data using Apache POI API.
http://poi.apache.org/

3. For better quick online graphs try following
http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html

http://code.google.com/apis/ajax/playground/?type=visualization#line_chart

http://webdesignledger.com/resources/13-useful-javascript-solutions-for-charts-and-graphs

-o-

#java-graphs-and-charts

Jetty JSP with Java 1.6

Problem statement: JSPs are not working in embedded Jetty.

http://docs.codehaus.org/display/JETTY/Jsp+Configuration

Add following to web.xml file

<servlet id="jsp">
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>logVerbosityLevel</param-name>
        <param-value>DEBUG</param-value>
    </init-param>
    <init-param>
        <param-name>fork</param-name>
        <param-value>false</param-value>
    </init-param>
    <init-param>
        <param-name>keepgenerated</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspf</url-pattern>
    <url-pattern>*.jspx</url-pattern>
    <url-pattern>*.xsp</url-pattern>
    <url-pattern>*.JSP</url-pattern>
    <url-pattern>*.JSPF</url-pattern>
    <url-pattern>*.JSPX</url-pattern>
    <url-pattern>*.XSP</url-pattern>
  </servlet-mapping>

Add following to support JSP with Java 1.6

	<dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jetty</artifactId>
		<version>6.1.26</version>
	</dependency>       
	<dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jetty-util</artifactId>
		<version>6.1.26</version>
	</dependency>
	<dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>servlet-api-2.5</artifactId>
		<version>6.1.14</version>
	</dependency>
	<dependency>
		<groupId>org.mortbay.jetty</groupId>
		<artifactId>jsp-2.1</artifactId>
		<version>6.1.14</version>
	</dependency>
	<dependency>
		 <groupId>org.mortbay.jetty</groupId>
		 <artifactId>jsp-api-2.1</artifactId>
		 <version>6.1.14</version>
	</dependency>
	<dependency>
		<groupId>org.apache.ant</groupId>
		<artifactId>ant</artifactId>
		<version>1.8.1</version>
	</dependency>

#jetty-jsp

Windows Port Issue – Bind: Address Already

Many times we used to see “Bind: Address Already” when running servers like apache, tomcat, jboss, weblogic, …etc in windows.

Step 1: Generally the given port is took over by other process. Find it with following commands.

>netstat -a -n -o (This gives Process ID)
>netstat -an
>netstat -an |find /i “listening”

Step 2: Use task manager and find that process who took over the port.

Step 3: Kill the process or change the configurations of respective application.

Issue resolved.
-o-

#bind-address-already, #netstat, #port, #windows-pid

Performance Monitoring in Production

How to monitor CPU, Disk Space, …etc in Production systems?

How to set up alerts?

Best solution I know is: http://www.hyperic.com

Another tool is: http://www.zabbix.com

#application-performance-monitoring

Quick Webservices

If you want to expose existing API as web services …try this…

http://ws.apache.org/axis/
http://enunciate.codehaus.org/

How to test them?

http://www.soapui.org
http://jakarta.apache.org/jmeter/
http://code.google.com/p/rest-client/

-o-

#apache-axis, #enunciate, #web-services

How to build single JSP page based sites in 4 hours?

Problem Statement: How to build quick web report?

Requirement: The report cotains same story lines with different data from database.

With the help of JSTL, JSP and Display Tag completed coding with out single java file or configuration.

Create basic structure to build the code

>mvn archetype:create -DgroupId=com.abcd.xyz -DartifactId=project_war_name -DarchetypeArtifactId=maven-archetype-webapp

Technologies Used:
Display Tag, JSTL, JSP

Links:
http://www.oracle.com/technology/sample_code/tech/java/codesnippet/jsps/jstlsql.html
http://displaytag.sourceforge.net/1.2/tut_sources.html
http://www.java2s.com/Code/Java/JSTL/JSTLSQLQuery.htm
http://demo.raibledesigns.com/appfuse/demos/users.jsp
HTML Editor: http://www.coffeecup.com/free-editor use to build basic three page layout.

Add these dependencies to pom.xml file


<dependency>
 <groupId>displaytag</groupId>
 <artifactId>displaytag</artifactId>
 <version>1.1</version>
</dependency>

<dependency>
 <groupId>com.oracle</groupId>
 <artifactId>ojdbc</artifactId>
 <version>11.1.0.7.0</version>
</dependency>

<dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>jstl</artifactId>
 <version>1.1.2</version>
</dependency>
<!-- standard.jar -->
<dependency>
 <groupId>taglibs</groupId>
 <artifactId>standard</artifactId>
 <version>1.1.2</version>
</dependency>

Add following code with real data to index.jsp page

<b>Heading of query information</b>
</br>
<sql:query var="results_abcd" dataSource="${dataSource}">
select col1, col2,col3 from table1 where condition1='asdf'
</sql:query>
<display:table name="pageScope.results_abcd.rows" id="table5" >
 <display:column property="col1" sortable="true" />
 <display:column property="col2" sortable="true" />
 <display:column property="col3" sortable="true" />
</display:table>
</br></br>

more jstl examples: http://academic2.strose.edu/Math_And_Science/avitabij/cis455fall07/jstljdbc/notes.htm
Note: in JSTL we need to use escape charecters when required during copy paste query from SQL editor to JSTL code.
Example: where userid like '%%'
in JSTL: where userid like '%\%'

Java – Performance Monitoring

Problem: One method is taking 30 seconds. How can we bring it down to the minimal?

First we need to identify all the code (methods) executed.
Get the time took by each method. So that we can troubleshoot one at a time.

How to do this?

1. Simple solution: Using difference of start time and end time.
We can’t use this always. This is not production quality code and we can’t litter the code with all these methods and calculations.

2. Using API level code
JMON API http://jamonapi.sourceforge.net/

3. Using Monitors. This is best way to test distributed installations.
InfraRED http://infrared.sourceforge.net

  • – –

#java-performance-monitoring