Monday, September 29, 2008

HOW to install java on Linux

Download the JDK from http://java.sun.com/j2se/1.5.0/download.jsp
(A) install jdk by running the RPM or bin file.

Installation Instructions

Install formats - JDK 5.0 is available in two installation formats.

  • Self-extracting Binary File - This file can be used to install the JDK in a location chosen by the user. This one can be installed by anyone (not only root users), and it can easily be installed in any location. As long as you are not root user, it cannot displace the system version of the Java platform suppled by Linux. To use this file, see Installation of Self-Extracting Binary below.
  • RPM Packages - A rpm.bin file containing RPM packages, installed with the rpm utility. Requires root access to install, and installs by default in a location that replaces the system version of the Java platform supplied by Linux. To use this bundle, see Installation of RPM File below.

Choose the install format that is most suitable to your needs.

Note: For any text on this page containing the following notation, you must substitute the appropriate JDK update version number for the notation.

For example, if you are downloading update 1.5.0_01, the following command:

]# ./jdk-1_5_0_-linux-i586.bin

would become:

]# ./jdk-1_5_0_01-linux-i586.bin

Installation of Self-Extracting Binary
Use these instructions if you want to use the self-extracting binary file to install the JDK.

1. Download and check the download file size to ensure that you have downloaded the full, uncorrupted software bundle.You can download to any directory you choose; it does not have to be the directory where you want to install the JDK.

Before you download the file, notice its byte size provided on the download page on the web site. Once the download has completed, compare that file size to the size of the downloaded file to make sure they are equal.

2. Make sure that execute permissions are set on the self-extracting binary.

Run this command:
chmod +x jdk-1_5_0_-linux-i586.bin

3. Change directory to the location where you would like the files to be installed.

The next step installs the JDK into the current directory.

4. Run the self-extracting binary.

Execute the downloaded file, prepended by the path to it. For example, if the file is in the current directory, prepend it with “./” (necessary if “.” is not in the PATH environment variable):

]# ./jdk-1_5_0_-linux-i586.bin

The binary code license is displayed, and you are prompted to agree to its terms.

The JDK files are installed in a directory called jdk1.5.0_ in the current directory. The JDK documentation is a separate download.

Installation of RPM Packages
Use these instructions, If you want to install RPM packages.

1. Download and check the download file size to ensure that you have downloaded the full, uncorrupted software bundle.

Before you download the file, notice its byte size provided on the download page on the web site. Once the download has completed, compare that file size to the size of the downloaded file to make sure they are equal.

2. Make sure that execute permissions are set on the RPM Packages.

Run this command:
chmod +x jdk-1_5_0_-linux-i586-rpm.bin
3. Start the installation process. Type:
]# ./jdk-1_5_0_-linux-i586-rpm.bin

The binary code license is displayed, and you are prompted to agree to its terms.

4. The installation file creates jdk-1_5_0_-linux-i586.rpm file in the current directory.

5. Run the RPM command at the terminal to install the packages. Type:
rpm -iv jdk-1_5_0_-linux-i586.rpm

6. The JDK is installed in jdk-1_5_0_ sub-directory under the current directory.

Note about Root Access: Unbundling the software automatically creates a directory called jdk1.5.0_. Note that if you choose to install the JDK into system-wide location such as /usr/local, you must first become root to gain the necessary permissions. If you do not have root access, simply install the JDK into your home directory, or a sub directory that you have permission to write to.

Note about Overwriting Files: If you unpack the software in a directory that contains a subdirectory named jdk1.5.0_, the new software overwrites files of the same name in that jdk1.5.0_ directory. Please be careful to rename the old directory if it contains files you would like to keep.

(B) Create links for the JDK by entering the following commands:
]# ln -s /usr/java/jdk1.5.0_06 /usr/local/jdk1.5.0_06
]# ln -s /usr/local/jdk1.5.0_06 /usr/local/jdk

(C) Stack components like JBoss and Tomcat need JAVA_HOME set properly before prior to operation. Create the system files: /etc/profile.d/java.sh and java.csh to export the following environment variables:
JAVA_HOME=/usr/local/jdk
PATH=$JAVA_HOME/bin:$PATH

