Hello, I want to run some job just after loading the Spring context but I do not know how to do this. have you any idea how to do that? thanks for your help
You can write a bean class that implements the org.springframework.context.Lifecycle
interface. Add this bean to your context, and the start()
method will be invoked by the container once that context has finished starting up.
Another possibility would be to register a listener to application context events (). Basically it's the same as skaffman's solution, just implement:
org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
instead of Lifecycle. It has only one method instead of three. :-)
Hello,
thank you all for your reply. In fact I missed a little detail in my question, I wanted to run Quartz Job just after loading the application context.. I tried the solution stakfeman, but I had some problems running the Quartz Jobs. Finally I found that solution: Use Quartz within Spring ,here is the code:
<!--
===========================Quartz configuration====================
-->
<bean id="jobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="processLauncher" />
<property name="targetMethod" value="execute" />
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<!-- see the example of method invoking job above -->
<property name="jobDetail" ref="jobDetail" />
<!-- 10 seconds -->
<property name="startDelay" value="10000" />
<!-- repeat every 50 seconds -->
<property name="repeatInterval" value="50000" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
thank you again for the help and I apologize if the question was not very clear ':(