Wednesday, 27 April 2011

Batch processing

Batch solutions are ideal for processing that is time and/or state based:
  • Time-based: The business function executes on a recurring basis, running at pre-determined schedules.
  • State-based: The jobs will be run when the system reaches a specific state.
Batch processes are usually data-centric and are required to handle large volumes of data off-line without affecting your on-line systems. This nature of batch processing requires proper scheduling of jobs.
See quartz as one solution.

For more information on batch processing visit: "High volume transaction processing in J2EE"

Quartz example

 Quartz is a full-featured, open source job scheduling system that can be integrated with, or used along side virtually any Java Enterprise of stand-alone application. The Quartz Scheduler includes many enterprise-class features, such as JTA transactions and clustering. The following is a list of features available:
  • Can run embedded within another free standing application
  • Can be instantiated within an application server (or servlet container).
  • Can participate in XA transactions, via the use of JobStoreCMT.
  • Can run as a stand-alone program (within its own Java Virtual Machine), to be used via RMI
  • Can be instantiated as a cluster of stand-alone programs (with load-balance and fail-over capabilities)
  • Supoprt for Fail-over
  • Support for Load balancing.
Quartz components

  1. Scheduler Task – Pure Java class, the task you want to schedule. 
  2. Scheduler Job – Get the “Scheduler Task” from “JobDetail” via “task name“, and specify which schedule task (method) to run.
  3. Scheduler JobDetail – Define a “task name” and link it with “Scheduler Job”.
  4. Trigger – When will run your “Scheduler JobDetail”.
  5. Scheduler – Link “JobDetail” and “Trigger” together and schedule it.
Understanding the whole sequence by code.

Prequisities of coding
The following example demonstrates the use of Quartz scheduler from a stand-alone application. Follow these steps to setup the example, in Eclipse.
  1. Download the latest version of quartz from opensymphony.
  2. Make sure you have the following in your class path (project-properties->java build path):
    • The quartz jar file (quartz-1.7.0.jar).
    • Commons logging (commons-logging-1.1.1.jar)
    • Add any server runtime to your classpath in eclipse. This is for including the Java transaction API used by Quartz. Alternatively, you can include the JTA class files in your classpath as follows
      1. Download the JTA classes zip file from the JTA download page.
      2. Extract the files in the zip file to a subdirectory of your project in Eclipse.
      3. Add the directory to your Java Build Path in the project->preferences, as a class directory.

The code components:

1. Scheduler Task

Create a pure Java class, this is the class you want to schedule.
package com.vaani.common;
 
public class RunMeTask
{
public void printMe() {
System.out.println("Run Me ~");
}
}

2. Scheduler Job

Create a ‘job’ to implement the Quartz Job interface, and also the execute() method. Get the scheduler task from Job Details via “task name”, and specify which schedule task (method) to run.

package com.vaani.common;
 
import java.util.Map;
 
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
public class RunMeJob implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException {
 
Map dataMap = context.getJobDetail().getJobDataMap();
RunMeTask task = (RunMeTask)dataMap.get("runMeTask");
task.printMe();
}
}
 
or  

//you can use already defined scheduler tasks like calendar etc..any POJO
public class RunMeJob implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException {
 
System.out.println("Executing job at: " + 
Calendar.getInstance().getTime()
+ " triggered by: " + context.getTrigger().getName());
}}

3. Scheduler JobDetail

Initialize a JobDetail object, link your scheduler job with setJobClass(RunMeJob.class); method, define a task name and put it into Job data map, dataMap.put(“runMeTask”, task);. This “task name” is the only link between your Job and JobDetail.
P.S : job.setName(“runMeJob”) has no special function, just a descriptive name, you can put anything.
RunMeTask task = new RunMeTask();
 
//specify your sceduler task details
JobDetail job = new JobDetail();
job.setName("runMeJob");
job.setJobClass(RunMeJob.class);
 
Map dataMap = job.getJobDataMap();
dataMap.put("runMeTask", task);

4. Quartz Triggers

Quartz triggers are used to define when the Quartz will run your declared scheduler job. There are two types of Quartz triggers :
  • SimpleTrigger – allows to set start time, end time, repeat interval to run yout job.
  • CronTrigger – allows Unix cron expression to specify the dates and times to run your job.
Code snippet – SimpleTrigger
SimpleTrigger trigger = new SimpleTrigger();
trigger.setName("runMeJobTesting");
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(30000);
Code snippet – CronTrigger
//configure the scheduler time
CronTrigger trigger = new CronTrigger();
trigger.setName("runMeJobTesting");
trigger.setCronExpression("0/30 * * * * ?");

5. Scheduler

Link the “JobDetail” and “Trigger” together and schedule it.
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);


Sample code

SimpleJob:
public class SimpleJob implements Job {
public void execute(JobExecutionContext context) 
throws JobExecutionException {
System.out.println("Executing job at: " + Calendar.getInstance().getTime()
+ " triggered by: " + context.getTrigger().getName());
}
}


Scheduling job with runner:
public class QuartzTest {
public static void main(String[] args) {
try {
// Get a scheduler instance.
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();

long ctime = System.currentTimeMillis();

// Create a trigger.
JobDetail jobDetail = new JobDetail("Job Detail", 
"jGroup", SimpleJob.class);
SimpleTrigger simpleTrigger = new SimpleTrigger("My Trigger", "tGroup");
simpleTrigger.setStartTime(new Date(ctime));

// Set the time interval and number of repeats.
simpleTrigger.setRepeatInterval(100);
simpleTrigger.setRepeatCount(10);

// Add trigger and job to Scheduler.
scheduler.scheduleJob(jobDetail, simpleTrigger);

// Start the job.
scheduler.start();
} catch (SchedulerException ex) {
ex.printStackTrace();
}
}
}



