Question

I have 2 grizzly applications I'd like to run side by side on different URLs on the same server. When I change my url from localhost to api.url.local for local debugging it throws a java.nio.channels.UnresolvedAddressException. Is there something I need to do in order for java to recognize another URL (ie - modify hosts file)? Or am I going in the wrong direction?

I'm currently on Windows but will be deploying to Linux. (If it were up to me, I'd run Linux)

public class Main {

    public static final URI BASE_URI = getBaseURI();

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost/").port(9998).build();
    }

    protected static HttpServer startServer() throws IOException {
        ResourceConfig rc = new PackagesResourceConfig("com.my.package.api.resources");
        rc.getFeatures()
            .put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

        return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
    }

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext annotationCtx = new AnnotationConfigApplicationContext(Config.class);

        HttpServer httpServer = startServer();

        System.out.println(String.format("Jersey app started with WADL available at " + "%sapplication.wadl\nHit enter to stop it...", BASE_URI, BASE_URI));
        System.in.read();
        httpServer.stop();
    }
}
Was it helpful?

Solution

Running two different applications under different urls on the same machine with one ip address is quite easy. But can require some setup depending on what you want to do.

My assumption is that you're doing this on your development machine. The biggest issue here is that the two servers require different port numbers.

Server #1
http://localhost:8080/

Server #2
http://localhost:8081/


If you want named instances instead. You will have to update your hosts file to point the named instance you want to your local machine. BUT, you'll still have to do separate ports per application.

Server #1 http://api.url.local:8080/

Server #2 http://api.url.local2:8081/

You will also have to update your tomcat (I'm assuming you're using tocmat) config. In the element, I believe you will have to update the 'name' attribute to be the name of your application's URL.


Friendly warning, if you are using the same IP Address, but different host names, SSL certificates will not work very well. Typically in that sort of setup you will need either a wildcard or unified communications certificate.


When you deploy to production, I would recommend using Apache HTTP Server as a proxy to your two applications and have two separate IP Address' unless you can use a SSL wildcard or a unified communications certificate.


This part of the answer is for if you wanted to run one application under two different URLs.

Proxy your application with a Apache Http Server instance and use mod_proxy to forward your two different server names or ip address' to the same Java instance.

My recommendation though, just run two instances of your application. Don't worry too much about resources, hardware is really cheep and worth a lot less then worrying about how to run one instance for two different sites.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top