Wednesday, July 28, 2010

Ant

> Ant is a pure Java build tool

> Ant allows the developer to automate the repeated process involved in the development of J2EE application.

> Developers can easily write the script to automate the build process like compilation, archiving and deployment.


> Downloading and Installing Ant
> Set the class path to the bin directory of the ant.

Let's assume that Ant is installed in c:\ant\. The following code has to be put into autoexec.bat file:

set ANT_HOME=c:\ant
set JAVA_HOME=c:\Program Files\Java\jdk1.6.0\
set PATH=%PATH%;%ANT_HOME%\bin

Testing Ant

Go to command prompt and issue the following command.

C:\anttest>Ant

Buildfile: build.xml does not exist!Build failed

C:\anttest>

If every this is installed correctly Ant will give the above message.

Now its time to do some work with Ant.

Ant uses configuration file called build.xml to work. This is the file where you defines the process of compiling, building and deploying.

Writing build.xml file

build.xml is a xml file used by ant utility to compile, build and deploy or run the application.

Now here is code of simple build.xml file which compiles a java file present in src directory and places compiled class file in build/src directory.


<?xml version="1.0"?>

<!-- Build file for our first application -->

< project name="Ant test project" default="build" basedir=".">

< target name="build" >

< javac srcdir="src" destdir="build/src" debug="true"

includes="**/*.java"

/>

</target>

</project>

-------------------------------------------------------------------------
The project tag:


requires three attributes namely name, default and basedir.

Here is the description of the attributes:

name |Represents the name of the project.
----------------------------------------------------
default |Name of the default target to use when no target is supplied.
basedir |Name of the base directory from which all path calculations are done.

All the attributes are required.


—-build1.xml ——–
< project name=”hello” default=”hello”>
< target name=”hello”>
< echo message=”Hello, World”/>
</target>

< target name=”goodbye”>
< echo message=”Goodbye, end of Hello world script”/>
< /target>
< /project>

Monday, July 26, 2010

mysql dump

>mysql -u root -p password

How to backup MySQL database? - using mysqldump -


>mysqldump -u [user] -p [database_name] > [backupfile].dump



dump restore

>mysql [database_name] < [backup_file].dump


drop the schema

>mysqladmin -u root -p drop schema_name


> mv rt3newdump.sql /home/karthik/temp
>mysqladmin -u root -p create rt3
>mysql -u root -p rt3 < rt3newdump.sql


How do I quickly rename a mysql database (change schema name)?

mysqldump -u username -p -v olddatabase > olddbdump.sql
mysqladmin -u username -p create newdatabase
mysql -u username -p newdatabase < olddbdump.sql

Thursday, July 22, 2010

Formatter Class in J2SE 1.5

Java 1.5 introduces a new class named java.util.Formatter that allows you to do string formatting similar to the printf function in C. It depends heavily on the varargs feature being introduced in 1.5

System.out.printf

String initials = "rk";
String comment = "just because";
System.out.printf("reason: %s (noted by %s)", comment, initials);

The example prints the following output on the console:

reason: just because (noted by rk)

----------------
String.format

If you only want to obtain a formatted string, but not print it, you can use the static method format on the String class. Here's an example that also demonstrates a few numeric conversions:

int a = 65;
String s =
String.format("char: %c integral: %d octal: %o hex: %x %n",
a, a, a, a);

The %n at the end of the format string indicates a platform-specific line separator. When printed, the String s looks like this:

char: A integral: 65 octal: 101 hex: 41

Numeric conversions also support flags for padding, grouping, justification, and sign.
-----------------------
Dates

Formatter provides an extensive number of date-related conversions. The following code:

String.format("%1$td %1$tb %1$ty", new Date())

produces a string with the value:

26 Feb 04

-----------------------------------------
java.util.Formatter

The Formatter class is at the core of the new formatting capability. It fully supports internationalization by letting you pass a Locale to the constructor; the other formatting methods (e.g. String.format) also allow this.

Formatter also lets you pass an Appendable object. This is a new interface that defines append methods so that the formatter can store its results in a text collector such as a stream object. Sun has modified all relevant Java classes, such as StringBuffer and PrintStream, to implement this interface. Here's how you might use it:

double avogadro = 6.0e23;
StringBuffer buffer = new StringBuffer();
Formatter formatter = new Formatter(buffer, Locale.US);
formatter.format("avogadro's number: %e %n", avogadro);
formatter.format("base of the natural log: %e %n", Math.E);
System.out.println(buffer.toString());

The corresponding output:

avogadro's number: 6.000000e+23
base of the natural log: 2.718282e+00

Web Server Benchmarking

1.Web site load analysis
2.performance tests

Web server benchmarking is useful in testing your infrastructure to see if it can with stand expected visitor growth and maintain a reasonable response under load
(i.e. requests per sec, latency, bandwidth).

>>Apache Benchmark Tool: ab
>>Benchmarking Tool: httperf

Apache Benchmark Tool: ab

The Apache httpd web server comes with a benchmarking tool to simulate a high load and to gather data for analysis.

>ab -n 1000 -c 10 -g test_data.txt http://www.karthik.com/index.html

