掲示板

Multiple Hook Plugins with the same Model Listener

thumbnail
11年前 に Brian Russell によって更新されました。

Multiple Hook Plugins with the same Model Listener

Junior Member 投稿: 41 参加年月日: 11/10/31 最新の投稿
I've searched through all of the documentation and message board posts I can find, but I have not found anything that addresses this question:

Is it possible to have more than one Hook Plugin deployed, which references the same value object listener model object?

My example is that I have 3 separate hook plugins, each needing to know when a new company is created, so that they will fire off their startup actions. I ended up having to use the Group listener, but regardless of what I am listening to, the issue is that only one of the listeners seems to get registered at a time (seemingly the last hook plugin loaded)

The portal.properties for the hooks have the following relevant properties set:
Hook 1
application.startup.events=com.closerware.websitesocial.portal.events.CwCalendarAppStartupAction
value.object.listener.com.liferay.portal.model.Group=com.closerware.websitesocial.portal.model.listeners.CalendarGroupListener

Hook 2
application.startup.events=com.closerware.websitesocial.portal.events.CwPermissionAppStartupAction
value.object.listener.com.liferay.portal.model.Group=com.closerware.websitesocial.portal.model.listeners.PermissionGroupListener

Hook 3
application.startup.events=com.closerware.websitesocial.portal.events.CwShoppingAppStartupAction
value.object.listener.com.liferay.portal.model.Group=com.closerware.websitesocial.portal.model.listeners.ShoppingGroupListener

The key here is that the listener classes all extend BaseModelListener<Group> to utilize the onAfterCreate(Group group) method, which fires off the startup events. The startup actions all work correctly during server startup. But only Hook 3's startup event is triggered upon creation of a new Group. So, it is probably all too obvious, but it seems that the last plugin to be loaded (Hook 3) is the only one actually having the value.object.listener property set for its Model Listener, as each hook likely overwrites the value.object.listener.com.liferay.portal.model.Group property of the previous hook.

The obvious solution is to combine the hooks into one and run all of the startup events from a single listener, but I'm trying to maintain a separation of interests in order to modularize the code.

Am I missing something here with declaring the value.object.listener properties, so that they do not overwrite each other? Or is it only possible to declare a single value.object.listener property per model object globally across all plugins?

Our Platform Setup
Liferay 6.1.0 CE GA1 (20120106155615760)
Tomcat 7.0.23
MySQL 5.5.20
Java 1.6.0_31
thumbnail
11年前 に Brian Russell によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Junior Member 投稿: 41 参加年月日: 11/10/31 最新の投稿
Oh, and just to add some more evidence that the properties overwrite each other. If I undeploy Hook 3, then the listener for Hook 2 is the only one that fires. And so on, down the line.
thumbnail
11年前 に Brian Russell によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Junior Member 投稿: 41 参加年月日: 11/10/31 最新の投稿
Is there anyone who has experienced the same issue and come up with a solution? Or any advice out there from the community?

This is still an issue with my hooks, where I have a number of different hooks which require something to fire on Group creation, but as it is, only one is fired.
thumbnail
11年前 に Alireza Zare によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Regular Member 投稿: 110 参加年月日: 10/09/03 最新の投稿
Why not handling them in the same code?
thumbnail
11年前 に Alireza Zare によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Regular Member 投稿: 110 参加年月日: 10/09/03 最新の投稿
for modularization purpose you can just use the same interface and do the modularization in your implementation.
thumbnail
11年前 に Brian Russell によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Junior Member 投稿: 41 参加年月日: 11/10/31 最新の投稿
I agree that I can just use one hook and put all in one code. But I shouldn't *have* to. One of the advantages of separating my customizations into separate hooks is that I can add/remove those customizations as needed. Each hook is focused on different functionality (i.e. shopping portlet, message boards, dockbar, permissions, etc.)

In looking at the code where the listeners are initialized, it would seem that they could simply be added to the list, rather than overwrite the list.

