Java Code Examples for org.eclipse.jetty.server.nio.SelectChannelConnector#setPort()

The following examples show how to use org.eclipse.jetty.server.nio.SelectChannelConnector#setPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From idworker with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    Server server = new Server();

    SelectChannelConnector connector = new SelectChannelConnector();
    int port = Integer.parseInt(System.getProperty("port", "18001"));
    connector.setPort(port);
    server.addConnector(connector);

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(new ServletHolder(new IdWorkerServlet()), "/*");
    server.setHandler(context);

    server.start();
    server.join();

}
 
Example 2
Source File: JettyFactory.java    From base-framework with Apache License 2.0 6 votes vote down vote up
/**
 * 创建用于开发运行调试的Jetty Server, 以src/main/webapp为Web应用目录.
 */
public static Server createServerInSource(int port, String contextPath) {
	Server server = new Server();
	// 设置在JVM退出时关闭Jetty的钩子。
	server.setStopAtShutdown(true);

	SelectChannelConnector connector = new SelectChannelConnector();
	connector.setPort(port);
	// 解决Windows下重复启动Jetty居然不报告端口冲突的问题.
	connector.setReuseAddress(false);
	server.setConnectors(new Connector[] { connector });

	WebAppContext webContext = new WebAppContext(DEFAULT_WEBAPP_PATH, contextPath);
	server.setHandler(webContext);

	return server;
}
 
Example 3
Source File: JettyBootStrap.java    From demo-springmvc-shiro with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("ConstantConditions")
private static Server createServer(){
	Server server = new Server();
	server.setStopAtShutdown(true);
	SelectChannelConnector connector = new SelectChannelConnector();
	connector.setPort(port);
	connector.setReuseAddress(false);
	server.setConnectors(new Connector[]{connector});
	String webAppPath = JettyBootStrap.class.getClassLoader().getResource(".").getFile();
	webAppPath = webAppPath.substring(0, webAppPath.indexOf("target")) + "src/main/webapp";
	WebAppContext context = new WebAppContext(webAppPath, contextPath);
	server.setHandler(context);
	return server;
}
 
Example 4
Source File: JettyHelper.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static void setAddressAndPort(final Server jetty,
    final String bindAddress, final int port) {
  SelectChannelConnector connector = new SelectChannelConnector();
  connector.setPort(port);

  if (!StringUtils.isBlank(bindAddress)) {
    connector.setHost(bindAddress);
  }
  jetty.addConnector(connector);
}
 
Example 5
Source File: HessianServer.java    From MyTv with Apache License 2.0 5 votes vote down vote up
@Override
public void start() throws Exception {
	// 启动web应用
	org.eclipse.jetty.server.Server server = new Server();
	server.setStopAtShutdown(true);
	SelectChannelConnector connector = new SelectChannelConnector();
	connector.setPort(Config.NET_CONFIG.getHessianPort());
	connector.setHost(Config.NET_CONFIG.getIp());
	// 解决Windows下重复启动Jetty居然不报告端口冲突的问题.
	connector.setReuseAddress(false);
	server.setConnectors(new Connector[] { connector });
	// web配置
	WebAppContext context = new WebAppContext();
	String resourcePath = MyTvUtils.getRunningPath(HessianServer.class);
	logger.info("web app context path: " + resourcePath);
	context.setContextPath("/");
	String descriptor = resourcePath + "/WEB-INF/web.xml";
	logger.info("web app descriptor: " + descriptor);
	context.setDescriptor(descriptor);
	context.setResourceBase(resourcePath);
	context.setParentLoaderPriority(true);
	ClassLoader appClassLoader = Thread.currentThread()
			.getContextClassLoader();
	context.setClassLoader(appClassLoader);

	server.setHandler(context);
	server.start();
}
 
Example 6
Source File: JettyHelper.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private static void setAddressAndPort(final Server jetty,
    final String bindAddress, final int port) {
  SelectChannelConnector connector = new SelectChannelConnector();
  connector.setPort(port);

  if (!StringUtils.isBlank(bindAddress)) {
    connector.setHost(bindAddress);
  }
  jetty.addConnector(connector);
}
 
