Daily Archives: August 24, 2013

Spring Book – Chapter 19 – Tasks and Scheduling

Concurrent programming and management of threads in general are necessary because the world of web programming is essentially concurrent because multiple clients can access in the time. Asynchronous programming promotes using the same thread to process multiple requests, sequentially, but with no request blocking the thread.

Spring has supported task scheduling and asynchronous method execution from its earliest releases. Spring 3.0 has made this easier by allowing you to declaratively do this by using XML and annotation-based programming.

This chapter initially looks into the various concepts dealing with concurrency and then looks into Java support for doing this. Later on it looks into how Spring Framework can support doing this in your application.

Terms and Concepts

The following concepts and terms that will be used throughout the chapter :

  • Concurrency Concurrency is a system property in which several computations are executing simultaneously, and potentially interacting with each other. Concurrency is a built in feature of the Java language and platform.
  • Process A process is a flow sequence that runs independently and isolated from other processes. It cannot have access to data in other processes.
  • Thread A thread is a single, sequential flow of control within a program. A thread can be called as a lightweight process which has access to shared data. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread.
  • Task A task is a basic unit of programming. In our sample application, downloading the cargo utilization report of the first hundred customers represents a simple task.
  • Multithreading Multithreading is the ability of a program to manage its use by more than one thread at the same time and to even handle multiple requests, without having to have multiple copies of the program running. Each execution of the program is kept track of as a thread with a separate identity.
  • Multitasking Multitasking is the ability of a program to allow a thread to perform more than one task at a time. The program can keep track of where you are in these tasks and go from one to the other without losing information. Being able to do multitasking doesn’t mean that an unlimited number of tasks can be performed at the same time. Each task consumes system storage and other resources and as more tasks are started, the system may slow down or begin to run out of storage.
  • Serial Processing Serial processing is strictly sequential, without overlap of the successive processing times on objects or distinct subsystems. In a standard type of serial system, each object takes the same average amount of time to process and the next object begins processing only when the previous one is completed.
  • Parallel Processing Parallel processing signifies simultaneous processing on several objects or subsystems at the same time, although processing may finish on different objects at different times. Parallelism is a term typically used to describe executions that physically execute simultaneously with the goal of solving a problem in less time or solving a larger problem in the same time. Parallelism differs from concurrency and actually exploits concurrency. The advantages of parallel processing are faster execution time and higher throughput. The disadvantages are that there are additional hardware and power requirements.
  • Task Scheduling This is another form of concurrency in action, wherein a piece of work can be scheduled to run once or repeatedly at an interval for various application use cases. In our sample application a task schedule can be created for sending periodic mails to the entire customer in the system giving details of their cargo loyalty points.

Note: Scheduling is needed if you want your application to automate the repetition of a task at specific intervals or particular date. In any modern application its common to have scheduling requirements, and the Spring Framework brings a level of abstraction to this as well. Spring has good support for scheduling and, as shown later in this chapter, it also supports using other third party scheduling services, such as Quartz.

  • Liveness Liveness defines a concurrent application’s ability to execute in a timely manner. There are various kinds of liveness problems that can occur in your application, namely deadlock, starvation and livelock.
  • Deadlock An application that allows concurrency is prone to a deadlock condition in which all the threads are waiting for an event that other threads in the same set have to cause. For example, if thread A waits for a lock on object C, which thread B holds, and thread B waits for a lock on object D, which is held by thread A, these two threads are locked and cannot continue further, resulting in deadlock.
  • Starvation Starvation describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress by itself. This happens when shared resources are made unavailable for long periods by other “greedy” threads.
  • Livelock A thread often acts in response to the action of another thread. If the other thread’s action is also a response to the action of another thread, then livelock may result. As with deadlock, livelocked threads are unable to make further progress, but unlike deadlock they are not blocked. They are simply too busy responding to each other to resume with their own work.

Currency Support by Java

Concurrency is a built in feature of the Java language and platform. Java has a predefined class java.lang.Thread which provides the mechanism by which threads are created. However to avoid all threads having to be child classes of Thread, it also uses a standard interface java.lang.Runnable. Any class which wishes to express concurrent execution can implement Runnable interface and provide the run method. Threads do not begin their execution until the start method in the Thread class is called. Listing 20-1 shows creating a thread by extending Thread class and Listing 20-2 shows creating of thread by implementing the Runnable interface.

Listing 20-1. Creating thread by extending Thread class

Listing 20-2. Creating thread by implementing Runnable interface

Page Visitors: 25259

Spring Professional Certification Exam

With Spring Framework gaining recognition day by day, getting certified will be one of the most valuable credentials in Java.

Can I write this exam?

One of the pre-requisites to write this exam is to have a training taken from SpringSource on Core Spring. If that’s the case, why should we have a book which also aims at helping readers to prepare for SpringSource Certification? Earlier there was an alternative which is called “Grandfathered” candidate, whereby an individual having adequate Proficiency evaluated by SpringSource could by-pass the training and get the exam voucher. While writing this book, I could read from SpringSource website that this is no longer applicable. Even after the training I feel there are so much required to actually pass the exam and get good score in the certification. The class manual is too easy and doesn’t cover edge cases so clearly isn’t sufficient. This book also goes into details of each objectives on which the exam is based.

How many questions?

There are a total of 50 questions to be answered. The questions are organized by subject areas detailed in the exam certification study guide provided by SpringSource.

Following that philosophy, here’s the distribution for Spring 3 as per Jeanne Boyarsky’s Spring 3.X Certification Experiences (http://www.selikoff.net/2010/08/20/jeannes-core-spring-3-certification-experiences/):

  • Container and test (20)
  • AOP (10)
  • JDBC (3)
  • Transactions (4)
  • Web (2)
  • REST (2)
  • Remoting  (2)
  • Security (2)
  • JMS (2)
  • JMX (2)

How much time do I get to write this exam?

You get total of 90 minutes to complete the exam, even reading the rules. Most people wont find this a problem, because usually these are not tricky, long or complicated.

How are the questions like?

The questions are aimed at testing the candidate’s depth and the actual working knowledge in Spring Framework. Each question has four answer choices. Even though it is multiple choices, it cannot be thought that you need to choose only one right answer. Answering a question makes the type of questions as follows:-

  • Select one answer
  • Select X answers
  • Select one or more answers (Very hard and tricky)
  • Select the one that is not correct (make sure that you read the question carefully to fall into wrong selection)

Most of the questions are fairly at a high level with few detailed ones. Usually it doesn’t stray from the course or study guide.

How many questions do I have to answer correctly?

The passing score for the exam is 76%. This boils down to 38 correct answers out of 50 questions. At the time of writing this book, all questions were equally scored, so 38/50 would give you the passing score for the exam.

How much does it cost?

In the case of Spring Certification it’s very tricky. As mentioned earlier the pre-requisites for taking this exam is training from SpringSource on Core Spring. According to various factors this can change. So it will not be a good idea to give an exact figure for the certification exam. If you have taken the training you get a voucher containing one free attempt at the certification test. Retakes are usually $150, I think that would be one of the reasons you need this book.

How can I register for the exam?

Once you get the exam voucher, you can register at any certified spring certification center. The exam can be taken at one of over 4500 world-wide testing facilities (http://www.pearsonvue.com/springsource/).

Exam resources available

You can get various resources which will be of help to you in the spring certification guide (http://www.springsource.com/files/core-spring-3.0-certification-study-guide_0.pdf) itself. Some very good links and resource locations are given below:

Spring Certification Path

SpringCertification

Figure 1 SpringSource Developer Certification Path

Page Visitors: 14526