Monthly Archives: August 2013

Spring Framework Vs. JEE – Part II

As promised this is my second part of my blog. The first part of the blog can be accessed from here.

As part writing this blog i browsed through various blogs on the same topic. As i told in my first blog, this really is a holy war and over coming few months and years, this will rage even harder.

I bust out into laugh after reading one of the blogs, the link for which is as given below:-

http://broadleaf.tumblr.com/post/31333966305/broadleaf-continues-to-choose-spring-framework

The point that i burst outlaughing when the author stated as below:-

<quote>

…… Java EE 5 took a few pages out of Spring’s book and simplified the development of enterprise components with…….

</quote>

Yes, JEE sure did take some very crucial and important pages out of Spring book and because of which it is becoming superior day-by-day if not by huge margin. Spring with full support from its community is not far behind and with foundation in place, its building up components one after another. Its also adding in more and more support for open source libraries with ever growing sub-projects under the Spring Framework umbrella.

Also now its is slowly becoming a norm when considering a new open source for development to see how strongly it can be integrated into Spring and how seamless Spring configurations can be used. In my web service framework comparison, you wouldn’t believe, this was considered as one of the requirements and it will be a bonus for any open source to state that “they can be integrated into Spring” with some few lines of Java or XML configuration.

Spring has an edge by keeping it open for good open source libraries to plug on at any time and replace these libraries with a new one by mere configurations (at-least theoretically possible… :)). Java theoretically is “Write once run anywhere”, but is it truly that easy for a a production application to move from one environment to a new one… Hmmm… I don’t think so… 🙂

I am sort of exhausted already writing this blog. I will have to put you all in some suspense and put everything else that i plan to write in a new blog part. Hold on tight and i am sure i wouldn’t make you unhappy. Expect third part in another 3 days time. Its weekend here and i am really looking forward to it in a big way.

Page Visitors: 363

Spring Framework Vs. JEE – Part I

Am I playing with fire here. Perhaps Yes.

As many say, this truly classify itself to be a holy war for predominance and will surely stir some dirty stuff out into the air.

One of my colleagues recently came to me and had a small debate as to why the our organization selected Spring over JEE. He was very much in favor of JEE especially JEE 6.

We sometime back (more than an year) had an exercise in which the task was to choose organization wide Java framework. As you guessed right, we did have Spring Framework and JEE as strong candidates. Our approach was really methodical and a methodology which was devised internal to the organization was used.

In this methodology the two candidates was put into test. First of all, various architecture quality attributes were put in such as Performance, Scalability, Testability etc. and then various requirements in these attributes were classified/grouped together. Once we have that, we get the various stakeholders involved and get their buy in and their stand on the overall acceptance of these quality attributes and rank it. We then went on with POC (Proof of Concept) and each requirement was rated against each of the chosen frameworks.

Finally after a grueling exercise of rating various requirement for each of the framework, a final rating comes out which give good indication as to which on quality attributes gets favored for each of the framework and according to the stakeholders, which one to choose out of these two. The choice was made and there was no surprise, it was Spring Framework.

For our company now the chosen default framework is Spring and anything else if it is taken, it goes as an exception.

For me when i say that i use Spring Framework, it doesn’t in any way mean that i don’t use JEE. They cannot be mutually exclusive, rather when combined together, it gives immense advantage to the project that you are developing. We do use Spring as our choice of framework but haven’t just taken out JEE in any sense out of the window, rather when the right time comes another evaluation as detailed above will be done to asses the situation even further. Till that time i feel choosing Spring as the default framework is a good one in all aspects.

In my next blog (Click here), I would try to give some of the major points, which made us choose Spring as a default framework of choice for developing any application within the organization.

Earlier i have done couple of comparisons as detailed below:-

Spring Integration Vs. Apache Camel

Web Service Framework Comparison – 2013 – Part I

Web Service Framework Comparison – 2013 – Part II

Page Visitors: 882

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: 24534

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: 13874

Basic Core Java Concepts

First of all, advance bail on my behalf if my content is insignificant at this point. I thought of writing this blog post after searching through Google Trends and noting that there are good amount of search queries till now looking for these basic core java concepts.

