REST Web Services Primer

Web services that follow Representational State Transfer principles are called REST web services. In REST, resources are identified by URIs and clients communicate using standard set of methods.

Principles of REST

  •  Resources are identified by URIs. For example:

    http://example.com/widgets/bar

    http://example.com/employees/foo

  • Standard set of methods:
    GET – Read
    POST – Update or create without a known id
    PUT – Update or create with a known id
    DELET – Remove
  • Link things together. Once client knows the entry point, it should be able to navigate to all other parts of the application.
  • Multiple representations
    Offer data in multiple formats (XML, JSON, HTML). Goal is to maximize reach.
  • Content negotiation using Accept header or URI. For example:
    Header based negotiation:
    GET /foo
    Accept: application/json
    URI-based: GET /foo.json
  •  Stateless communications
    Everything required to process a request should be contained in the request. Avoid using sessions because they are less scalable and cannot be bookmarked.

JAX-RS 1.1 Overview

JAX-RS is a specification defined by JSR-311 in the Java Community Process. Current available version is 1.1. Some of the key features provided by JAX-RS include:

  • POJO based resource API (Server side)
  • HTTP Centric
  • Container independent
  • Inclusion in Java EE but without dependency
  • Annotation driven. For example:
    @Path
    @Produces/Consumes
    @HEAD/PathParam/QueryParam
    @GET/POST/PUT/DELETE
  • Data format/media types – XML, JSON, (X)HTML
  • Accept HTTP header and URI-based content negotiation

JAX-RS 2.0 is under development and will provide more features including:
* Low level client API using builder pattern
* Hypermedia
* Bean validation for form or query parameters
* Filters/Handlers
* Server-side asynchronous request processing
* Server-side content negotiation

Jersey Overview

Jersey is the open source, production quality, JAX-RS (JSR 311) Reference Implementation for building RESTful Web services.

  • Multiple container support
  • Resource configuration
    * Server filters
    * Resource filters
    * Plugable component providers
    * Pluggable dispatchers
    * Pluggable JAX-RS providers
    * Resource Injection
  • Client API with client configuration
  • WADL support for representation of resources
  • JSON with JAXB binding
  • Spring integration
  • MIME multipart API
  • Atom

Take Control of Java Batch jobs – Part2

In part1 of the blog, I talked about WebSphere Feature Pack for Modern Batch. In part 2, I will introduce Spring Batch framework. Spring batch framework was developed as a standard-based way to implement common batch patterns in Java.

Spring Batch architecture consists of following layers:

Application: This consists of your custom code and configuration of your batch jobs.
Core: This layer contains the core elements of framework including Job and Step interface as well as JobLauncher and JobParameters.
Infrastructure:  This layer deals with reading and writing to files and databases.

Note: Spring Batch framework does not include a scheduler. You will need to use external schedulers like cron or Quartz for scheduling jobs at a given time.

Main Components of a Spring Batch Application

Job Repository: Component provided by infrastructure to store job execution data.
Job Launcher: Component provided by  infrastructure to start executing a job.
Job: Application component representing a batch job.
Step: A phase in a job, a job usually consists of multiple steps in a sequence.
Tasklet: A transactional  process that occurs in a step.
Item: A record read from a data source or a record written to a data source.
Chunk: A certain sized group of items. This is used for chunk processing.
Item Reader: Component responsible for reading items from data source.
Item Writer: Component responsible for writing items to data source.
Item Processor: Component responsible for processing an item before output.

What is a Spring Batch Job?

A batch job in Spring Batch is composed of a sequence of steps configured in XML configuration file. It is easier to develop, maintain, and test a batch job that is divided into multiple steps. Spring Batch framework allows for control flow decisions based on step completed, failed, or any other custom logic like checking a table in database.

Links for further information:

Spring Batch Framework
User’s Guide

Take Control of Java Batch jobs – Part1

Batch processing is widely used in back-end data processing, data roll-up, and off-line processing. In part 1 of this post, I will talk about WebSphere Feature Pack for Modern Batch. In part 2 I will talk about Spring Batch, and in part 3 we will develop our own simple batch processing framework.

WebSphere Feature Pack for Modern Batch:

If you are using WebSphere Application Server V7, you can install Feature Pack for Modern Batch for free. Feature pack provides support for Java batch programming model and tools to control and execute batch applications. In normal J2EE applications, an individual request is usually completed in seconds but batch work may run for hours or even days. Feature Pack for Modern Batch extends the WebSphere Application Server to accommodate these resource intensive and long running applications.

Major parts of Feature Pack for Modern Batch

Job Scheduler: Job scheduler provides job management functions like submit, cancel, and restart. A relational database is used to store all job history like jobs waiting to run, currently running jobs, and jobs already finished. Job scheduler provides management functions through web based management console, a command line shell, web services, and EJB interface. Job scheduler can be hosted on individual WAS server or a cluster.

Batch Container: Batch container provides execution environment for batch jobs. A relational database is used to store checkpoint information for transactional batch jobs.

