Total Pageviews

Saturday, October 20, 2012

Java Threads via Extending Thread class, implementing Runnable interface and Callable interface


If anybody asks - What are the two methods of using Threads in Java? The answer is on our tip of tongue- TWO, but further if it is asked what is the difference between threads implemented via extending Thread class and implementing runnable interface? we feel dificulty in explaining. Here some of my thoughts are presented to draw a line among them.
  1.  As we all know Java doesn't support multiple inheritance. If you want to extend the Thread class then it will make your class unable to extend other classes as java is having single inheritance feature whereas If you implement runnable interface, you can gain better object-oriented design and consistency and also avoid the single inheritance problems.
  2. In OOP, we extend a class for adding some new behaviour. If we are not making any modification on Thread than its a good OOP design to use Runnable interface.
  3. When we extend a thread class, all the Properties and Behaviours of Thread class and inherited by the subclass, its kind of overhead if we are not going to use it and the same can be achieved on ease with Runnable.
  4. Runnable interface represent Task and the same gives flexibility to get them executed by either Executors or Thread or any other means. So it draws a line between Task as Runnable than Thread as good design decision. However Executors accept Runnable as Task and they have worker thread which executes those tasks, but that's a totally different point. 
  5. While we are differentiating task as Runnable exhibits it can be reused and we are also provided with liberty to execute it from different means. As we know, a thread cannot be restarted once it's complete so again Runnable seems more promising. 


A Short Note on Callable and Runnable:
Both the interfaces are implemented by those classes who want to execute a thread of execution, but few notable differences are as follows:
  1. A Thread implemented via Callable interface may return a value, whereas its not possible if we are using a Runnable interface.
  2. A Thread implemented via Callable interface may throw checked exceptions, whereas its not possible if we are using a Runnable interface.


What I feel, the developers of Java felt a need of enhancing the capabilities of the Runnable interface, without changing the usage of the Runnable interface and that is why probably they've developed Callable interface.

I hope this will clear some of the doubt clouds about threads.

Thursday, October 18, 2012

Liferay: Reading cookies in Theme Template

We can read the cookies in theme template using the following code:
Hope this will help.

Sunday, October 14, 2012

Overriding Events: Working with PreActions and PostActions in Liferay using EXT

Liferay provides us facility to execute some code before and after of some specific events, namely Login, Logout and Service. Login and Logout are self-explanatory, Service is the event that takes place before or after every Action. For instance, If we have CustomServicePreAction and CustomLOginPreAction, in that case CustomServicePreAction will be called first then CustomLOginPreAction and then the LoginAction class will be invoked.
To achieve this, we need to make some entry in the portal-ext.properties, however some values are already present in portal.properties file, we have to update those properties, as following:
Here the our custom classes are: 

  1. com.liferay.portal.events.CustomServicePreAction, 
  2. com.liferay.portal.events.CustomServicePostAction, 
  3. com.liferay.portal.events.CustomLoginPreAction, 
  4. com.liferay.portal.events.CustomLoginPostAction,
  5. com.liferay.portal.events.CustomLogoutPreAction, and 
  6. com.liferay.portal.events.CustomLogoutPostAction.


Now we have to add the following classes in Ext:

1. CustomServicePreAction


2. CustomServicePostAction

3. CustomLoginPreAction

4. CustomLoginPostAction

5. CustomLogoutPreAction

6. CustomLogoutPostAction


And now, run your code. It will work.

Saturday, October 13, 2012

Creating Account Creation URL in Theme Velocity Template in LIferay


For creating URL for new account creation in theme velocity template, the following code can be used: