掲示板

Spring Service objects are not available in Liferay Scheduler

thumbnail
9年前 に krishna mohan mathakala によって更新されました。

Spring Service objects are not available in Liferay Scheduler

Junior Member 投稿: 68 参加年月日: 12/09/08 最新の投稿
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
9年前 に David H Nebinger によって更新されました。

RE: Spring Service objects are not available in Liferay Scheduler

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
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
9年前 に Harish Kumar によって更新されました。

RE: Spring Service objects are not available in Liferay Scheduler

Expert 投稿: 483 参加年月日: 10/07/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 ?
8年前 に Agustinus Sri Setiawan によって更新されました。

RE: Spring Service objects are not available in Liferay Scheduler

New Member 投稿: 1 参加年月日: 14/12/09 最新の投稿
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