Here's a little chunk of that code where the listener is set for the Group model object from com.liferay.portal.service.persistence.GroupPersistenceImpl:
	
        public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.portal.util.PropsUtil.get(
						"value.object.listener.com.liferay.portal.model.Group")));

		if (listenerClassNames.length &gt; 0) {
			try {
				List<modellistener<group>&gt; listenersList = new ArrayList<modellistener<group>&gt;();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<group>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
</group></modellistener<group></modellistener<group>

So each time it is reinitializing the "listeners" array, which is an instance variable.

This also means that the base listeners will then no longer be called if you were to add a listener for say, User. If I set up, in my hook properties file, a value object listener for User, it would overwrite the call to com.liferay.portal.model.UserListener

So the base portal.properties has:
    
    value.object.listener.com.liferay.portal.model.Contact=com.liferay.portal.model.ContactListener
    value.object.listener.com.liferay.portal.model.Layout=com.liferay.portal.model.LayoutListener,com.liferay.portal.service.impl.LayoutSetPrototypeLayoutListener
    value.object.listener.com.liferay.portal.model.LayoutSet=com.liferay.portal.model.LayoutSetListener,com.liferay.portal.service.impl.LayoutSetPrototypeLayoutSetListener
    value.object.listener.com.liferay.portal.model.PortletPreferences=com.liferay.portal.model.PortletPreferencesListener
    value.object.listener.com.liferay.portal.model.User=com.liferay.portal.model.UserListener
    value.object.listener.com.liferay.portal.model.UserGroup=com.liferay.portal.model.UserGroupListener
    value.object.listener.com.liferay.portlet.journal.model.JournalArticle=com.liferay.portlet.journal.model.JournalArticleListener
    value.object.listener.com.liferay.portlet.journal.model.JournalTemplate=com.liferay.portlet.journal.model.JournalTemplateListener

If in your hook portal.properties you were to add your own listener to any of these model objects, you'd lose the listeners listed above. That can't be right can it? But I suppose it's possible that it would be exactly what someone would want, in order to override the base listeners.
thumbnail
11年前 に Dave Weitzel によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Regular Member 投稿: 208 参加年月日: 09/11/18 最新の投稿
Did you ever find an answer to this problem?
I may be hitting the same issue (or at least "deleting" the standard Liferay Listener capability by writing my own, and with Liferay's base listener dependding on portal-impl I may not be able to clone it fully.
thumbnail
11年前 に Brian Russell によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Junior Member 投稿: 41 参加年月日: 11/10/31 最新の投稿
Unfortunately Dave, I ended up just giving in and combining all of my hooks into one, which includes all of my code updating various modules. So by doing this, it cleared up this issue of hook 1 listener being overwritten by hook 2 listener. So that is the only solution I can offer you.

Though I do believe the base liferay listeners are still intact when doing this.
thumbnail
10年前 に Ranga Rao Bobbili によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Regular Member 投稿: 152 参加年月日: 07/07/20 最新の投稿
Hi,

I am also facing similar kind of issue while loading portal properties from hook(overriding portal.properties) and ext environment(portal-ext.properties).

Basically session timeout related properties are not loading when i use properties hook and ext environment(portal-ext.properties).

Thanks in advance.......

Thanks,
Ranga Rao Bobbili
Adaequare INC
thumbnail
5年前 に Enrique Valdes Lacasa によって更新されました。

RE: Multiple Hook Plugins with the same Model Listener

Junior Member 投稿: 92 参加年月日: 14/07/29 最新の投稿

Hello Brian.

 

I'm answering after a while but read this and wanted to give some feedback.

 

In the Liferay official online documentation about model listeners for Liferay 6.2 there's a note that says:

 

"Only modify a portal property that accepts a single value from a single hook plugin. If the property’s value is overridden from multiple plugins, Liferay won’t know which value to use."

 

So it seems like what you were trying to do was not possible.

 

Regards.