Model Listeners are designed to listen for persistence events on models and perform lightweight actions in response. Model Listeners implement ModelListener interface. Each model can have it’s own model listener events either before or after model changes. There can be multiple listeners on a single model, and the order in which the listeners run can be controlled.
You can create a custom model listener in a module by following steps :
1. Create model listener class
Create a custom model listener class that extends BaseModelListener<T> as below
package ...;
import ...;
public class CustomModelListener extends BaseModelListener {
...
}
2. Register the model listener service
Register the model listener service with Liferay’s OSGi runtime by adding @Component annotation configuration as below
package ...;
import ...;
@Component(
immediate = true,
service = ModelListener.class
)
public class CustomModelListener extends BaseModelListener {
...
}
3. Listen for persistence events
Implement any methods of ModelListener interface and add your business logic into method body.
package ...;
import ...;
@Component(
immediate = true,
service = ModelListener.class
)
public class CustomModelListener extends BaseModelListener {
/*
* implement your desired methods of ModelListener interface to add your business logic
*/
}
The ModelListener interface provides a number of methods to listen for the model events
onBeforeAddAssociation | If there’s an association between two models, use this method to do something before an association record is added. |
onBeforeCreate | Use this method to do something before the persistence layer’s “create” method is called |
onBeforeUpdate | Use this method to do something before the persistence layer’s “update” method is called |
onBeforeRemoveAssociation | If there’s an association between two models, use this method to do something before a removal from the mapping table. |
onBeforeRemove | Use this method to do something before the persistence layer’s “remove” method is called |
onAfterAddAssociation | If there’s an association between two models, use this method to do something after an association record is added |
onAfterCreate | Use this method to do something after the persistence layer’s “create” method is called. |
onAfterUpdate | Use this method to do something after the persistence layer’s “update” method is called. |
onAfterRemoveAssociation | If there’s an association between two models, use this method to do something after an association record is removed. |
onAfterRemove | Use this method to do something after the persistence layer’s “remove” method is called. |
Please refer below example of Model Listener for User model
// CustomUserModelListener.java //
package ...;
import org.osgi.service.component.annotations.Component;
import com.liferay.portal.kernel.exception.ModelListenerException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.BaseModelListener;
import com.liferay.portal.kernel.model.ModelListener;
import com.liferay.portal.kernel.model.User;
@Component(
immediate = true,
service = ModelListener.class
)
public class CustomUserModelListener extends BaseModelListener {
private static final Log log = LogFactoryUtil.getLog(CustomUserMoledListener.class);
@Override
public void onBeforeCreate(User userModel) throws ModelListenerException {
log.info("in onBeforeCreate method");
log.info("userModel : " + userModel);
super.onBeforeCreate(userModel);
}
@Override
public void onAfterCreate(User userModel) throws ModelListenerException {
log.info("in onAfterCreate method");
log.info("userModel : " + userModel);
super.onAfterCreate(userModel);
}
}
You can test the given CustomUserModelListener by creating new user from Liferay control panel, You can see logs are getting printed in console when model listener is called.