Rohit Bajaj
asked on
Configuring Jetty parameter port using embedded Jetty
Hi,
I am using Embedded jetty to run my spring mvc application.
I am running it using
I am setting port like :
Here is my complete main java file :
d/staging
I want to put some jetty.properties file there which has port information corresponding to the environment
and then pick the proper port number.
What are ways i can do this ?
I think i will need to create a jetty.properties file having an entry like :
port=8080/11001
Then read that file and set the proper port number.
Please suggest if there are any better ways to do this ?
Thanks
I am using Embedded jetty to run my spring mvc application.
I am running it using
java -DFLOCK_APPS_CONFIG=staging/prod -jar snippet.jar
Now if the parameter is staging i need to run it on port 11001 and if its on prod i need to run in on 8080.I am setting port like :
private ServerConnector connector() {
ServerConnector connector = new ServerConnector(server);
connector.setPort(port);
return connector;
}
Here is my complete main java file :
package org.directi.code;
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;
public class AppStart {
private static final Logger LOG = LoggerFactory.getLogger(AppStart.class.getName());
private int port;
private Server server;
public static void main(String[] args) throws Exception {
Log.setLog(new JavaUtilLog());
LOG.info("robin suri");
String env = System.getProperty("FLOCK_APPS_CONFIG");
int port = Integer.parseInt(System.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();
}
}
And i am having folder structure liek : resources->properties->proI want to put some jetty.properties file there which has port information corresponding to the environment
and then pick the proper port number.
What are ways i can do this ?
I think i will need to create a jetty.properties file having an entry like :
port=8080/11001
Then read that file and set the proper port number.
Please suggest if there are any better ways to do this ?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.