Scheduled Tasks in Liferay with Autowired

佐手、 提交于 2020-01-17 01:41:56

问题


im trying to create scheduled task in liferay portlet.

Liferay: 6.2

Spring: 3.1.4.RELEASE

With

    <scheduler-entry>
        <scheduler-description>test-scheduler</scheduler-description>
        <scheduler-event-listener-class>
            project.ScheduledProcesser
        </scheduler-event-listener-class>
        <trigger>

            <simple>
                <simple-trigger-value>
                    1
                </simple-trigger-value>
                <time-unit>minute</time-unit>
            </simple>

        </trigger>
    </scheduler-entry>

and the corrensponding class

@Component
public class ScheduledProcesser implements MessageListener {
private static Log log = LogFactoryUtil
            .getLog(ScheduledProcesser.class);


    @Autowired
    @Qualifier("myRequestService")
    private RequestService service;

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

        log.info("Starting");

        Request req = service.get("AAA746");

        if (req!=null)
        log.info("REQ -" + req.getId());

        log.info("Finished");
    }

The method is firing. But the service component is null. Normally is the service in other parts working well.

I have tried to find the solution, but maybe there is some settings missing. Thanx,


回答1:


MessageListener is not instantiated by Spring, but by Liferay (see the implementation of QuartzSchedulerEngine.getMessageListener(String, ClassLoader)). And Liferay just instantiates the class. So you can't autowire anything into a MessageListener that is defined in the liferay-portlet.xml.

But you could use PortalBeanLocatorUtil.locate instead, if your service is defined in the portal application context.



来源:https://stackoverflow.com/questions/31407340/scheduled-tasks-in-liferay-with-autowired

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