Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Liferay 7: Can somebody get the ModelListener sample to run?
Hello,
I tried to run this sample on Liferay 7.0-GA5 / Liferay 7.1-GA1 Wildfly Bundle.
The OSGi deployment was successful.
But the ModelListener won't get called.
Can someone help?
https://github.com/liferay/liferay-blade-samples/tree/master/maven/extensions/model-listener
Never had problems with ModelListeners, on Liferay 7 / DXP, this is a similar example but listening to article modifications, not the layout.
Full code example is below (with some own services commented out):
package au.com.fifthocean.cospaces.portal.manager.listener;
import org.osgi.service.component.annotations.Component;<br>
import org.osgi.service.component.annotations.Reference;
//import au.com.fifthocean.cospaces.portal.manager.CoSpacesManager;
import
com.liferay.portal.kernel.exception.ModelListenerException;<br>
import com.liferay.portal.kernel.log.Log;<br> import
com.liferay.portal.kernel.log.LogFactoryUtil;<br> import
com.liferay.portal.kernel.model.BaseModelListener;<br> import
com.liferay.portal.kernel.model.ModelListener;<br> import com.liferay.journal.model.JournalArticle;
@Component(immediate = true, service =
ModelListener.class)<br> public class ArticleApprovalListener
extends BaseModelListener<JournalArticle> {<br> private
static final Log logger = LogFactoryUtil.getLog(ArticleApprovalListener.class);
private final static int APPROVED_STATE = 0;<br>
@Override<br> public void onAfterUpdate(JournalArticle model)
throws ModelListenerException {<br> if(model.getStatus()
== APPROVED_STATE) {<br>
//this._spacesManager.enrollArticleForKPointAllocation(model);<br>
}<br> }
//@Reference<br> //private volatile CoSpacesManager _spacesManager;
}
Are you sure that your OSGI module is deployed and active?
Checked through other projects, found code which listens to page changes, also works on all LR7 / DXPs:
import org.osgi.service.component.annotations.Component;<br>
import org.osgi.service.component.annotations.Reference;
import
com.liferay.portal.kernel.exception.ModelListenerException;<br>
import com.liferay.portal.kernel.log.Log;<br> import
com.liferay.portal.kernel.log.LogFactoryUtil;<br> import
com.liferay.portal.kernel.model.BaseModelListener;<br> import
com.liferay.portal.kernel.model.ModelListener;<br> import com.liferay.portal.kernel.model.Layout;
import
au.com.fifthocean.portal.subject.search.manager.SearchPortalManager;<br>
import au.com.fifthocean.portal.subject.search.service.service.SearchSubjectLocalServiceUtil;
@Component(immediate = true, service =
ModelListener.class)<br> public class PageMutationListener extends
BaseModelListener<Layout> {<br> private static final Log
logger = LogFactoryUtil.getLog(PageMutationListener.class);<br>
<br> @Override<br> public void onAfterUpdate(Layout
model) throws ModelListenerException {<br> //
logger.info("layout updated " + model.getName());<br>
// check if layout is a subject home<br> try {<br>
Boolean isSubjectHome =
(boolean)model.getExpandoBridge().getAttribute(SearchPortalManager.customFieldIsSubjectHGome);<br>
if(isSubjectHome) {<br>
this._manager.updatePageIndex(model.getCompanyId(),
model.getPlid());<br> } else {<br>
// attempt to delete the index<br> // this is in
case the 'Is Subject Hiome' was turned to 'false'<br>
SearchSubjectLocalServiceUtil.deletePageIndexes(model.getCompanyId(),
model.getPlid());<br> }<br> }
catch(Exception e) {<br> logger.error("Unable to
access page custom field " +
SearchPortalManager.customFieldIsSubjectHGome, e);<br>
}<br> }<br> <br> @Reference<br> private
volatile SearchPortalManager _manager;
}
It does not work for me. I tried many types of ModelListeners but they don't get called.
The sample above should work.
I used the maven build. The module is active. But it does not work.
I also used the legacy method. (war file with portal.properties). Does not work either.
So it's hard to say what's the cause in your case, I used the same blade sample project (probably 6-8 months ago), all using maven.
May be try to get the tomcat LR7 bundle, as is, do not modify anything, check whether your module works on it. If it works, so the difference would be wildfly bundle-specifc troubles or some other settings. So far, we could not complain about any model listeners on our installations...
So to summarize the work of my last week:
There is no difference between Tomcat or Wildfly regarding Modellisteners.
Sometimes they work and sometimes they don't. Even if you redeploy the exactly same OSGi module.
The System.outs from the sample application won't show up on the console in any case. The same happens for log messages via JUL, SL4J and LOG4J too. These messages go to /dev/null . At least for Wildfly. You have to use the Liferay Logging Api. Any exception that happens in the Modellistener will go to /dev/null too and you won't see nothing, telling you your Modellistener does not work. I am using some 3rd party libs that are deployed together with my OSGi module. The 3rd party lib is using SL4J. So any messages this lib wants to tell you goes to /dev/null. If there was any dependecy problem with theses 3rd party libs in the OSGi module you can be 99% sure your module won't be able to be redeployed again. the console will show you that it has been redeployed but sometimes the code will not be replaced or sometimes the ModelListeners will not be registered.
Powered by Liferay™