Liferay Scheduler not working after server restart

空扰寡人 提交于 2019-12-05 17:15:23
Tobias Liefke

This seems to be an issue with the implementation of Liferay scheduling. Quartz is correctly storing and restoring your trigger and the job. But Liferay isn't using your MessageListener as a job. Instead it will wrap your MessageListener in a MessageSenderJob and it will register your MessageListener.

The MessageSenderJob will still be triggered after restart, and it will send your message to the message bus. But if you didn't registered your MessageListener until that point, their will be no receiver of that message.

Solution: You will have to register your MessageListener on every startup. Either by calling scheduling SchedulerEngineHelperUtil.schedule again, or by calling MessageBusUtil.registerMessageListener. See my question here for some options to register startup actions.

Here an example in case you want to create the trigger dynamically (due to some action in the UI):

@WebListener
public class SchedulerListener implements ServletContextListener, PortalLifecycle, MessageListener {

    private SchedulerEventMessageListenerWrapper listenerWrapper;

    public void contextInitialized(final ServletContextEvent sce) {
        // Wait until the portal is ready
        PortalLifecycleUtil.register(this, PortalLifecycle.METHOD_INIT);
    }

    public void portalInit() {
        // Register our listener
        listenerWrapper = new SchedulerEventMessageListenerWrapper();

        listenerWrapper.setGroupName(getClass().getName());
        listenerWrapper.setJobName(getClass().getName());
        listenerWrapper.setMessageListener(this);

        listenerWrapper.afterPropertiesSet();

        MessageBusUtil.registerMessageListener(DestinationNames.SCHEDULER_DISPATCH, listenerWrapper);
    }

    public void contextDestroyed(final ServletContextEvent event) {
        // Unregister
        if (listenerWrapper != null) {
            MessageBusUtil.unregisterMessageListener(DestinationNames.SCHEDULER_DISPATCH, listenerWrapper);
        }
    }

    public void portalDestroy() {
        // Ignore
    }

    public void receive(final Message message) {
        // ... your job code here ...
    }

}

If you want to have a fixed trigger instead, you can drop the listenerWrapper and put your SchedulerEngineHelperUtil.schedule(...) code from your question into portalInit().

If you want to know, what's the point of StorageType.PERSISTED: It's for executing triggers that would have been fired while the server was down or that had just started as the server was going down.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!