NOTE:
The shells bash and cshell use a different syntax for setting environment variables.

- For java.sh include the lines:
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH

- For java.csh include the lines:
setenv JAVA_HOME /usr/local/jdk
setenv PATH $JAVA_HOME/bin:$PATH

You can either use setenv or declare -x to declare the path in .csh file.
If you get any error like bad discriptor after running that .csh file, you can directly declare the path using following command.
]# export PATH=”$PATH:/usr/local/jdk-version”

(D) To verify that the installation was successful, at the prompt enter:
]# java –version

MySQL Database Indexing

What is Database Indexing?
Database index is like data structure that improve the performance of a database.
Index on table can be created one or more column on databse table, which improves random access of any records and efficient access of ordered records.

Types of Indexes in MySql

1. Normal Indexes :-
Normal Index have not restriction like Uniqueness,it is a basic index.

2. Unique Indexes :–
It is like a Normal index but only one difference, all values of the indexed columns must only occur once.

3.Primary Keys :-
Primary keys are basically unique index and must be add "PRIMARY KEY" in specific column.

How to Create Index in Mysql at time of table creation.

CREATE TABLE student (
fname VARCHAR(30),
lname VARCHAR(30),
studID INT, INDEX (studID)
)

You can create index on existing table used following MySQL Statement

CREATE INDEX index_studID ON student(student)

Advantages of database index
1. Database indexes speed up the database selection operation.


Advantages of database index
1. Database indexes slow down the database insert,update,delete operations.
2. Database Index takes more space.

Thread Safety in JSP & Servlet

You can thread safe your jsp (java server pages) pages in two ways

1. By implementing the SingleThreadModel interface.

This is done by adding the directive to your jsp page

<%@ page isThreadSafe=”false” %>

Within your jsp page.

When you implement SingleThreadModel interface instead of a single instance of the servlet generated for your jsp page loaded in memory, jsp engine creates new instance of that jsp page for each client request.
but avoid this method because, this methods has some performance issue and there are many pitfalls.

2. By using synchronized block or synchronized method

(a) synchronized block
when you expect that more than 2 threads may operate on your code (means om jsp page) and they may inconsistent your data then used synchronized block on particular object
e.g.

import javax.servlet.*;
import javax.servlet.http.*;
import java.math.*;
import java.io.*;