Option Description
-n number of requests. Default is 1 which is useless.
-c concurrent requests. Default 1
-g GNU plot output. Labels are on first line of output.
-q Suppress progress stattus output to stderr.
-t Time limit. Maximum number of seconds.
-A username:password Specify authentication credentials.
-X proxy[:port] Specify a proxy server.

Console output:
Finished 1000 requests


Server Software: Apache/2.2.3
Server Hostname: www.karthik.com
Server Port: 80

Document Path: /index.html
Document Length: 83241 bytes

Concurrency Level: 10
Time taken for tests: 14.793312 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 83608000 bytes
HTML transferred: 83241000 bytes
Requests per second: 67.60 [#/sec] (mean)
Time per request: 147.933 [ms] (mean)
Time per request: 14.793 [ms] (mean, across all concurrent requests)
Transfer rate: 5519.25 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 20 250.2 0 3000
Processing: 53 126 52.7 120 317
Waiting: 19 51 29.5 46 246
Total: 53 147 260.2 120 3305

Percentage of the requests served within a certain time (ms)
50% 120
66% 142
75% 159
80% 172
90% 198
95% 227
98% 282
99% 314
100% 3305 (longest request)

This shows the load limit of the server.
---------------------------------------------------------------------------------

Benchmarking Tool: httperf


Web performance benchmarking tool httperf. Httperf sends requests to the web server at a specified rate and gathers stats. Increase till one finds the saturation point.
Installation:

* apt-get install httperf

Example usage:

* Print performance stats for home page of your-domain.com: httperf --hog --server www.your-domain.com
* Create 100 connections at a rate of 10/sec: httperf --hog --server http://www.your-domain.com/ --num-conn 100 --rate 10 --timeout 5
* Generate 10 sessions at a rate of one session/sec every 2 seconds: httperf --hog --ser=www --wsess=10,5,2 --rate 1 --timeout 5

httperf command line options:

Command Command Description
--hog Use as many TCP ports as necessary to generate stats (else limited to port 1024-5000)
--num-calls Session oriented workloads.
--max-connections=# Limit the number of connections to that specified.
--num-calls=# Specify the number of calls to issue on each connection before closing it.
--server host-name Default localhost. Specify IP address of host name.
--wsess=N1,N2,X Specify session where
N1: number of sessions
N2: number of calls per session
X: delay between calls (sec)
--timeout Stop if there is no response within timeout period.

Wednesday, July 21, 2010

Java 6 core package

Packages are usually defined using a hierarchical naming pattern, with levels in the hierarchy separated by periods (.) (pronounced "dot"). Although packages lower in the naming hierarchy are often referred to as "subpackages" of the corresponding packages higher in the hierarchy, there is no semantic relationship between packages.

Monday, July 19, 2010

Unit Tests - "Written Once and Forgotten Forever"

Unit test cases that are written once and forgotten for ever, with all the dataset/environmental dependencies in it. What is the importance of dataset/environment? Let's take an example, observe the test case below.

public void testEmpFinder () {
//Weird Test case for Fun!!!
String result = "JOHN";
//Passing employee id returns employee object.
//verify the name matches.

Employee emp = EmpFinder.find(1);
assertEquals(emp.getName,result);
}

What's wrong? The developer had made an assumption, that on querying with employeeid='1' will return employee with the name "JOHN". The data could be coming from a database table "EMPLOYEE". But It’s very evident this test case would fail if run on an environment where the employeeid='1' data doesn't exists. This makes the test cases obsolete the moment they are written.

java first

Java is a popular and widely used programming language. It was originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems’ Java platform. The language’s syntax has much in common with C and C++, but its object model is simpler and has less low-level facilities. Undoubtedly, one of Java’s strong points is an automatic memory management. The developer determines when objects are created, but it’s a runtime task to free memory, when objects are no longer in use.

java Tutorials

>>downloads and installation(jdk and any IDE(netbeans or eclipse)
>>Hello World


download

First of all you have to download some stuff from the Internet:

1.Download the latest JDK from: http://java.sun.com/javase/downloads/index.jsp
2.Download Eclipse IDE for Java Developers from: http://www.eclipse.org/downloads/


hello world

HelloWorld.java

public class HelloWorld {
/**

* @param args

*/

public static void main(String[] args) {

System.out.println("Hello World! I am new to Java.");

}

}

First of all, Java is an object-oriented programming language. Thus, Java program necessarily consists of classes. There are no global variables and functions, as you may see in C++. We called the class HelloWorld

public class HelloWorld {


}


Draw attention to the fact, that filename of the file is .java. It is obligatory in Java.

Entry point

Each application in Java has an entry point to start from. It is a public static method called main. Arguments to application are passed through args parameter.

public static void main(String[] args) {


}

Print command
System.out.println("Hello World! I am new to Java.");

Comments
Above the main method declaration you see comments block:

/**

* @param args

*/

It is special comments, called Javadoc. Using this system, one may automatically generate documentation for properly commented source.

Build and run

Building
Enter the directory with your source file and run the following command:

javac HelloWorld.java
After it is executed you will see a new file HelloWorld.class. This file is a compiled HelloWorld class.

Running
Now you can run it using following command:

java HelloWorld