How to make Java class run on startup using Context Listener without affecting other scheduled tasks?

1k views Asked by At

I am using Eclipse in Apache Tomcat server to execute my java based web project.

I have to run a function written in the class, "socket.java" only once, when application starts. I tried to initialize this class, i have other scheduled tasks running on other thread implemented by other servletContextListner. When i run this 'Socket.Java' on ServletContextListner, then the application is not working properly. Now the scheduled tasks on other ServletContextListner is only running once.

How to solve this?

my Listener class is as below,:-

package com.my.classes;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class SocketContextListner implements ServletContextListener{

    private Executor Task;
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        Task= Executors.newSingleThreadExecutor();      
        Socket b = new Socket();
        Task.execute(b);
    }
}

The other ServletContextListener with many scheduled task, which is not running when i implemented the Context listener for Socket

package com.my.classes;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class JMonitorContextListner implements ServletContextListener{

    private ScheduledExecutorService scheduler1;
    @Override
    public void contextDestroyed(ServletContextEvent event1) {
        // TODO Auto-generated method stub
        scheduler1.shutdownNow();
//      System.out.println("Context1 Destroyed");
    }

    @Override
    public void contextInitialized(ServletContextEvent event1) {
        // TODO Auto-generated method stub
//      System.out.println("Context1 initialized"); 
        scheduler1 = Executors.newSingleThreadScheduledExecutor();
        scheduler1.scheduleAtFixedRate(new Jmonitor(), 0, 10, TimeUnit.SECONDS);
        scheduler1.scheduleAtFixedRate(new FeedStatus(), 0, 10, TimeUnit.SECONDS);
        scheduler1.scheduleAtFixedRate(new AutoReset(), 0, 30, TimeUnit.SECONDS);
        scheduler1.scheduleAtFixedRate(new AutoLogoff(), 0, 1, TimeUnit.MINUTES);
    }

}

My web.xml file:

<listener>
<listener-class>com.my.classes.JMonitorContextListner</listener-class>
</listener>
<listener>
<listener-class>com.my.classes.SocketContextListner</listener-class>
</listener> 

My Console: enter image description here

console print such as The priority, ip from db and Null point exception are supposed to print again and again as it is from the scheduled task

Please suggest me, how to initialize the class "Socket.java" without affecting the normal working of application?

When i created separate listener for Socket.java and executed it on a different thread, my scheduled tasks on "JMonitorContextListner" stops after first run. Why is it happening?

Suggestion and any piece of code is highly appreciated.

0

There are 0 answers