Exemple #1
0
  /**
   * Creates configuration properties object for new connection.
   *
   * <p>See GridGain client javadoc for more information: {@link GridClientConfiguration}.
   *
   * <p>All parameters are optional.
   *
   * @return Configuration.
   */
  private static Properties configuration() {
    Properties cfg = new Properties();

    // Node ID where to execute query. This property is useful when you have several
    // local caches with same name in topology and want to specify which of them to connect.
    //
    // Uncomment line below and provide correct ID if needed.
    //        cfg.setProperty(CONF_NODE_ID, "E0869485-512C-41F9-866D-BE906B591BEA");

    // Communication protocol (TCP or HTTP). Default is TCP.
    cfg.setProperty(CONF_PROTO, "TCP");

    // Socket timeout. Default is 0 which means infinite timeout.
    cfg.setProperty(CONF_TIMEOUT, "0");

    // Flag indicating whether TCP_NODELAY flag should be enabled for outgoing
    // connections. Default is true.
    cfg.setProperty(CONF_TCP_NO_DELAY, "true");

    // Flag indicating that SSL is needed for connection. Default is false.
    cfg.setProperty(CONF_SSL_ENABLED, "false");

    // SSL context factory class name. Class must extend
    // org.gridgain.client.ssl.GridSslContextFactory
    // interface, have default constructor and be available on classpath.
    // Ignored if SSL is disabled.
    cfg.setProperty(CONF_SSL_FACTORY, "org.gridgain.client.ssl.GridSslBasicContextFactory");

    // SSL pass-phrase.
    // Ignored is SSL is disabled.
    cfg.setProperty(CONF_CREDS, "s3cr3t");

    // Flag indicating that topology is cached internally. Cache will be refreshed
    // in the background with interval defined by CONF_TOP_REFRESH_FREQ property
    // (see below). Default is false.
    cfg.setProperty(CONF_TOP_CACHE_ENABLED, "false");

    // Topology cache refresh frequency. Default is 2000 ms.
    cfg.setProperty(CONF_TOP_REFRESH_FREQ, "2000");

    // Maximum amount of time that connection can be idle before it is closed.
    // Default is 30000 ms.
    cfg.setProperty(CONF_MAX_IDLE_TIME, "30000");

    return cfg;
  }
  /**
   * Starts Grid instance. Note that if grid is already started, then it will be looked up and
   * returned from this method.
   *
   * @return Started grid.
   */
  private Grid startGrid() {
    Properties props = System.getProperties();

    gridName = props.getProperty(GRIDGAIN_NAME.name());

    if (!props.containsKey(GRIDGAIN_NAME.name()) || G.state(gridName) != GridFactoryState.STARTED) {
      selfStarted = true;

      // Set class loader for the spring.
      ClassLoader curCl = Thread.currentThread().getContextClassLoader();

      // Add no-op logger to remove no-appender warning.
      Appender app = new NullAppender();

      Logger.getRootLogger().addAppender(app);

      try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

        Grid grid = G.start(cfgPath);

        gridName = grid.name();

        System.setProperty(GRIDGAIN_NAME.name(), grid.name());

        return grid;
      } catch (GridException e) {
        throw new GridRuntimeException("Failed to start grid: " + cfgPath, e);
      } finally {
        Logger.getRootLogger().removeAppender(app);

        Thread.currentThread().setContextClassLoader(curCl);
      }
    }

    return G.grid(gridName);
  }