Quartz is a full-featured, open source job scheduling system that can be integrated with,or used along side virtually any J2EE or J2SE application.It can mainly be used for Driving WrkFlow and System maintainance.
It can be integrated with any system with ease.It can perform simple or complicated schedules based on given cron expression.The execution logic can be embedded into simple java classes by implementing certain intefaces.
The job information can either be stored in memory through RAMJobStore or can be stored in relational databases using JDBCJobStore.If RAMJobStore is used whenever the server restarts, previously scheduled data is lost.
For scheduling jobs a scheduler needs to be started and jobs can be added as shown below.
SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();
Scheduler sched = schedFact.getScheduler();
sched.start();
JobDetail jobDetail = new JobDetail("myJob", null, DumbJob.class);
Trigger trigger = TriggerUtils.makeHourlyTrigger(); // fire every hour trigger.setStartTime(TriggerUtils.getEvenHourDate(new Date())); // start on the next even hour trigger.setName("myTrigger");
sched.scheduleJob(jobDetail, trigger);
Custom job should implement the following interface
Job Interfacepackage org.quartz;
public interface Job {
public void execute(JobExecutionContext context) throws JobExecutionException; }
CronTrigger needs to be used for more complicated schedules.
An example of a complete cron-expression is the string "0 0 12 ? * WED" - which means "every Wednesday at 12:00 pm".
Wednesday, September 24, 2008
Quartz - For Scheduling
Thursday, September 11, 2008
Writing Simple Taglibs
Writing tag libraries has mainly 3 parts
1 .The tld file defining the tag parameters
2. Java Class Containing the implementation of the Tag
3. Jsp Code Calling the Taglib
1.Tld for Tag
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>oreillySamples</shortname>
<info>OReilly Sample Tag library</info>
<tag>
<name>hello</name>
<tagclass>com.ivy.tags.Hello </tagclass>
<bodycontent>JSP</bodycontent>
<info>
This is a simple hello tag.
</info>
<attribute>
<name>name</name>
<required>false</required>
<rtexpvalue>true</rtexpvalue>
</attribute>
<attribute>
<name>iterations</name>
<required>false</required>
<rtexpvalue>false</rtexpvalue>
</attribute>
</tag>
</taglib>
2.Java Class For the Tag
public class Hello extends BodyTagSupport {
private String name=null;
private int iterations=1;
/**
* Getter/Setter for the attribute name as defined in the tld file
* for this tag - Name & iterations
*/
//core content of the tag needs to be written here
public int doStartTag() throws JspTagException{
try {
JspWriter out = pageContext.getOut();
out.println("<table border=\"1\">");
if (name != null)
out.println("<tr><td> Hello " + name + " </td></tr>");
else
out.println("<tr><td> Hello World <td></tr>");
} catch (Exception ex) {
throw new JspTagException("All is not well in the world." + ex );
}
// Evaluate the body if there is one
return EVAL_BODY_TAG;
}
//For the end part execution
public int doEndTag() throws JspTagException {
try {
JspWriter out = pageContext.getOut();
out.println("</table>");
} catch (Exception ex){
throw new JspTagException("All is not well in the world." + ex);
}
return(EVAL_PAGE);
}
public int doAfterBody() throws JspTagException {
if (iterations >= 1) {
BodyContent body = getBodyContent();
try {
JspWriter out = body.getEnclosingWriter();
out.println(body.getString());
body.clearBody(); // Clear for next evaluation
} catch(IOException ioe) {
throw new JspTagException("Error in Hello tag doAfterBody " + ioe);
}
return(EVAL_BODY_TAG);
} else {
return(SKIP_BODY);
}
}}
3. JSP PAGE
Include the written tld <%@ taglib uri="./sample.tld" prefix="sample" %>
<sample:hello name="userName" iterations="5">
<tr><td><b>Have a nice day</b></td></tr>
</sample:hello>
CSS Sprite - Another Web Optimisation Technique
When there are lots of images to be downloaded for a web page, the best option would be to use Css Sprite.In simple
words it reduces the number of images to be downloaded into one single big image.
Thus all the image requests will be reduced into one single request.At slight cost of increase in size better performance is attained.
Different Technique can be used to a create a single image from multiple images. The single image needs to be mentioned in the css and
where the individual images are required in the jsp the big image is specified with the offset, which cuts off the unwanted part of the image.
Reference http://css-tricks.com/css-sprites-what-they-are-why-theyre-cool-and-how-to-use-them/
Multiple images before Sprite
#nav li a {background:none no-repeat left center}
#nav li a.item1 {background-image:url('../img/image1.gif')}
#nav li a:hover.item1 {background-image:url('../img/image1_over.gif')}
#nav li a.item2 {background-image:url('../img/image2.gif')
Sungle Image with Sprite & offsets mentioned for each image
#nav li a {background-image:url('../img/image_nav.gif')}
#nav li a.item1 {background-position:0px 0px}
#nav li a:hover.item1 {background-position:0px -72px}
#nav li a.item2 {background-position:0px -143px;}
The website provides interface which can take in images uploaded as zip and convert into a single image.
Wednesday, August 13, 2008
Symbolic Links
There are two concepts of `link' in Unix, usually called hard link and
soft link.
A soft link (or symbolic link, or symlink) :it is a small special file that contains a pathname. Thus,
soft links can point at files on different filesystems
Command
To create Soft link
ln -s [target] [linkname]
Delete links
rm [link name]
Display Links
ls -l
A hard link is just a name for a file. (And a file can have several names). It is deleted from disk only when the last name is removed. Hard link can be created without the -s option.
The major use of links would be to avoid redundancy and it saves a lot of maintenance headaches.A typical case would be to avoid redudant image copy into multiple locations . Instead create symbolic links. Changing one image serves the need , if not links can be overridden with location specific image.
Monday, August 11, 2008
Direct Web Remoting (DWR)
Direct Web Remoting (DWR) exports java code to the browser. It consists of:
A java servlet running on the server that processes requests and sends responses back to the browser
Javascript running in the browser sends requests and can dynamically update the webpage
Methods for installtion
1 .Download the DWR jar
http://directwebremoting.org/dwr/download
2. Addition into web.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
3.Add dwr.xml
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="JDate">
<param name="class" value="java.util.Date"/>
</create>
<create creator="new" javascript="Demo">
<param name="class" value="your.java.Bean"/>
</create>
</allow>
</dwr>
The DWR config file defines what classes DWR can create and remote for use by Javascript
http://localhost:8080/[YOUR-WEBAPP]/dwr/ will show all accesible java methods
Include links to the javascript files that make the magic happen:
<script src='/[YOUR-WEBAPP]/dwr/interface/[YOUR-SCRIPT].js'></script>
<script src='/[YOUR-WEBAPP]/dwr/engine.js'></script>
DWR generates Javascript code that is similar to the Java code that exported using dwr.xml
Example
public class Remote {
public String getData(int index) { ... }
}
<script type="text/javascript"
src="[WEBAPP]/dwr/interface/Remote.js"> </script>
<script type="text/javascript"
src="[WEBAPP]/dwr/engine.js"> </script>
Remote.getData(42, function(str) { alert(str); });
It also helps in calling Meta-Data Objects ,handling Timeouts and Handling Errors and creating Creating Javascript objects to match Java objects.
Hence the java objects can be mapped to javascript and exchanged vise versa.
For more info refer : directwebremoting.org/ -
Wednesday, August 6, 2008
Web Performance Techniques
1. Zipping the static content(images,css,javascript) and page response.
This can be achived by using the following configuration on the apache vhost configuration
For static content
<FilesMatch "\.(html|htm|js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
For page response ending with .do requests
<LocationMatch "\.(do)$">
SetOutputFilter DEFLATE
</LocationMatch>
2. Adding expires header
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Expires "Wed, 31 Dec 2008 23:59:59 GMT"
</FilesMatch>
Expires also can be set as access plus x days
By adding this all images with the above extensions will not be requested.
If the above is missing all requests are made and images are downloaded for every requests.
if the browser cache is enabled , still Get requests are made and if the response is 304 meaning file has not changed the image will not be downloaded else
will download the image.
3. KeepAlive On
KeepAliveTimeout 15
MaxKeepAliveRequests 100
By setting this on httpd.conf the connection once obtained will not be disconnected until 15 seconds. Hence the multiple requests on a page will be faster.
The disadvantage could be , if the requests are very high the latter once will not get a connection until these are closed.
The cache setting will override the expires header setting, if it is less than the time interval for expires header.
Must use Addons With FireFox
FireBug - Shows request basis data along with headers and allows easy debugging of html content.
Web developer - Comes with lot of web development features like enable/disable cache/image/css/form data/proxy etc.
Live HTTP headers - Gives all trhe details required with headers.
Tamper Data -gives header data and also allows to tamper the data.
.jpg)