To SOAP, or to REST, that is the question

Today I was asked to describe the difference between SOAP and REST web services and how to decide which one to use? Here is what I came up with:

  • SOAP web services support RPC-Style and Message oriented style while REST is only RPC-Style.
  • REST is closely tied to HTTP while SOAP is transport independent and can be used with HTTP, Messaging, e-mail, UDP, etc.
  • SOAP is XML oriented while REST allows multiple data formats like XML, JSON, HTML, text etc.
  • REST reads can be easily cached thus performing better and scales easily. Also SOAP is more chatty thus using more bandwidth.
  • On the security side, WS-security allows for a SOAP message to identify the caller, sign the message, and encrypt message contents. This allows SOAP message to be secure even when using non-secure transport like e-mail. Security in REST is only SSL using HTTPS transport.
  • SOAP supports more complete transaction management using WS-AtomicTransaction.
  • SOAP provides standard WS-ReliableMessaging framework while REST does not have any messaging framework and client is expected to recover from communication failures.

So how do you go about deciding which one to use?  I guess it depends on your requirements. Most internet applications don’t require the level of security or transaction support offered by SOAP, so REST is preferred because it is easier to implement, test, and maintain. REST is also a better choice if you are just exposing data because of its support for multiple data formats. On the other hand, if you require complex security, transport independence, reliable message delivery, and distributed transaction support then SOAP is the better candidate.

Please feel free to leave comments if you think I have missed something.

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

 

Incremental Design And Development

The longer your application exists and being actively used, it will probably change as users want features added or changed. Faced with this, most developers will try to design application for the future changes. We may start writing code that is not needed  but we write it anyway to make it future proof. We cannot exactly know how our program will evolve over time and all the extra code we write may become unusable.

Another mistake made by developers is writing the code that is rigid in structure and not easy to change. It makes sense to spend some time during the design phase and pay special attention to code maintainability. Most programmers will end up maintaining code written by someone else and it is a nightmare to maintain badly designed code.

We may also be inclined to over engineer and be too generic, thinking that we can make any future changes easily for all possible situations. You can end up writing a lot of unnecessary code to be generic. It is perfectly fine to spend time designing a good solution but if you feel that your design is making things more complex than they need to be, you are probably over engineering.

This is where Agile method of incremental development and design can help. Agile development requires less time and effort by designing system piece by piece instead of designing the entire system upfront. We can easily update design with every iteration and add new features.

DB2 V10 Improvements

DB2 V10 is released by IBM. Here are some new and improved features:

Automation:

Self tuning memory, Automatic Maintenance, Health Monitoring features, Automatic storage management, and Self-Configuration features.

Storage optimization:

DB2 can dramatically reduce that cost with industry leading data compression technologies that compress tables, indexes, archive logs, temporary space, LOBs, XML, and back-up data. Adaptive data compression applies multiple compression techniques, including both table-wide and page-level compression, for maximum impact.

Performance:

DB2 Workload Manager is now integrated with DB2 pureScale and includes a powerful time-based control mechanism that can be quickly configured and deployed to automatically throttle low priority workloads.

Security:

DB2 offers a comprehensive suite of security features including authentication, authorization, trusted contexts, auditing, row and column access control, label-based access control, and encryption.

SQL Compatibility:

The latest version of DB2 includes extensive native support for the PL/SQL procedural language, new data types, scalar functions, improved concurrency, built-in packages, OCI, SQL*Plus, and more.

Time Travel Query:

Time Travel Query helps you to make your existing DB2 tables time-aware easily and provides a cost-effective means to resolve auditing and compliance issues.
You can achieve full traceability of backdated corrections through the use of bitemporal tables and allowing DBAs to use an existing SQL application and run it
across different time periods.

Awesome collaborative software development environment

If you are a small software development team or developing open source software, you can use Rational Team Concert for free. Ten developer license is free or professional version is free for approved open source projects.

Rational Team Concert will increase your team productivity by simplifying team related operations, work item tracking, source control, planning, and much more. Here is a list of some features:

Client access limit per server 10
Databases supported Derby(included), DB2 (Express-C available free)
Application servers supported Tomcat (included)
Client support Eclipse, Microsoft Visual Studio, ISPF, Command line, Web
Importers CVS, Subversion, Bugzilla, ClearCase, ClearQuest
Common lifecycle administration
Planning: agile, formal, hybrid
Common reporting and analytics
Source code management
Build management
Work item tracking
Customizable process/work item workflow
Project milestone tracking and status
Role-based process permissions

 

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;
	}

Improvements in Android SDK V17

Android SDK was recently bumped to V17 and as usual includes updates and new features. Here is a brief summary:

  • Android 4.0 Platform support (Unified UI framework for developers).
  • Emulator now supports hardware accelerated graphics rendering.
  • Support for running Android x86 system images in virtualization mode on Windows and Mac OS X. This should improve emulator performance greatly. This feature is
    experimental in this release so expect some hiccups.
  • Support Package revision 7.
  • ADT 17.0 – new build and Lint features including: feature to automatically setup JAR dependencies,  a feature that allows you to run some code only in debug mode, support for custom views with custom attributes in libraries, Lint check for Android API calls that require a version of Android higher than the minimum supported version, and Added over 20 new Lint rules, including checks for performance, XML layouts, manifest and file handling.

I am excited about running Android x86 system images in virtualization mode and will be setting it up as soon as I get some time and may write about it soon.

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