Scheduled Tasks in Liferay with Autowired

2019-08-17 23:52发布

问题:

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.