org.eclipse.jetty.websocket.jsr356.server.ServerContainer Java Examples

The following examples show how to use org.eclipse.jetty.websocket.jsr356.server.ServerContainer. 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: Application.java    From rest-utils with Apache License 2.0 7 votes vote down vote up
final Handler configureWebSocketHandler() throws ServletException {
  final ServletContextHandler webSocketContext =
          new ServletContextHandler(ServletContextHandler.SESSIONS);
  webSocketContext.setContextPath(
          config.getString(RestConfig.WEBSOCKET_PATH_PREFIX_CONFIG)
  );

  configureSecurityHandler(webSocketContext);

  ServerContainer container =
          WebSocketServerContainerInitializer.configureContext(webSocketContext);
  registerWebSocketEndpoints(container);

  configureWebSocketPostResourceHandling(webSocketContext);
  applyCustomConfiguration(webSocketContext, WEBSOCKET_SERVLET_INITIALIZERS_CLASSES_CONFIG);

  return webSocketContext;
}
 
Example #2
Source File: KsqlRestApplication.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
@Override
protected void registerWebSocketEndpoints(ServerContainer container) {
  try {
    final ListeningScheduledExecutorService exec = MoreExecutors.listeningDecorator(
        Executors.newScheduledThreadPool(
            config.getInt(KsqlRestConfig.KSQL_WEBSOCKETS_NUM_THREADS),
            new ThreadFactoryBuilder()
                .setDaemon(true)
                .setNameFormat("websockets-query-thread-%d")
                .build()
        )
    );
    final ObjectMapper mapper = getJsonMapper();
    final StatementParser statementParser = new StatementParser(ksqlEngine);

    container.addEndpoint(
        ServerEndpointConfig.Builder
            .create(
                WSQueryEndpoint.class,
                WSQueryEndpoint.class.getAnnotation(ServerEndpoint.class).value()
            )
            .configurator(new Configurator() {
              @Override
              @SuppressWarnings("unchecked")
              public <T> T getEndpointInstance(Class<T> endpointClass) {
                return (T) new WSQueryEndpoint(
                    mapper,
                    statementParser,
                    ksqlEngine,
                    exec
                );
              }
            })
            .build()
    );
  } catch (DeploymentException e) {
    log.error("Unable to create websockets endpoint", e);
  }
}
 
Example #3
Source File: InstWebSocketServerContainerInitializer.java    From dropwizard-websockets with MIT License 5 votes vote down vote up
public static ServerContainer configureContext(final MutableServletContextHandler context, final MetricRegistry metrics) throws ServletException {
    WebSocketUpgradeFilter filter = WebSocketUpgradeFilter.configureContext(context);
    NativeWebSocketConfiguration wsConfig = filter.getConfiguration();
    
    ServerContainer wsContainer = new ServerContainer(wsConfig, context.getServer().getThreadPool());
    EventDriverFactory edf = wsConfig.getFactory().getEventDriverFactory();
    edf.clearImplementations();

    edf.addImplementation(new InstJsrServerEndpointImpl(metrics));
    edf.addImplementation(new InstJsrServerExtendsEndpointImpl(metrics));
    context.addBean(wsContainer);
    context.setAttribute(javax.websocket.server.ServerContainer.class.getName(), wsContainer);
    context.setAttribute(WebSocketUpgradeFilter.class.getName(), filter);
    return wsContainer;
}
 
Example #4
Source File: SaslTest.java    From rest-utils with Apache License 2.0 5 votes vote down vote up
@Override
protected void registerWebSocketEndpoints(final ServerContainer container) {
  try {
    container.addEndpoint(ServerEndpointConfig.Builder
        .create(
            WSEndpoint.class,
            WSEndpoint.class.getAnnotation(ServerEndpoint.class).value()
        ).build());
  } catch (DeploymentException e) {
    fail("Invalid test");
  }
}
 
Example #5
Source File: CustomInitTest.java    From rest-utils with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(final ServletContextHandler context) {
  try {
    ServerContainer container = context.getBean(ServerContainer.class);

    container.addEndpoint(ServerEndpointConfig.Builder
        .create(
            WSEndpoint.class,
            WSEndpoint.class.getAnnotation(ServerEndpoint.class).value()
        ).build());
  } catch (Exception e) {
    fail("Invalid test");
  }
}
 
Example #6
Source File: DevServer.java    From java-tutorial with Creative Commons Attribution Share Alike 4.0 International 4 votes vote down vote up
public static void main(String[] args) {

        // 初始化 WebAppContext
        System.out.println("[INFO] Application loading");
        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setContextPath(CONTEXT);
        webAppContext.setResourceBase(getAbsolutePath() + RESOURCE_BASE_PATH);
        webAppContext.setDescriptor(getAbsolutePath() + RESOURCE_BASE_PATH + WEB_XML_PATH);
        webAppContext.setDefaultsDescriptor(WINDOWS_WEBDEFAULT_PATH);

        // 初始化 server
        Server server = new Server(PORT);
        server.setHandler(webAppContext);
        supportJspAndSetTldJarNames(server, TLD_JAR_NAMES);

        System.out.println("[INFO] Application loaded");

        try {
            // Initialize javax.websocket layer
            ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(webAppContext);

            // Add WebSocket endpoint to javax.websocket layer
            wscontainer.addEndpoint(WebSocketServer.class);

            System.out.println("[HINT] Don't forget to set -XX:MaxPermSize=128m");
            System.out.println("Server running at http://localhost:" + PORT + CONTEXT);
            System.out.println("[HINT] Hit Enter to reload the application quickly");

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

            // 等待用户输入回车重载应用
            while (true) {
                char c = (char) System.in.read();
                if (c == '\n') {
                    DevServer.reloadWebAppContext(server);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
 
Example #7
Source File: Application.java    From rest-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Used to register any websocket endpoints that will live under the path configured via
 * {@link io.confluent.rest.RestConfig#WEBSOCKET_PATH_PREFIX_CONFIG}
 */
protected void registerWebSocketEndpoints(ServerContainer container) { }