Example 7
Source File: Server.java    From maven-framework-project with MIT License 5 votes vote down vote up
protected Server() throws Exception {

        System.out.println("Starting Server");

        /**
         * Important: This code simply starts up a servlet container and adds
         * the web application in src/webapp to it. Normally you would be using
         * Jetty or Tomcat and have the webapp packaged as a WAR. This is simply
         * as a convenience so you do not need to configure your servlet
         * container to see CXF in action!
         */
        org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server();

        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(8080);
        server.setConnectors(new Connector[] {connector});

        WebAppContext webappcontext = new WebAppContext();
        webappcontext.setContextPath("/");

        webappcontext.setWar("D:\\developer\\workspace\\maven-framework-project\\cxf-soap-ws\\target\\cxf-soap-ws.war");

        HandlerCollection handlers = new HandlerCollection();
        handlers.setHandlers(new Handler[] {webappcontext, new DefaultHandler()});

        server.setHandler(handlers);
        server.start();
        System.out.println("Server ready...");
        server.join();
    }
 
Example 8
Source File: TestClient1.java    From rice with Educational Community License v2.0 4 votes vote down vote up
/**
   * Creates a Server that exposes the TestClient1 services via http and https
   *
   * @return the Server instance
   */
  @Override
  protected Server createServer() {

      // Need this CredentialsSourceFactory in our config to enable our test of basic auth
      // with our httpInvoker-echoServiceSecure

      registerTestCredentialsSourceFactory();

      ConfigConstants configConstants = new ConfigConstants();

      Server server = new Server();

      SelectChannelConnector connector0 = new SelectChannelConnector();
      connector0.setPort(configConstants.SERVER_HTTP_PORT);
      connector0.setMaxIdleTime(30000);
      connector0.setRequestHeaderSize(8192);

      SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector();

      ssl_connector.setPort(configConstants.SERVER_HTTPS_PORT);
      SslContextFactory cf = ssl_connector.getSslContextFactory();
      cf.setKeyStore(configConstants.KEYSTORE_PATH);
      cf.setKeyStorePassword(configConstants.KEYSTORE_PASS);
      cf.setKeyManagerPassword(configConstants.KEYSTORE_PASS);

      server.setConnectors(new Connector[]{connector0, ssl_connector});

      URL webRoot = getClass().getClassLoader().getResource(configConstants.WEB_ROOT);
      String location = webRoot.getPath();

      LOG.debug("#####################################");
LOG.debug("#");
LOG.debug("#  Starting Client1 using following web root " + location);
LOG.debug("#");
LOG.debug("#####################################");

      WebAppContext context = new WebAppContext();
      context.setResourceBase(location);
      context.setContextPath(configConstants.CONTEXT);

      HandlerCollection handlers = new HandlerCollection();
      handlers.addHandler(context);
      server.setHandler(handlers);

      server.setDumpAfterStart(true);
      //server.setDumpBeforeStop(true);

      return server;
  }
 
Example 9
Source File: WebSocketListener.java    From spring-integration-aws with MIT License 4 votes vote down vote up
@Override
public void start() {

	SelectChannelConnector wsConnector = new SelectChannelConnector();
	wsConnector.setHost(listenAddress);
	wsConnector.setPort(listenPort);
	wsConnector.setName("webSocket");
	wsConnector.setThreadPool(new QueuedThreadPool(10));
	server.setConnectors(new Connector[] { wsConnector });
	server.setHandler(new WebSocketHandler() {

		@Override
		public WebSocket doWebSocketConnect(HttpServletRequest request,
				String protocol) {

			log.debug("Request path:" + request.getRequestURI());
			String beanName = request.getRequestURI().replaceFirst("\\/",
					"");
			final WebsocketHandler handler = applicationContext.getBean(
					beanName, WebsocketHandler.class);

			return new WebSocket.OnTextMessage() {

				@Override
				public void onOpen(Connection connection) {
					connection.setMaxIdleTime(3600000);
					handler.setConnection(connection);
					handler.start();
				}

				@Override
				public void onClose(int code, String message) {
					handler.stop();
					log.info("Connection closed.");
				}

				@Override
				public void onMessage(String data) {
					handler.postMessage(data);
				}
			};
		}
	});

	serverThread.execute(new Runnable() {

		@Override
		public void run() {
			try {
				server.start();
				server.join();
			} catch (Exception e) {
				log.warn(e.getMessage(), e);
			}
		}
	});
}