I have been trying to run an a method as soon as server starts . (As per my understanding Issue could be Context not getting loaded at Server start or restart). This below Works on Tomcat It feels Like Websphere is doing a lazy initialization which it should not
When i Deploy the component on Websphere or restart it the context is not loaded unless i hit the URL eg "http://localhost/myapp/"
I have tried 3 Methods to run the method
- Scheduling
@EnableScheduling
public class MyClass{
MyClass(){
}
@Scheduled(cron = "2 * * * * *")
public void myMethod() {
}
- Init
<bean id="myClass" class="com.abc.abc.billing.myClass" init-method="readConfigScheduler"/>
- ApplicationListener
@Component
public class StartUp implements ApplicationListener<ContextRefreshedEvent>
{
@Autowired
private MyClass cls;
@Override
public void onApplicationEvent(ContextRefreshedEvent arg0)
{
System.out.println("ContextLoaded");
cls.MyMethod();
}
}
As soon as i hit the URL "http://localhost/myapp/" all the above three method Works.
i have already tried the below links for help but i am not sable to make it work.
Execute method on startup in spring
Also i talked to few people which said its working when they tried but on later investigation i found out that when u deploy the app through eclipse , it opens the url for app automatically which loads the context.
So when we restarted the app it didnt work
My Web.xml looks something like this
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>MyApp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/MyApp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
I am quite new to Spring so any help would be good