Our project has batch process which is scheduled to run at server start up and then every 24 hours subsequently. Below is configuration in the spring file.
<bean id="RenewalBatchSvc" class="com.rsaame.pas.renewals.scheduler.RenewalBatchService" >
<property name="renewalBatchSchedulerSvc" ref="RenewalBatchSchedulerSvc" />
</bean>
<bean id="RenewalBatchScheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- wait 20 seconds before starting repeated execution -->
<property name="delay" value="20000" />
<!-- run every 24 hrs 86400000-->
<property name="period" value="86400000" />
<property name="timerTask" ref="RenewalBatchSvc" />
</bean>
<bean id="RenewalBatchTimerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="RenewalBatchScheduledTask" />
</list>
</property>
</bean>
What this batch process does is, it takes all the policies which are to be renewed, from the batch table, and renew them. Each policy is taken, then renewal process is called. In the renewal process, we use a bean called "location" which is scoped as session. below is the definition.
<bean id="location" class="com.mindtree.ruc.cmn.utils.LoginLocation" scope="session">
<aop:scoped-proxy/>
</bean>
We don't use dispatcherservlet, but we use context listener as defined in below (in web.xml) :
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
Error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.location': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:341)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:33)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.getTarget(Cglib2AopProxy.java:653)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:604)
at com.mindtree.ruc.cmn.utils.LoginLocation$$EnhancerByCGLIB$$db19ad5f.getLocation(<generated>)
at com.mindtree.ruc.cmn.utils.LocationHandler.getLocation(LocationHandler.java:17)
at com.mindtree.ruc.cmn.utils.Utils.getSingleValueAppConfig(Utils.java:707)
Question:
The above exception will not occur when batch process runs at server start up. But it occurs only when batch is invoked after 24 hours. Why is it so ? If the exception is coming, then it should come even when batch is running at (IBM websphere) server startup. How come session is available at server startup even before application is totally up and nobody is using it?
In the places where we are defining beans as session/request scope, but we are using those beans during server start up (no session or request is made yet), how come, spring will not throw exception?