Example usage for javax.websocket.server ServerContainer setDefaultMaxSessionIdleTimeout

List of usage examples for javax.websocket.server ServerContainer setDefaultMaxSessionIdleTimeout

Introduction

In this page you can find the example usage for javax.websocket.server ServerContainer setDefaultMaxSessionIdleTimeout.

Prototype

void setDefaultMaxSessionIdleTimeout(long timeout);

Source Link

Document

Set the default session idle timeout.

Usage

From source file:io.tempra.AppServer.java

public static void main(String[] args) throws Exception {

    try {// www  .j a  v a2  s.c o  m
        // String dir = getProperty("storageLocal");//
        // System.getProperty("user.home");
        String arch = "Win" + System.getProperty("sun.arch.data.model");
        System.out.println("sun.arch.data.model " + arch);

        // Sample to force java load dll or so on a filesystem
        // InputStream in = AppServer.class.getResourceAsStream("/dll/" +
        // arch + "/RechargeRPC.dll");
        //
        // File jCliSiTefI = new File(dir + "/" + "RechargeRPC.dll");
        // System.out.println("Writing dll to: " +
        // jCliSiTefI.getAbsolutePath());
        // OutputStream os = new FileOutputStream(jCliSiTefI);
        // byte[] buffer = new byte[4096];
        // int length;
        // while ((length = in.read(buffer)) > 0) {
        // os.write(buffer, 0, length);
        // }
        // os.close();
        // in.close();
        // System.load(jCliSiTefI.toString());
        // addLibraryPath(dir);

    } catch (Exception e) {
        e.printStackTrace();
    }

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");

    Server jettyServer = new Server(Integer.parseInt(getProperty("localport")));
    // security configuration
    // FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
    // holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM,
    // "*");
    // holder.setInitParameter("allowCredentials", "true");
    // holder.setInitParameter(
    // CrossOriginFilter.ACCESS_CONTROL_ALLOW_ORIGIN_HEADER, "*");
    // holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM,
    // "GET,POST,HEAD");
    // holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM,
    // "X-Requested-With,Content-Type,Accept,Origin");
    // holder.setName("cross-origin");

    // context.addFilter(holder, fm);

    ResourceHandler resource_handler = new ResourceHandler();

    // add application on embedded server
    boolean servApp = true;
    boolean quioskMode = false;

    // if (System.getProperty("noServApp") != null )
    // if (System.getProperty("noServApp").equalsIgnoreCase("true"))
    // servApp = false;
    if (servApp) {
        //   ProtectionDomain domain = AppServer.class.getProtectionDomain();
        String webDir = AppServer.class.getResource("/webapp").toExternalForm();
        System.out.println("Jetty WEB DIR >>>>" + webDir);
        resource_handler.setDirectoriesListed(true);
        resource_handler.setWelcomeFiles(new String[] { "index.html" });
        resource_handler.setResourceBase(webDir); //
        // "C:/git/tsAgenciaVirtual/www/");
        // resource_handler.setResourceBase("C:/git/tsAgenciaVirtual/www/");

        // copyJarResourceToFolder((JarURLConnection) AppServer.class
        // .getResource("/app").openConnection(), createTempDir("app"));

        // resource_handler.setResourceBase(System
        // .getProperty("java.io.tmpdir") + "/app");
    }
    // sample to add rest services on container
    context.addServlet(FileUploadServlet.class, "/upload");
    ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/services/*");
    jerseyServlet.setInitOrder(0);

    jerseyServlet.setInitParameter("jersey.config.server.provider.classnames",
            RestServices.class.getCanonicalName() + "," + RestServices.class.getCanonicalName());

    HandlerList handlers = new HandlerList();
    // context.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));

    handlers.setHandlers(new Handler[] { resource_handler, context,

            // wscontext,
            new DefaultHandler() });
    jettyServer.setHandler(handlers);
    try {
        // Initialize javax.websocket layer
        ServerContainer wscontainer = WebSocketServerContainerInitializer.configureContext(context);
        wscontainer.setDefaultMaxSessionIdleTimeout(TimeUnit.HOURS.toMillis(1000));

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

    } catch (Throwable t) {
        t.printStackTrace(System.err);
    }

    // try {
    jettyServer.start();
    jettyServer.dump(System.err);

    if (servApp && quioskMode) {
        try {
            Process process = new ProcessBuilder(
                    "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe", "--kiosk",
                    "--kiosk-printing", "--auto", "--disable-pinch", "--incognito",
                    "--disable-session-crashed-bubble", "--overscroll-history-navigation=0",
                    "http://localhost:" + getProperty("localport")).start();
            // Process process =
            // Runtime.getRuntime().exec(" -kiosk http://localhost:8080");
            InputStream is = process.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;

            System.out.printf("Output of running %s is:", Arrays.toString(args));
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }

        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    // } finally {
    // jettyServer.destroy();
    // }//}

    jettyServer.join();
}

From source file:org.jumpmind.metl.Develop.java

public static void main(String[] args) throws Exception {

    System.out//from   w w  w  .ja va2s.  c  o m
            .println(IOUtils.toString(new FileInputStream("../metl-server/src/main/resources/Metl.asciiart")));

    new File("working").mkdirs();
    System.setProperty("org.jumpmind.metl.ui.init.config.dir", "working");

    Server server = new Server(42000);
    ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
            "org.eclipse.jetty.annotations.AnnotationConfiguration");

    WebAppContext webapp = new WebAppContext();
    webapp.setParentLoaderPriority(true);
    webapp.setConfigurationDiscovered(true);
    webapp.setContextPath("/metl");
    webapp.setWar("src/main/webapp");
    webapp.setResourceBase("src/main/webapp");

    ConcurrentHashMap<String, ConcurrentHashSet<String>> map = new ClassInheritanceMap();
    ConcurrentHashSet<String> set = new ConcurrentHashSet<>();
    set.add("org.jumpmind.metl.ui.init.AppInitializer");
    map.put("org.springframework.web.WebApplicationInitializer", set);
    webapp.setAttribute(AnnotationConfiguration.CLASS_INHERITANCE_MAP, map);

    server.setHandler(webapp);

    ServerContainer webSocketServer = WebSocketServerContainerInitializer.configureContext(webapp);
    webSocketServer.setDefaultMaxSessionIdleTimeout(10000000);

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

}

From source file:org.jumpmind.metl.StartWebServer.java

public static void runWebServer() throws Exception {

    disableJettyLogging();//from  ww w . ja v  a  2s .c o m

    System.out.println(IOUtils.toString(StartWebServer.class.getResource("/Metl.asciiart")));

    Server server = new Server(PORT);

    ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration",
            "org.eclipse.jetty.annotations.AnnotationConfiguration");

    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addBean(mbContainer);

    ProtectionDomain protectionDomain = StartWebServer.class.getProtectionDomain();
    URL location = protectionDomain.getCodeSource().getLocation();
    File locationDir = new File(location.getFile()).getParentFile();

    WebAppContext webapp = new WebAppContext();

    //        HashSessionManager sessionManager = new HashSessionManager();
    //        File storeDir = new File(locationDir, "sessions");
    //        storeDir.mkdirs();
    //        sessionManager.setStoreDirectory(storeDir);
    //        sessionManager.setLazyLoad(true);
    //        sessionManager.setSavePeriod(5);
    //        sessionManager.setDeleteUnrestorableSessions(true);        
    //        SessionHandler sessionHandler = new SessionHandler(sessionManager);
    //        webapp.setSessionHandler(sessionHandler);

    webapp.setContextPath("/metl");
    webapp.setWar(location.toExternalForm());
    webapp.addAliasCheck(new AllowSymLinkAliasChecker());

    File pluginsDir = new File(locationDir, "plugins");
    pluginsDir.mkdirs();
    StringBuilder extraClasspath = new StringBuilder();
    File[] files = pluginsDir.listFiles();
    Arrays.sort(files);
    for (File file : files) {
        if (file.isFile() && file.getName().endsWith(".jar")) {
            extraClasspath.append(file.toURI().toURL().toExternalForm()).append(",");
        }
    }
    webapp.setExtraClasspath(extraClasspath.toString());

    server.setHandler(webapp);

    ServerContainer webSocketServer = WebSocketServerContainerInitializer.configureContext(webapp);
    webSocketServer.setDefaultMaxSessionIdleTimeout(10000000);

    server.start();

    if (extraClasspath.length() > 0) {
        getLogger().info("Adding extra classpath of: " + extraClasspath.toString());
    }
    getLogger().info("To use Metl, navigate to http://localhost:" + PORT + "/metl");

    server.join();
}