Example usage for com.liferay.portal.kernel.messaging DestinationNames BACKGROUND_TASK_STATUS

List of usage examples for com.liferay.portal.kernel.messaging DestinationNames BACKGROUND_TASK_STATUS

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.messaging DestinationNames BACKGROUND_TASK_STATUS.

Prototype

String BACKGROUND_TASK_STATUS

To view the source code for com.liferay.portal.kernel.messaging DestinationNames BACKGROUND_TASK_STATUS.

Click Source Link

Usage

From source file:com.liferay.server.admin.web.internal.portlet.action.EditServerMVCActionCommand.java

License:Open Source License

protected void reindex(final ActionRequest actionRequest) throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    Map<String, Serializable> taskContextMap = new HashMap<>();

    String className = ParamUtil.getString(actionRequest, "className");

    if (!ParamUtil.getBoolean(actionRequest, "blocking")) {
        _indexWriterHelper.reindex(themeDisplay.getUserId(), "reindex",
                _portalInstancesLocalService.getCompanyIds(), className, taskContextMap);

        return;/*from ww w .  j  ava2  s . co  m*/
    }

    final String jobName = "reindex-".concat(_portalUUID.generate());

    final CountDownLatch countDownLatch = new CountDownLatch(1);

    MessageListener messageListener = new MessageListener() {

        @Override
        public void receive(Message message) throws MessageListenerException {

            int status = message.getInteger("status");

            if ((status != BackgroundTaskConstants.STATUS_CANCELLED)
                    && (status != BackgroundTaskConstants.STATUS_FAILED)
                    && (status != BackgroundTaskConstants.STATUS_SUCCESSFUL)) {

                return;
            }

            if (!jobName.equals(message.getString("name"))) {
                return;
            }

            PortletSession portletSession = actionRequest.getPortletSession();

            long lastAccessedTime = portletSession.getLastAccessedTime();
            int maxInactiveInterval = portletSession.getMaxInactiveInterval();

            int extendedMaxInactiveIntervalTime = (int) (System.currentTimeMillis() - lastAccessedTime
                    + maxInactiveInterval);

            portletSession.setMaxInactiveInterval(extendedMaxInactiveIntervalTime);

            countDownLatch.countDown();
        }

    };

    _messageBus.registerMessageListener(DestinationNames.BACKGROUND_TASK_STATUS, messageListener);

    try {
        _indexWriterHelper.reindex(themeDisplay.getUserId(), jobName,
                _portalInstancesLocalService.getCompanyIds(), className, taskContextMap);

        countDownLatch.await(ParamUtil.getLong(actionRequest, "timeout", Time.HOUR), TimeUnit.MILLISECONDS);
    } finally {
        _messageBus.unregisterMessageListener(DestinationNames.BACKGROUND_TASK_STATUS, messageListener);
    }
}