org.eclipse.jetty.server.nio.SelectChannelConnector Java Examples

The following examples show how to use org.eclipse.jetty.server.nio.SelectChannelConnector. 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: VarOneServer.java    From varOne with MIT License 6 votes vote down vote up
private static Server setupJettyServer(VarOneConfiguration conf) {
	AbstractConnector connector = new SelectChannelConnector();
    

    // Set some timeout options to make debugging easier.
    int timeout = 1000 * 30;
    connector.setMaxIdleTime(timeout);
    connector.setSoLingerTime(-1);
    connector.setHost(conf.getServerAddress());
    connector.setPort(conf.getServerPort());

    final Server server = new Server();
    server.addConnector(connector);

    return server;
}
 
Example #2
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 #3
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 #4
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 #5
Source File: ContainerStarter.java    From ats-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Method for starting the Jetty server with the ATS Agent webapp.
 * @return the started server.
 * @throws IOException
 */
private static Server startServer() throws IOException {

    addAppender();

    final int agentPort = getAgentDefaultPort();
    log.info("Starting ATS agent at port: " + agentPort);

    final String jettyHome = getJettyHome();

    logSystemInformation(jettyHome);

    // start the server
    Connector connector = new SelectChannelConnector();
    connector.setPort(agentPort);

    Server server = new Server();
    server.setConnectors(new Connector[]{ connector });

    WebAppContext webApp = new WebAppContext();
    webApp.setContextPath("/agentapp");
    webApp.setWar(jettyHome + "/webapp/agentapp.war");
    webApp.setAttribute("org.eclipse.jetty.webapp.basetempdir",
                        getJettyWorkDir(jettyHome));

    server.setHandler(webApp);
    server.setStopAtShutdown(true);

    setExtraClasspath(webApp, jettyHome);

    try {
        server.start();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

    log.info("ATS agent started");
    return server;
}
 
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: 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 #8
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 #9
Source File: StramTestSupport.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public void start() throws Exception
{
  server = new Server();
  Connector connector = new SelectChannelConnector();
  connector.setPort(port);
  server.addConnector(connector);

    // Setup the basic application "context" for this application at "/"
  // This is also known as the handler tree (in jetty speak)
  ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
  contextHandler.setContextPath("/");
  server.setHandler(contextHandler);
  WebSocketServlet webSocketServlet = new WebSocketServlet()
  {
    @Override
    public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
    {
      return websocket;
    }
  };

  contextHandler.addServlet(new ServletHolder(webSocketServlet), "/pubsub");
  server.start();
  if (port == 0) {
    port = server.getConnectors()[0].getLocalPort();
  }
}
 
Example #10
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 #11
Source File: HttpConductorImpl.java    From sql-layer with GNU Affero General Public License v3.0 4 votes vote down vote up
public ConnectionMonitor(SelectChannelConnector connector) {
    this.connector = connector;
}
 
Example #12
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 #13
Source File: Main.java    From dexter with Apache License 2.0 4 votes vote down vote up
private void start() {
	// Start a Jetty server with some sensible(?) defaults
	try {
		Server srv = new Server();
		srv.setStopAtShutdown(true);

		// Allow 5 seconds to complete.
		// Adjust this to fit with your own webapp needs.
		// Remove this if you wish to shut down immediately (i.e. kill <pid>
		// or Ctrl+C).
		srv.setGracefulShutdown(5000);

		// Increase thread pool
		QueuedThreadPool threadPool = new QueuedThreadPool();
		threadPool.setMaxThreads(100);
		srv.setThreadPool(threadPool);

		// Ensure using the non-blocking connector (NIO)
		Connector connector = new SelectChannelConnector();
		connector.setPort(port);
		connector.setMaxIdleTime(30000);
		srv.setConnectors(new Connector[] { connector });

		// Get the war-file
		ProtectionDomain protectionDomain = Main.class
				.getProtectionDomain();
		String warFile = protectionDomain.getCodeSource().getLocation()
				.toExternalForm();
		String currentDir = new File(protectionDomain.getCodeSource()
				.getLocation().getPath()).getParent();

		// Handle signout/signin in BigIP-cluster

		// Add the warFile (this jar)
		WebAppContext context = new WebAppContext(warFile, contextPath);
		context.setServer(srv);
		resetTempDirectory(context, currentDir);

		// Add the handlers
		HandlerList handlers = new HandlerList();
		handlers.addHandler(context);
		handlers.addHandler(new ShutdownHandler(srv, context, secret));
		handlers.addHandler(new BigIPNodeHandler(secret));
		srv.setHandler(handlers);

		srv.start();
		srv.join();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #14
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);
			}
		}
	});
}