java.net.BindException: Address already in use But process keeps on running

Hi,
I am using embedded jetty to start my spring MVC project. Here is my main file :

package org.directi.code;

import org.apache.log4j.PropertyConfigurator;
import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.log.JavaUtilLog;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class AppStart {
    private int port;
    private Server server;

    public static void main(String[] args) throws Exception {
        String env = System.getProperty("FLOCK_APPS_CONFIG");
        Properties props = new Properties();
        props.load(AppStart.class.getResourceAsStream("/properties/" + env + "/jetty.properties"));
        int port = Integer.parseInt(props.getProperty("port"));
        AppStart appStart = new AppStart(port);
        appStart.start();
        appStart.waitForInterrupt();
    }

    public AppStart(int port) {
        this.port = port;
    }

    public void start() throws Exception {
        server = new Server();
        ServerConnector connector = connector();
        server.addConnector(connector);
        WebAppContext webAppContext = getWebAppContext();
        server.setHandler(webAppContext);
        server.start();
    }

    private ServerConnector connector() {
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(port);
        return connector;
    }

    private WebAppContext getWebAppContext() throws IOException {
        WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        context.setDescriptor("webapp/WEB-INF/web.xml");
        context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
                ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/.*taglibs.*\\.jar$");
        context.setResourceBase(new ClassPathResource("webapp").getURI().toString());
        context.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
        context.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
        context.addBean(new ServletContainerInitializersStarter(context), true);
        context.setClassLoader(getUrlClassLoader());
        return context;
    }

    private List<ContainerInitializer> jspInitializers() {
        JettyJasperInitializer sci = new JettyJasperInitializer();
        ContainerInitializer initializer = new ContainerInitializer(sci, null);
        List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
        initializers.add(initializer);
        return initializers;
    }

    private ClassLoader getUrlClassLoader() {
        ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
        return jspClassLoader;
    }

    public void stop() throws Exception {
        server.stop();
    }

    public void waitForInterrupt() throws InterruptedException {
        server.join();
    }
}

Open in new window


I start the application like :
java -DFLOCK_APPS_CONFIG=staging -jar target/snippet.jar >>./snippet2.log 2>&1 &

Open in new window


In the log file i see the following output :
Exception in thread "main" java.net.BindException: Address already in use
	at sun.nio.ch.Net.bind0(Native Method)
	at sun.nio.ch.Net.bind(Net.java:433)
	at sun.nio.ch.Net.bind(Net.java:425)
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
	at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
	at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:326)
	at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
	at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:244)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.server.Server.doStart(Server.java:384)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.directi.code.AppStart.start(AppStart.java:54)
	at org.directi.code.AppStart.main(AppStart.java:35)

Open in new window


Now ideally my process should have shutdown. But i see the process running when i do
ps -ef | grep snippet
502 35205 34773   0  6:47PM ttys001    0:04.82 /usr/bin/java -DFLOCK_APPS_CONFIG=staging -jar target/snippet.jar

Even after the Exception is thrown. How can i make it stop if this happens .

Thanks
Rohit BajajAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
It's a bit hard to tell 100% what is happening but maybe Jetty has already started other threads and it is these threads that are keeping your app alive. Try the following...

package org.directi.code;

import org.apache.log4j.PropertyConfigurator;
import org.apache.tomcat.InstanceManager;
import org.apache.tomcat.SimpleInstanceManager;
import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
import org.eclipse.jetty.plus.annotation.ContainerInitializer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.log.JavaUtilLog;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.webapp.WebAppContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class AppStart {
    private int port;
    private Server server;

    public static void main(String[] args) throws Exception {
        String env = System.getProperty("FLOCK_APPS_CONFIG");
        Properties props = new Properties();
        props.load(AppStart.class.getResourceAsStream("/properties/" + env + "/jetty.properties"));
        int port = Integer.parseInt(props.getProperty("port"));
        AppStart appStart = new AppStart(port);
        try {
            appStart.start();
            appStart.waitForInterrupt();
        } finally {
            appStart.stop();
        }
    }

    public AppStart(int port) {
        this.port = port;
    }

    public void start() throws Exception {
        server = new Server();
        ServerConnector connector = connector();
        server.addConnector(connector);
        WebAppContext webAppContext = getWebAppContext();
        server.setHandler(webAppContext);
        server.start();
    }

    private ServerConnector connector() {
        ServerConnector connector = new ServerConnector(server);
        connector.setPort(port);
        return connector;
    }

    private WebAppContext getWebAppContext() throws IOException {
        WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        context.setDescriptor("webapp/WEB-INF/web.xml");
        context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
                ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/.*taglibs.*\\.jar$");
        context.setResourceBase(new ClassPathResource("webapp").getURI().toString());
        context.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
        context.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
        context.addBean(new ServletContainerInitializersStarter(context), true);
        context.setClassLoader(getUrlClassLoader());
        return context;
    }

    private List<ContainerInitializer> jspInitializers() {
        JettyJasperInitializer sci = new JettyJasperInitializer();
        ContainerInitializer initializer = new ContainerInitializer(sci, null);
        List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
        initializers.add(initializer);
        return initializers;
    }

    private ClassLoader getUrlClassLoader() {
        ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
        return jspClassLoader;
    }

    public void stop() throws Exception {
        if (server != null) {
            server.stop();
        }
    }

    public void waitForInterrupt() throws InterruptedException {
        if (server != null) {
            server.join();
        }
    }
}

Open in new window


Note the changes in the "main" method AND the changes to the last two methods in that class.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.