Java EE batch application: Batch applications are standard Java EE applications which are deployed as EAR files and implements either a transactional or compute-intensive programming model provided by Modern Batch.

xJCL: Batch jobs are described using XML-based job control language. xJCL file identifies which applications to run, inputs, and outputs.

Batch Programming Models

Feature pack for Modern Batch provides a transactional model and a compute-intensive programming model. Both are implemented as Java objects and packaged in EAR for deployment.

A transactional batch job can be composed of one or more batch steps and are processed sequentially. Checkpoint algorithms are used by run-time to decide how often to commit transactions. Checkpoint algorithms are defined in the xJCL job control file. Time-based and record-based checkpoint algorithms are provided by the Feature Pack and provides API to create your own checkpoint algorithms. Batch jobs return system defined return codes as well as application defined result codes. Optional result algorithms can also be defined to act on return codes.

Compute-intensive batch jobs are not divided into steps and only have one job step. They are submitted asynchronously and run for extended periods of time. Packaged compute-intensive application can contain multiple work objects.

Links

Download the “Getting Started Guide”

Download WebSphere Application Server V7 Feature Pack for Modern Batch

 

Detecting file type stored in Blob

Recently I had to create a download servlet which allowed users to download pdf, excel, or  power point files stored in DB2 database as Blob. Whoever designed the database did not include a mime type column in the database so I had to figure out the file type. After looking at files in a binary editor, I was able to figure out the magic marker and then I used Blob.position() method from java.sql.Blob. Below is a code I used to detect file types from Blob:


/**
 * Returns file type (.pdf, .doc, .xls, or .ppt) from a Blob
 * <p>
 * This method returns empty string if exception or file type
 * cannot be determined.
 *
 * @param  attachment  SQL Blob
 * @return the file type stored in Blob
 * @see    java.sql.Blob
 */
 private String findMimeType(Blob attachment) {

 String returnString = "";

 //Define binary markers for the file types
 byte[] officeMagic = hexStringToByteArray("D0CF11E0A1B11AE1");
 byte[] wordDocMagic = hexStringToByteArray("4D6963726F736F667420576F726420446F63756D656E74");
 byte[] excelMagic = hexStringToByteArray("4D6963726F736F667420457863656C20576F726B7368656574");
 byte[] excelMagic2 = hexStringToByteArray("4D6963726F736F667420457863656C");
 byte[] pdfMagic = hexStringToByteArray("255044462D");
 byte[] pptMagic =
 hexStringToByteArray("50006F0077006500720050006F0069006E007400200044006F00630075006D0065006E0074");
 byte[] pptMagic2 = hexStringToByteArray("4D6963726F736F667420506F776572506F696E74");

	try {
		//check if pdf file
		if (attachment.position(pdfMagic, 1) != -1) {

			returnString = ".pdf";
		} else if (attachment.position(officeMagic, 1) != -1) {
			//Microsoft office document

			if (attachment.position(wordDocMagic, 1) != -1) {
				returnString = ".doc";
			} else if (attachment.position(excelMagic, 1) != -1) {
				returnString = ".xls";
			} else if (attachment.position(excelMagic2, 1) != -1) {
				returnString = ".xls";
			} else if (attachment.position(pptMagic, 1) != -1) {
				returnString = ".ppt";
			} else if (attachment.position(pptMagic2, 1) != -1) {
				returnString = ".ppt";
		}
	}

	} catch (Exception e) {

     	//log warning here
	}

	return returnString;
}

	/**
	 * This method converts a HEX string to byte array
	 * @param s Hex string
	 * @return byte array
	 */
	private static byte[] hexStringToByteArray(String s) {
	    int len = s.length();
	    byte[] data = new byte[len / 2];
	    for (int i = 0; i < len; i += 2) {
	        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
	                             + Character.digit(s.charAt(i+1), 16));
	    }
	    return data;
	}

Java Enumeration Primer

Here is a quick primer on Java enums:

  • Enumeration is fixed set of constants
  • Provides type-safe representation of constant data
  • enum is the Java class that represents enumeration
  • enum cannot extend any other class
  • Constants in enum are public,final, and static
  • enum can declare additional methods and fields
  • enum can declare any number of constructors
  • values() returns array of enum values
  • If you need int value, you can use ordinal() static method
  • static valueOf() method can be used to convert a string into corresponding enum
  • enum can be used in switch statement

Here is an example of enum with constructors:


public enum IceCream {

	PLAIN(2),
	SUGAR(3),
	WAFFLE(5);

	private IceCream(int scoops) {
		this.scoops = scoops;
	}

	public final int scoops;
}

public class TestEnum {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		IceCream cone1 = IceCream.PLAIN;
		IceCream cone2 = IceCream.SUGAR;
		IceCream cone3 = IceCream.WAFFLE;

		System.out.println("cone 1 needs " + cone1.scoops + " scoops");
		System.out.println("cone 2 needs " + cone2.scoops + " scoops");
		System.out.println("cone 3 needs " + cone3.scoops + " scoops");

	}

}

Output:

cone 1 needs 2 scoops
cone 2 needs 3 scoops
cone 3 needs 5 scoops