Output
Apr 27, 2011 5:20:47 PM org.quartz.simpl.SimpleThreadPool initialize
INFO: Job execution threads will use class loader of thread: main
Apr 27, 2011 5:20:47 PM org.quartz.core.SchedulerSignalerImpl <init>
INFO: Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
Apr 27, 2011 5:20:47 PM org.quartz.core.QuartzScheduler <init>
INFO: Quartz Scheduler v.1.7.3 created.
Apr 27, 2011 5:20:47 PM org.quartz.simpl.RAMJobStore initialize
INFO: RAMJobStore initialized.
Apr 27, 2011 5:20:47 PM org.quartz.impl.StdSchedulerFactory instantiate
INFO: Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
Apr 27, 2011 5:20:47 PM org.quartz.impl.StdSchedulerFactory instantiate
INFO: Quartz scheduler version: 1.7.3
Apr 27, 2011 5:20:47 PM org.quartz.core.QuartzScheduler start
INFO: Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
Executing at: Wed Apr 27 17:20:47 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:47 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:47 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:48 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:48 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:48 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:48 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:48 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:48 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:48 IST 2011 triggered by: My Trigger
Executing at: Wed Apr 27 17:20:48 IST 2011 triggered by: My Trigger

References
The Unix cron expression is highly flexible and powerful, you can learn and see many advance cron expression examples in following website.
1. http://en.wikipedia.org/wiki/CRON_expression
2. http://www.quartz-scheduler.org/docs/examples/Example3.html

Monday, 25 April 2011

Spring AOP vs AspectJ

Spring AOP is simpler than using AspectJ as there is no requirement to introduce the compiler into build processes for Compile Time Weaving. If you only need to advise the execution of operations on Spring beans, then Spring AOP is the right choice. If you need to advise objects not created by the Spring container, then you will need to use AspectJ. You will also need to use AspectJ if you wish to advise join points other than simple method executions (for example, field get or set join points, and so on).When using AspectJ, you have the choice of the AspectJ language syntax or the @AspectJ annotation style. Clearly, if you are not using Java 5+ then the choice has been made for you… use the code style. If aspects play a large role in your design, and you are able to use the AspectJ Development Tools (AJDT) plugin for Eclipse, then the AspectJ language syntax is the preferred option: it is cleaner and simpler because the language was purposefully designed for writing aspects. If you are not using Eclipse, or have only a few aspects that do not play a major role in your application, then you may want to consider using the @AspectJ style and sticking with a regular Java compilation in your IDE, and adding an aspect weaving phase to your build script.

Moreover following are the conclusive differences in Spring AOP and AspectJ.
Spring-AOP : Runtime weaving through proxy using concept of dynamic proxy if interface exists or cglib library if direct implementation provided.
AspectJ: Compile time weaving through AspectJ Java Tools(ajc compiler) if source available or post compilation weaving (using compiled files).Also, load time weaving with Spring can be enabled – it needs the aspectj definition file and offers flexibility. Compile time weaving can offer benefits of performance (in some cases) and also the joinpoint definition in Spring -aop is restricted to method definition only which is not the case for AspectJ.

Weaving style of working with AOP

 Earlier we saw proxy style of handling aop, now we will see weaving style. Spring 2.5 supported LTW (load time weaving).
Weaving means bytecode instrumentation.

Types of Weaving
Code weaving comes in three flavors:

  • Load-time weaving(LTW): Aspect weaving is performed by the class loader when classes are first loaded.
    Weave class files when being loaded in VM
    i.e. Add aspect to class files OR Aspect + class files
  • Compile-time weaving: Aspects are weaved into the class files when they are compiled.
    Aspect + source code = class files
  • Binary Weaving (linker)
    Aspect + source/byte code = class files


Aspectwerkz and AspectJ works with both compile and load-time weaving.

Compile time weaving and binary weaving requires a seperate AspectJ compiler, which has nothing to do with spring. You will have to seperate lean AspectJ or try it out. Eclipse plugins are also available for AspectJ making it easier to achieve the same.

See how to configure the weaving method and proxy method of AOP in spring.


AOP support in spring

In spring, AOP is supported via 2 methods:
Click on the links to read in detail.

How to configure any of the methods in spring?

We need to decide with approach to use:
  • <aop:aspectj-autoproxy/> to enable proxy based AOP implementation.
  • <context:load-time-weaver /> to enable LTW ( load time weaving ) of classes.
    Works for Tomcat 5.5+, dm server 1.0+, weblogic 10, oc4j 10.x, glassfish.
    For standalone applications use:
    java-javaagent:spring-agent.jar ...

Spring's dynamic proxies for AOP

Till spring 2.0, spring's implementation is called as proxy-based AOP implementation irrespective of which AOP library you are using. These proxies essentially wrap a targ et object (eg. BankAccount instance) in order to apply aspects (SecurityManager calls) before and after delegation of the target object.
These proxies appear as the class of the target object to any client, making the proxies simple drop-in replacements anywhere the original target is used.

So before using AOP, code used to look like this:

But after using proxy pattern the code will look like this:


Since the client would be unaware that he is taling to the different class, the Proxy generated by Spring would either be sublcass of BankAccount or implement interfaces exposed by BankAccount class so that client class remains transparent to the changes in the configuration.
Client invokes methods exposed by the proxy, which in turn will execute interceptors configured for target bean.

Client will never know that he is calling the method of a proxy class. Proxy classes are wrappers around our actual components and contain same signature methods taking it 100% transparent to the system.

In spring, proxy classes are used in many places apart from AOP as well.

See how to configure the weaving method and proxy method of AOP in spring.

AOP : Tutorial