문제

I am having some problems when I try to implement a new function in my working servlet.

Now I have a servlet in which mobile phones can register. Mobile phones use rest to register against this servlet. And it works perfect. Anytime you try to register a phone, it works.

But now, I need to add a new functionality. I want to register this server against other component of my infrastructure.

I want that registration done at the very beggining. I mean, when the servlet starts, make the registration and forget about it, just work as it did before.

This is the error tomcat gives me:

Grave: The web application [/servletRegister] appears to have started a thread named [Timer-8] but has failed to stop it. This is very likely to create a memory leak.

This is my start class:

@Override
public Set<Class<?>> getClasses() {
    //-------------------------------
    //Set registration here
    //GatewayRegistrationHandler reg = GatewayRegistrationHandler.getInstance(); 
    //reg.registerDevice();
    //-------------------------------
    //register on a new thread due to process time      
    new Thread (new RegisterGatewayOnBackground()).start();

    //Next are the working servlet code
    Set<Class<?>> classes = new HashSet<Class<?>>();
    classes.add(PublicationsResource.class);  /
    classes.add(DeviceResource.class);
    return classes;
}

}

I tried the commented lines firstly. Then I got a memory leak and I tried to execute them in a new thread trying to avoid the leak. But the behavior is the same.

The background function is this:

public class RegisterGatewayOnBackground implements Runnable {
    public RegisterGatewayOnBackground() {
    }   
    public void run() {
        registerDevice();
    }
    private void registerDevice() {         
          GatewayRegistrationHandler reg = GatewayRegistrationHandler.getInstance(); 
          reg.registerDevice();   
    }
}

GatewayRegistrationHandler works fine because when I run the servlet, it executes, makes the registration and then, after that, crash. I thought it was a time problem and background would solve it but I am stuck here since background does the same.

I don't know any way to check where to find my memory leak. I am looking for advice or any tools which might help me solve the problem.

도움이 되었습니까?

해결책

When you start your thread like that, it will not be named "Timer-x". Therefore, this was probably a thread started elsewhere.

The message tomcat is giving you indicates that the webapp is somehow being undeployed (and then it checks for threads which are still there, and complains if there is). I'm not sure why the undeploy is happening, but if it's because you are stopping the webapp., you may not need to fix this unless you do (lots of) hot-deploys (deploying and undeploying while keeping the tomcat running). This is because, if it's leaking memory right before you are going to kill the process anyways, the memory leak won't have any harm and it would be waste of time to fix it.

If you want to fix it, one easy way is to hook a profiler and see who started this "Timer" thread.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top