Last active
October 31, 2016 03:20
-
-
Save eclecticlogic/9049595 to your computer and use it in GitHub Desktop.
Creating a Jetty server with public and private connectors and setting up Jersey as a public servlet and one private servlet. Also bootsrap Spring. All this done without web.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JettyBootstrap { | |
private static Logger logger = LoggerFactory.getLogger(JettyBootstrap.class); | |
private String bindAddress; | |
private int port; | |
public void start() { | |
try (AbstractApplicationContext context = new ClassPathXmlApplicationContext( | |
new String[] { "ioc/spring-context-main.xml" })) { | |
run(context); | |
} // end try | |
} | |
public void run(ApplicationContext context) { | |
Server server = new Server(); | |
{ | |
ServerConnector connector = new ServerConnector(server); | |
connector.setHost(getBindAddress()); | |
connector.setPort(getPort()); | |
connector.setName("main"); | |
server.addConnector(connector); | |
} | |
{ | |
ServerConnector connector = new ServerConnector(server); | |
connector.setHost(getBindAddress()); | |
connector.setPort(7007); | |
connector.setName("internal"); | |
server.addConnector(connector); | |
} | |
JerseyApplication application = context.getBean(JerseyApplication.class); | |
ContextHandlerCollection handlerCollection = new ContextHandlerCollection(); | |
{ | |
ServletContainer servlet = new ServletContainer(application); | |
ServletContextHandler handler = new ServletContextHandler(); | |
ServletHolder holder = new ServletHolder(servlet); | |
handler.addServlet(holder, "/abc/*"); | |
handler.setVirtualHosts(new String[] {"@main"}); | |
handlerCollection.addHandler(handler); | |
} | |
{ | |
MyAdminServlet servlet = new MyAdminServlet(); | |
ServletContextHandler handler = new ServletContextHandler(); | |
ServletHolder holder = new ServletHolder(servlet); | |
handler.addServlet(holder, "/def/*"); | |
handler.setVirtualHosts(new String[] {"@internal"}); | |
handlerCollection.addHandler(handler); | |
} | |
server.setHandler(handlerCollection); | |
try { | |
server.start(); | |
server.join(); | |
} catch (Exception e) { | |
logger.error(e.getMessage(), e); | |
} | |
} | |
public String getBindAddress() { | |
return bindAddress; | |
} | |
public void setBindAddress(String bindAddress) { | |
this.bindAddress = bindAddress; | |
} | |
public int getPort() { | |
return port; | |
} | |
public void setPort(int port) { | |
this.port = port; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment