留言板

Spring Service objects are not available in Liferay Scheduler

thumbnail
krishna mohan mathakala,修改在9 年前。

Spring Service objects are not available in Liferay Scheduler

Junior Member 帖子: 68 加入日期: 12-9-8 最近的帖子
Hi,

I am using Liferay 6.2 Spring portlet.
I have used the spring configuration file to configure all my beans.
I have implemented a Scheduler in Liferay called SimpleScheduler .
I have configured scheduler in xml file as below.

<bean id="ccheduler" class="com.getransportation.cwc.uma.scheduler.SimpleScheduler " >
<property name="reportService" ref="reportService"></property>


</bean>
I am not able to get the reportService object in the scheduler .
When I try to acces reportService object It is throwing null object.
Need help on How to get the reportService object in scheduler. Rest of the app is working fine.
thumbnail
David H Nebinger,修改在9 年前。

RE: Spring Service objects are not available in Liferay Scheduler

Liferay Legend 帖子: 14916 加入日期: 06-9-2 最近的帖子
Is this thing also defined in liferay-portlet.xml using the <scheduler-entry /> tag and passing in a message listener class?

If so, these guys are instantiated outside of spring and have no access to the spring context.

You could follow Liferay's model of using a singleton class (the various XxxUtil classes with a static member and static methods exposing access to the member), but there are other options as well, none of which include direct spring injection into your class.
thumbnail
Harish Kumar,修改在9 年前。

RE: Spring Service objects are not available in Liferay Scheduler

Expert 帖子: 483 加入日期: 10-7-31 最近的帖子
Hi David

I am facing the same issue. Instead of declaring the scheduler in liferay-portlet.xml using the <scheduler-entry /> tag, I have declared the scheduler bean definition in portlet's spring config xml file. Still I am getting the NPE for the injected services.

Its happening because of scheduler class is implementing the com.liferay.portal.kernel.messaging.MessageListener interface.

could you please suggest any solution ?
Agustinus Sri Setiawan,修改在8 年前。

RE: Spring Service objects are not available in Liferay Scheduler

New Member 发布: 1 加入日期: 14-12-9 最近的帖子
I also got the same problem and i solved it by implementing a custom ApplicationContextProvider as below:

ApplicationContextProvider.java
package com.test.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component("applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext context = null;

    public static ApplicationContext getApplicationContext() {
        return context;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
}


Please make sure spring discover this class. In my case, i add this line into my context xml file:
<bean id="applicationContextProvider" class="com.test.util.ApplicationContextProvider"/>

Then, you can use this ApplicationContextProvider to inject your service. For example:

ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
MyService myService = (MyService) applicationContext.getBean("myService");

MyScheduler.java
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;

import java.util.Date;
import java.util.List;

import org.springframework.context.ApplicationContext;

public class MyScheduler implements MessageListener {

	private static Log log = LogFactoryUtil.getLog(MyScheduler.class);

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

		log.info("receive - My scheduler is running... ");
		
		ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();

		MyService myService = (MyService) applicationContext.getBean("myService");
		
		List<!--?--> resultList = myService.findAll();
		
		if(resultList!=null &amp;&amp; !resultList.isEmpty()) {
			log.info("receive - There is "+resultList.size()+" record(s)");
			...
		}
		else {
			log.info("receive - There is no record. Skip the cron job. ");
		}
	}

}


source:
http://stackoverflow.com/questions/36011545/using-spring-autowired-service-classes-within-a-liferay-indexer
http://stackoverflow.com/questions/25711038/liferay-6-2-spring-autowired-not-working-in-a-hook