I don’t want to split this blogs into multiple parts as i feel it wouldn’t be good for a reader to go onto various parts and would like to keep it an ever evolving blog post with full support from my readers.

[TODO]

My previous blogs could give some inputs in bulleted points to new Java developers as given below:-

Basic Java FAQ – Part 1

Basic Java FAQ – Part 2

Basic Java FAQ – Part 3

SCJP – Quick Notes

and finally and small mock example to check knowledge

Mock Exam

Page Visitors: 466

Post-Redirect-Get Pattern in JSF

Recently i have written a blog on how to implement PRG pattern in your Spring MVC web application in here. In this blog i would explain implementing this pattern in your JSF based web applications. I think this can be used for any JSF implementations like RichFaces, MyFaces etc.

Having said that, for me writing this blog post is very easy as i am not going to write it myself. Rather i would direct you to well written blog on the same topic by Ed Burns in the below URL:-

https://blogs.oracle.com/enterprisetechtips/entry/post_redirect_get_and_jsf

For AJAX based submissions there shouldn’t be any issues because these requests are being submitted by JavaScript.

Ed is currently the co-spec lead for JavaServer Faces. He is the coauthor of JavaServer Faces: The Complete Reference and the author of Secrets of the Rockstar Programmers. He is also the coauthor of the upcoming book JavaServer Faces 2.0: The Complete Reference. Read Ed Burns’s blog.

I wrote this blog so that i cover the two most used web application frameworks with respect to this pattern and its implementation.

Page Visitors: 1103

Post-Redirect-Get Pattern in Spring MVC

Do you have back button issues? Duplicate form submissions when you click back button? I think you have landed in right blog. With respect to Spring MVC, the blog even tries to give the complete solution by which to handle it.

One of the easiest way by which you can handle these issues is by implementing Post-Redirect-Get (PRG) pattern.

What is Post-Redirect-Get Pattern?

Post/Redirect/Get (PRG) is a web development design pattern that prevents duplicate form submission. When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original HTTP POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase.

To avoid this problem, PRG pattern is used, instead of returning a web page directly, the POST operation returns a redirection command. The HTTP 1.1 specification introduced the HTTP 303 response code to ensure that in this situation, the web user’s browser can safely refresh the server response without causing the initial HTTP POST request to be resubmitted.

I don’t want to re-invent the wheel, so i would just let the reader go through the Wikipedia which has explained it beautifully. Please follow the following URL:

http://en.wikipedia.org/wiki/Post/Redirect/Get

Beautiful well constructed figures in Wikipedia will make you understand this pattern in minutes.

I hope you would now have a clear picture of PRG pattern after going through the above URL and are ready to see this implemented in your Spring MVC application as detailed below in this blog post.

I really don’t know whether this is the best approach by which Post-Redirect-Get pattern can be achieved in Spring MVC. Nevertheless i am sharing an approach below by which PRG pattern can be achieved. If this is not at all right approach, or there is another good way by which to do it, please let me know through your comments to this blog post and i promise to correct it accordingly.

In Spring MVC PRG is implemented by having two request-mappings for each page one for GET and the other for POST. The page should always be rendered using GET method and the form should always use POST method for submitting data to controller. To achieve PRG after submitting the form in the POST method of the request should send the response with redirect to the page, by doing this the page will be rendered using GET request. The following a sample code.

The flow of the above example is {GET(prgTest.html} –> {POST(prgTest.html)} –> {REDIRECT(prgTest.html)} –> {GET(prgTest.html)}

I thank my dear friend Karthikeyan Vaithilingam, for allowing me to print his R&D on this in my blog. Please follow the following link to his other blog entries:

http://seenukarthi.com/blog/index.html

I would also suggest reader to go through another approach as given in the below blog post and implement whichever is the best approach in your web application.

http://www.mkyong.com/spring-mvc/handling-duplicate-form-submission-in-spring-mvc/

If you are using JSF or one its implementations like RichFaces as your choice of web application, to implement PRG, please blog my blog in here.

Page Visitors: 24944