1

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

  1. Scheduling
@EnableScheduling
public class MyClass{

MyClass(){
}

@Scheduled(cron = "2 * * * * *")
public void myMethod() {
}
  1. Init
<bean id="myClass" class="com.abc.abc.billing.myClass"   init-method="readConfigScheduler"/>
  1. 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

applicationContext.xml is not getting loaded when I have kept the spring-servlet.xml in Web application

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

Community
  • 1
  • 1

2 Answers2

1

Seems you get the same problem which describe in this question

By default, websphere optimizes server start time and memory use by not starting a servlet until a request is received for the web application, if you want to load servlets when a web application is installed, add the following line to the server.xml configuration file or a file that it includes:

<webContainer deferServletLoad="false"/>
Community
  • 1
  • 1
Liping Huang
  • 4,378
  • 4
  • 29
  • 46
0

org.springframework.web.context.ContextLoaderListener

You may want to create your own ServletContextListener which should extend above ContextLoaderListener. That's how I do it. Make sure to call super.contextInitialized(...) in the contextInitialized method that you will implement, and you will have to replace above tag with your class name in the xml.

For example -

<listener>
  <listener-class>a.b.c.e.f.MyContextLoaderListener</listener-class>
</listener>
Sanjeev Dhiman
  • 1,169
  • 1
  • 11
  • 20
  • I tried the above . when i first but when i loaded it on server. but the DI i used to call the method failed in null pointer exception on first loadup(Looks like its not getting initalzed) Also when i restarted the server same issue , the listener didnt even start – Jashanpreet Singh Sep 06 '16 at 08:13
  • Code Used: public class MyListener extends ContextLoaderListener implements ServletContextListener{ public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } public void contextInitialized(ServletContextEvent arg0) { super.contextInitialized(arg0); System.out.println("Loaded"); } com.healthplan.emc.billing.MyListener This print statement didnt Print when i stopped and started the server – Jashanpreet Singh Sep 06 '16 at 08:28