Executing the BackGround Task


In Liferay we can exceute the background task asynchrously.

The Background Task framework allows the developers to create and manage any kind of tasks that need to be run in a separated thread without the need of knowing anything about JAVA threading or concurrency

Here the code

ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

Map taskContextMap = null;
String taskName = null;
String servletContextNames [] = new String[1] ;

Portlet portlet = PortletLocalServiceUtil.getPortletById(request.getAttribute(WebKeys.PORTLET_ID).toString());
servletContextNames[0] = portlet.getContextName();

try {
BackgroundTaskLocalServiceUtil.addBackgroundTask(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), taskName, servletContextNames,TestBackgroundTaskExecutor.class, taskContextMap,new ServiceContext());
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}



Here we have registered the background task ,where TestBackgroundTaskExecutor.class is the background registered class where the execution code contains.

In the below code i am getting the portlet context name.

Portlet portlet = PortletLocalServiceUtil.getPortletById(request.getAttribute(WebKeys.PORTLET_ID).toString());
servletContextNames[0] = portlet.getContextName();

Since TestBackgroundTaskExecutor.class is resides in the same portlet, so we are getting the current portlet context name.

BackGround Class Implementaion is

Here we need to extend with the BaseBackgroundTaskExecutor and write the code.
public class TestBackgroundTaskExecutor extends BaseBackgroundTaskExecutor {
@Override
public BackgroundTaskResult execute(BackgroundTask backGroundTask) throws Exception {

System.out.println("In BackgroundTaskResult class"+backGroundTask);

return BackgroundTaskResult.SUCCESS;

}

}



https://www.liferay.com/web/daniel.kocsis/blog/-/blogs/background-task-a-k-a-the-secret-weapon-behind-the-new-asynchronous-staging

If you get the below exception as ,

liferay/background_task-1][BackgroundTaskMessageListener:133] Unable to execute background task
java.lang.ClassNotFoundException: com.test.portlet.TestBackgroundTaskExecutor

Check "TestBackgroundTaskExecutor"  and  registered class, it may be because of the contextPath.

Comments

  1. I got the error java.lang.ClassNotFoundException: while invoking the Background task in hook.
    So i just added my plug-in-name (hardcoded).. it worked.

    Portlet portlet =
    PortletLocalServiceUtil.getPortletById(request.getAttribute(WebKeys.
    PORTLET_ID).toString());

    portlet.getContextName() ---> Which returns the "project-module-name" for example "mi-wom-balance"

    servletContextNames[0] = "mi-wom-balance";

    BackgroundTaskLocalServiceUtil.addBackgroundTask(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
    taskName,
    servletContextNames,TestBackgroundTaskExecutor.class, taskContextMap,new ServiceContext());

    ReplyDelete

Post a Comment

Popular posts from this blog

How to know which liferay version we are using

Viewing the SQL Query in liferay : debugging the SQL Query in Hibernate