Blogs
UPDATE: 2019/01/11: Please refer to Kyle Stiemann's blog titled How can I share session data across portlets in Liferay? which discusses the various benefits and drawbacks of data sharing approaches that are now available.
If you want to share data between portlets, a common way to do that is by storing data in the user's session. Liferay Portal has some nice features for sharing session data like <shared-session-attributes> in the liferay-portlet.xml file, and shared.session.attributes in the portal-ext.properties file.
But what if you're trying to go session-less? Or what if you want to share data globally for all users across sessions?
One way to do share data like this is to store the data in the portal's ROOT context. The first step is to create a GlobalStartupAction in the Liferay EXT environment, like this:
package com.foo.liferay.startup; public class GlobalStartupAction extends SimpleAction { private static List<String> fooList; public List<String> getFooList() { return fooList; } public static void @Override public void run(String[] ids) throws ActionException { // Cache all the data fooList = new ArrayList<String>(); fooList.add(new String("red")); fooList.add(new String("green")); fooList.add(new String("blue")); } }
Then, the GlobalStartupAction needs to be registered in the portal-ext.properties file. There is already a global startup action specified there for Liferay's own startup purposes, but thankfully this value can be a comma-delimited list, so we can just add the GlobalStartupAction after the Liferay one:
global.startup.events=com.liferay.portal.events.GlobalStartupAction,com.foo.liferay.startup.GlobalStartupAction
And then inside each portlet, use the magic Liferay PortalClassInvoker to call the static getFooList() method. The PortalClassInvoker utility figures out the classloader for the Liferay Portal context before calling the specified method:
import com.liferay.portal.kernel.util.PortalClassInvoker; public class ApplicationScopeBean { private static final List fooList; public ApplicationScopeBean() { List<String> fooList = (List<String>) PortalClassInvoker.invoke("com.foo.liferay.startup.GlobalStartupAction", "getFooList"));
} }