Getting notified about model changes in Liferay

I came across the problem that I had to know how often the Users tend to update their profile picture. After a little research I found that the best way to do this are the Liferay Model Listeners. You define them like this:

Create your ModelListener class.

At first, create the class that will be notified about model changes and updates. You need to implement the ModelListener interface (com.liferay.portal.model.ModelListener) like this:

public class TestListener implements ModelListener<BlogsEntry> {

This class allows to listen to all changes regarding Blog entries in Liferay. The following methods can be implemented:

  • onAfterAddAssociation
  • onAfterCreate
  • onAfterRemove
  • onAfterRemoveAssociation
  • onAfterUpdate
  • onBeforeAddAssociation
  • onBeforeCreate
  • onBeforeRemove
  • onBeforeRemoveAssociation
  • onBeforeUpdate

It should be pretty obvious what they do and when they are called.

Referencing in Liferay 5.1

In Liferay 5.1 you reference the model listener by adding the following to your lilferay-hook.xml:


Referencing in Liferay 5.2 and later

In Liferay 5.2 and later you create a portal.properties file which contains your model listener and reference that in your liferay-hook.xml:





If you liked this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.