Code example for AtomicLong

Methods: longValue, set

0
 
  public FileMaxSCNHandler(StaticConfig config)
  { 
    _staticConfig = config;
    _flushCounter = new AtomicLong(0);
    _scn = new AtomicLong(0);
    _scnFileName = _staticConfig.getScnDir().getAbsolutePath() + File.separator + _staticConfig.getKey();
    LOG.info("creating file:" + _scnFileName);
  } 
 
  /** 
   * Reads scn value from SCN File name according to configuration. If SCN file does not 
   * exist, it creates the SCN file w/the initial value specified in the configuration. An 
   * exception is thrown if we the SCN file exists but we fail to read the SCN value from 
   * it. 
   * 
   * @throws IOException 
   *           if we fail to open or read from the SCN file 
   * @throws RuntimeException 
   *           if the SCN value cannot be read or parsed from the file 
   */ 
  protected void loadInitialValue() throws IOException, 
      DatabusException 
  { 
    Long initVal = null;
    LOG.info("Trying to read initial SCN from file: " + _scnFileName);
 
    File file = new File(_scnFileName);
    if (file.exists())
    { 
      FileReader fileReader = new FileReader(file);
      try 
      { 
        BufferedReader reader = new BufferedReader(fileReader);
        String scnLine = reader.readLine();
        if (null != scnLine)
        { 
          try 
          { 
            String scnString = scnLine.substring(0, scnLine.indexOf(SCN_SEPARATOR));
            _scn.set(Long.parseLong(scnString));
 
            LOG.info("Starting from MAX SCN:" + scnString);
          } 
          catch (Exception e)
          { 
            LOG.error("Could not read initial SCN value. Value missing or not in expected format; scnLine = "
                             + scnLine,
                         e);
            throw new DatabusException("Failed to load initial SCN value. Value misisng or not in expected format.", 
                                       e);
          } 
        } 
        else 
        { 
          LOG.warn("SCN file empty; defaulting to initial value from configuration:" +
                      _staticConfig.getInitVal());
          _scn.set(_staticConfig.getInitVal());
        } 
        reader.close();
      } 
      finally 
      { 
        fileReader.close();
      } 
    } 
    else 
    { 
      LOG.info("Initial max SCN does not exist. Defaulting to initial value from configuration: "
          + _staticConfig.getInitVal());
      _scn.set(_staticConfig.getInitVal());
      writeScnToFile(); 
    } 
  } 
 
  /** 
   * Write SCN value to file. If SCN file exists, move it aside and create a new file 
   * w/new value. 
   */ 
  private void writeScnToFile() throws IOException 
  { 
    long scn = _scn.longValue();
 
    File dir = _staticConfig.getScnDir();
    if (! dir.exists() && !dir.mkdirs())
    { 
      throw new IOException("unable to create SCN file parent:" + dir.getAbsolutePath());