Foren
Spring Service objects are not available in Liferay Scheduler
krishna mohan mathakala, geändert vor 9 Jahren.
Spring Service objects are not available in Liferay Scheduler
Junior Member Beiträge: 68 Beitrittsdatum: 08.09.12 Neueste Beiträge
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.
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.
David H Nebinger, geändert vor 9 Jahren.
RE: Spring Service objects are not available in Liferay Scheduler
Liferay Legend Beiträge: 14916 Beitrittsdatum: 02.09.06 Neueste Beiträge
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.
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.
Harish Kumar, geändert vor 9 Jahren.
RE: Spring Service objects are not available in Liferay Scheduler
Expert Beiträge: 483 Beitrittsdatum: 31.07.10 Neueste Beiträge
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 ?
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, geändert vor 8 Jahren.
RE: Spring Service objects are not available in Liferay Scheduler
New Member Beitrag: 1 Beitrittsdatum: 09.12.14 Neueste Beiträge
I also got the same problem and i solved it by implementing a custom ApplicationContextProvider as below:
ApplicationContextProvider.java
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
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
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 && !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