public class testServlet extends HttpServlet
{
//A variable that is NOT thread-safe!
private int cnt = 0;
private String str = “”;

public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.getWriter().println(”");
resp.getWriter().println(this + “:
“);
synchronized (str)
{
for (int c = 0; c < counter =" ”">

This string is called a str and is shared among all threads, thus ensure that at time one thread can access code block, if another thread want to enter code block then 1st thread must leave that block.

(b) Synchronized method
A whole method can be synchronized, just you need to add synchronized keyword in method signature.

e.g.
public synchronized void show()
{
// your code
}

Other precautions

To be thread safe in your JSP, do not declared variables in <%! %> tags. variables that declared in declaration tag are not thread safe.

Attributes.
Page level attributes are threadsafe.
Request level attributes are threadsafe, as long as you forward in a linear fashion. i.e. forward from servlet to JSP, and ensure that no more code runs in the servlet after forwarding.
Session level attributes: potentially not thread safe if one user makes multiple requests (eg clicks twice on a link/button)
application level attributes: not thread safe at all.

Also consider any resources that you pick up from session attributes/singletons etc as potentially not threadsafe.

Monitoring Bandwidth usage with SNMP and RRDtool for Linux

Before monitor any server you need to install SNMP and RRDTool packages on monitor server and net-snmp package on server which you want to monitor.

lets assume server which has SNMP and RRDTool means monitor server has ip address 10.10.10.1
and server which you want to monitor has IP address 99.99.99.99

SNMP Installation
SNMP packages require for querring .

- type command at the prompt
#yum search snmp
It will show you different snmp packages.
You neet to install 2 packages
net-snmp
net-snmp-devel
type command
#yum install net-snmp
#yum install net-snmp-devel

RRDTool Installation

RRD store and display time-series data (i.e. network bandwidth, server load average). It stores the data in a very compact way that will not expand over time, and it presents useful graphs by processing the data to enforce a certain data density.

RRDTool 1.2.x has dependancies ,hence you need to install following packages.

* libart_lgpl-2.3.11-2.i386.rpm
* libart_lgpl-devel-2.3.11-2.i386.rpm
* freetype-2.1.3-6.i386.rpm
* freetype-devel-2.1.3-6.i386.rpm
* zlib-1.1.4-8.i386.rpm
* zlib-devel-1.1.4-8.i386.rpm
* libpng-1.2.2-16.i386.rpm
* libpng-devel-1.2.2-16.i386.rpm

Download RRDTool from http://ftp.idilis.ro/mirrors/rrdtool/

lets assume you have rrdtool-1.2.12.tar.gz

To extract RRDTool run this command:

#tar -xzvf rrdtool-1.2.12.tar.gz

then change directory:
#cd rrdtool-1.2.12

to compile ans install RRDTool type:
#./configure –disable-tcl
#make
#make install

Apply following step on server which you want to monitor means 99.99.99.99
Install net-snmp
Type following commands
#yum search snmp
#yum install net-snmp

then edit snmpd.conf file and create readonly community
#vi /etc/snmp/snmpd.conf

add following line in snmpd.conf file
rocommunity community_name ip_address_of_monitor_server

start snmpd service
/etc/init.d/snmpd start

Apply following step on monitor server means 10.10.10.1
To monitor bandwidth you need to create RRD file,which will store bandwidth data.

step 1
Create RRD file.
rrdtool create bandwidth.rrd -s 60 \
DS:in:COUNTER:300:U:U \
DS:out:COUNTER:300:U:U \
RRA:AVERAGE:0.5:1:10080

step 2
Update RRD file.

/usr/bin/rrdupdate bandwidth.rrd \
N:`/usr/bin/snmpget -v 1 -c community_name -Oqv 9.99.99.99 IF-MIB::ifInOctets.2`: \
`/usr/bin/snmpget -v 1 -c community_name -Oqv 99.99.99.99 IF-MIB::ifOutOctets.2`

Above command should be run with 1 or 5 minute interval as per your need,hence add above command in cronjob.

step 3
Generate graph from bandwidth.rrd file

rrdtool graph bandwidth.png \
–end now –start end-30h –width 400 \
–x-grid HOUR:1:HOUR:6:HOUR:2:0:%H \
–imgformat=PNG \
–title=”your title” \
–rigid \
–base=1000 \
–height=120 \
–width=500 \
–alt-autoscale-max \
–lower-limit=0 \
–vertical-label=”bits per second” \
DEF:a=bandwidth.rrd:in:AVERAGE \
DEF:b=bandwidth.rrd:out:AVERAGE \
VDEF:totin=a,TOTAL \
VDEF:totout=b,TOTAL \
CDEF:cdefa=a,8,* \
CDEF:cdeff=b,8,* \
AREA:cdefa#00CF00:”Incoming” \
GPRINT:cdefa:LAST:”Cur\:%6.2lf %sb/s” \
GPRINT:cdefa:AVERAGE:”Avg\:%6.2lf %sb/s” \
GPRINT:cdefa:MAX:”Max\:%6.2lf %sb/s” \
GPRINT:cdefa:MIN:”MIN\:%6.2lf %sb/s\l” \
LINE1.3:cdeff#0000FF:”Outgoing” \
GPRINT:cdeff:LAST:”Cur\:%6.2lf %sb/s” \
GPRINT:cdeff:AVERAGE:”Avg\:%6.2lf %sb/s” \
GPRINT:cdeff:MAX:”Max\:%6.2lf %sb/s” \
GPRINT:cdeff:MIN:”MIN\:%6.2lf %sb/s\l” \
GPRINT:totin:”Total IN\:%6.2lf %sb/s” \
GPRINT:totout:”Total OUT\:%6.2lf %sb/s” \
VRULE:1084777200#FF0000:”"

After firing above command, a file bandwidth.png will be in current direcoty which is bandwidth graph for server 99.99.99.99