ch.qos.logback.core.util.Duration类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(13.8k)|赞(0)|评价(0)|浏览(261)

本文整理了Java中ch.qos.logback.core.util.Duration类的一些代码示例,展示了Duration类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Duration类的具体详情如下:
包路径:ch.qos.logback.core.util.Duration
类名称:Duration

Duration介绍

[英]Duration instances represent a lapse of time. Internally, the duration is stored in milliseconds. However, whenever a parameter of type Duration is expected, Joran (logback's configuration system) will automatically convert strings such as "20 seconds" "3.5 minutes" or "5 hours" into Duration instances.

The recognized units of time are the "millisecond", "second", "minute" "hour" and "day". The unit name may be followed by an "s". Thus, "2 day" and "2 days" are equivalent. In the absence of a time unit specification, milliseconds are assumed.

Note: the conversion magic is entirely due to the fact that this class follows the #valueOf convention.
[中]持续时间实例表示时间的流逝。在内部,持续时间以毫秒为单位存储。但是,每当需要Duration类型的参数时,Joran(logback的配置系统)将自动将字符串(如“20秒”、“3.5分钟”或“5小时”)转换为Duration实例。
公认的时间单位是“毫秒”、“秒”、“分钟”、“小时”和“天”。装置名称后面可以跟一个“s”。因此,“2天”和“2天”是等价的。在没有时间单位规范的情况下,假定为毫秒。
注意:转换魔法完全是因为这个类遵循#valueOf约定。

代码示例

代码示例来源:origin: ch.qos.logback/logback-classic

void processScanAttrib(InterpretationContext ic, Attributes attributes) {
  String scanAttrib = ic.subst(attributes.getValue(SCAN_ATTR));
  if (!OptionHelper.isEmpty(scanAttrib) && !"false".equalsIgnoreCase(scanAttrib)) {
    ScheduledExecutorService scheduledExecutorService = context.getScheduledExecutorService();
    URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(context);
    if (mainURL == null) {
      addWarn("Due to missing top level configuration file, reconfiguration on change (configuration file scanning) cannot be done.");
      return;
    }
    ReconfigureOnChangeTask rocTask = new ReconfigureOnChangeTask();
    rocTask.setContext(context);
    context.putObject(CoreConstants.RECONFIGURE_ON_CHANGE_TASK, rocTask);
    String scanPeriodAttrib = ic.subst(attributes.getValue(SCAN_PERIOD_ATTR));
    Duration duration = getDuration(scanAttrib, scanPeriodAttrib);
    if (duration == null) {
      return;
    }
    addInfo("Will scan for changes in [" + mainURL + "] ");
    // Given that included files are encountered at a later phase, the complete list of files 
    // to scan can only be determined when the configuration is loaded in full.
    // However, scan can be active if mainURL is set. Otherwise, when changes are detected
    // the top level config file cannot be accessed.
    addInfo("Setting ReconfigureOnChangeTask scanning period to " + duration);
    ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(rocTask, duration.getMilliseconds(), duration.getMilliseconds(),
            TimeUnit.MILLISECONDS);
    context.addScheduledFuture(scheduledFuture);
  }
}

代码示例来源:origin: camunda/camunda-bpm-platform

public static Duration buildByMilliseconds(double value) {
 return new Duration((long) (value));
}

代码示例来源:origin: camunda/camunda-bpm-platform

public static Duration valueOf(String durationStr) {
 Matcher matcher = DURATION_PATTERN.matcher(durationStr);
 if (matcher.matches()) {
  String doubleStr = matcher.group(DOUBLE_GROUP);
  String unitStr = matcher.group(UNIT_GROUP);
  double doubleValue = Double.valueOf(doubleStr);
  if (unitStr.equalsIgnoreCase("milli")
    || unitStr.equalsIgnoreCase("millisecond") || unitStr.length() == 0) {
   return buildByMilliseconds(doubleValue);
  } else if (unitStr.equalsIgnoreCase("second")
    || unitStr.equalsIgnoreCase("seconde")) {
   return buildBySeconds(doubleValue);
  } else if (unitStr.equalsIgnoreCase("minute")) {
   return buildByMinutes(doubleValue);
  } else if (unitStr.equalsIgnoreCase("hour")) {
   return buildByHours(doubleValue);
  } else if (unitStr.equalsIgnoreCase("day")) {
   return buildByDays(doubleValue);
  } else {
   throw new IllegalStateException("Unexpected " + unitStr);
  }
 } else {
  throw new IllegalArgumentException("String value [" + durationStr
    + "] is not in the expected format.");
 }
}

代码示例来源:origin: camunda/camunda-bpm-platform

void processScanAttrib(InterpretationContext ic, Attributes attributes) {
 String scanAttrib = ic.subst(attributes.getValue(SCAN_ATTR));
 if (!OptionHelper.isEmpty(scanAttrib)
     && !"false".equalsIgnoreCase(scanAttrib)) {
  ReconfigureOnChangeFilter rocf = new ReconfigureOnChangeFilter();
  rocf.setContext(context);
  String scanPeriodAttrib = ic.subst(attributes.getValue(SCAN_PERIOD_ATTR));
  if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
   try {
    Duration duration = Duration.valueOf(scanPeriodAttrib);
    rocf.setRefreshPeriod(duration.getMilliseconds());
    addInfo("Setting ReconfigureOnChangeFilter scanning period to "
        + duration);
   } catch (NumberFormatException nfe) {
    addError("Error while converting [" + scanAttrib + "] to long", nfe);
   }
  }
  rocf.start();
  LoggerContext lc = (LoggerContext) context;
  addInfo("Adding ReconfigureOnChangeFilter as a turbo filter");
  lc.addTurboFilter(rocf);
 }
}

代码示例来源:origin: ch.qos.logback/logback-classic

private Duration getDuration(String scanAttrib, String scanPeriodAttrib) {
  Duration duration = null;
  if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
    try {
      duration = Duration.valueOf(scanPeriodAttrib);
    } catch (NumberFormatException nfe) {
      addError("Error while converting [" + scanAttrib + "] to long", nfe);
    }
  }
  return duration;
}

代码示例来源:origin: tony19/logback-android

private Duration getDurationOfScanPeriodAttribute(String scanPeriodAttrib, Duration defaultDuration) {
 Duration duration = null;
 if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
  Exception ex = null;
  try {
   duration = Duration.valueOf(scanPeriodAttrib);
  } catch (IllegalArgumentException e) {
   ex = e;
  } catch (IllegalStateException e) {
   ex = e;
  }
  if (ex != null) {
   addWarn("Failed to parse 'scanPeriod' attribute ["+scanPeriodAttrib+"]", ex);
  }
 }
 if (duration == null) {
  addInfo("No 'scanPeriod' specified. Defaulting to " + defaultDuration.toString());
  duration = defaultDuration;
 }
 return duration;
}

代码示例来源:origin: tony19/logback-android

Duration d = Duration.valueOf("12");
assertEquals(12, d.getMilliseconds());
Duration d = Duration.valueOf("159 milli");
assertEquals(159, d.getMilliseconds());
Duration d = Duration.valueOf("15 millis");
assertEquals(15, d.getMilliseconds());
Duration d = Duration.valueOf("8 milliseconds");
assertEquals(8, d.getMilliseconds());
Duration d = Duration.valueOf("10.7 millisecond");
assertEquals(10, d.getMilliseconds());
Duration d = Duration.valueOf("10 SECOnds");
assertEquals(10 * 1000, d.getMilliseconds());
Duration d = Duration.valueOf("12seconde");
assertEquals(12 * 1000, d.getMilliseconds());
Duration d = Duration.valueOf("14 SECONDES");
assertEquals(14 * 1000, d.getMilliseconds());
Duration d = Duration.valueOf("12second");
assertEquals(12 * 1000, d.getMilliseconds());
Duration d = Duration.valueOf("10.7 seconds");
assertEquals(10700, d.getMilliseconds());

代码示例来源:origin: tony19/logback-android

/**
 * Set the maximum history in time duration of records to keep
 *
 * @param maxHistory
 *                max history in time duration (e.g., "2 days")
 */
public void setMaxHistory(String maxHistory) {
 this.maxHistory = Duration.valueOf(maxHistory);
}

代码示例来源:origin: tony19/logback-android

public static Duration valueOf(String durationStr) {
 Matcher matcher = DURATION_PATTERN.matcher(durationStr);
 if (matcher.matches()) {
  String doubleStr = matcher.group(DOUBLE_GROUP);
  String unitStr = matcher.group(UNIT_GROUP);
  double doubleValue = Double.valueOf(doubleStr);
  if (unitStr.equalsIgnoreCase("milli")
    || unitStr.equalsIgnoreCase("millisecond") || unitStr.length() == 0) {
   return buildByMilliseconds(doubleValue);
  } else if (unitStr.equalsIgnoreCase("second")
    || unitStr.equalsIgnoreCase("seconde")) {
   return buildBySeconds(doubleValue);
  } else if (unitStr.equalsIgnoreCase("minute")) {
   return buildByMinutes(doubleValue);
  } else if (unitStr.equalsIgnoreCase("hour")) {
   return buildByHours(doubleValue);
  } else if (unitStr.equalsIgnoreCase("day")) {
   return buildByDays(doubleValue);
  } else {
   throw new IllegalStateException("Unexpected " + unitStr);
  }
 } else {
  throw new IllegalArgumentException("String value [" + durationStr
    + "] is not in the expected format.");
 }
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * {@inheritDoc}
 */
@Override
protected void append(E event) {
 if (event == null || !isStarted()) return;
 try {
  final boolean inserted = queue.offer(event, eventDelayLimit.getMilliseconds(), TimeUnit.MILLISECONDS);
  if (!inserted) {
   addInfo("Dropping event due to timeout limit of [" + eventDelayLimit +
     "] milliseconds being exceeded");
  }
 } catch (InterruptedException e) {
  addError("Interrupted while appending event to SocketAppender", e);
 }
}

代码示例来源:origin: camunda/camunda-bpm-platform

public static Duration buildByHours(double value) {
 return new Duration((long) (HOURS_COEFFICIENT * value));
}

代码示例来源:origin: com.alibaba.citrus.tool/antx-autoexpand

void processScanAttrib(Attributes attributes) {
 String scanAttrib = attributes.getValue(SCAN_ATTR);
 if (!OptionHelper.isEmpty(scanAttrib)
   && !"false".equalsIgnoreCase(scanAttrib)) {
  ReconfigureOnChangeFilter rocf = new ReconfigureOnChangeFilter();
  rocf.setContext(context);
  String scanPeriodAttrib = attributes.getValue(SCAN_PERIOD_ATTR);
  if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
   try {
    Duration duration = Duration.valueOf(scanPeriodAttrib);
    rocf.setRefreshPeriod(duration.getMilliseconds());
    addInfo("Setting ReconfigureOnChangeFilter scanning period to "
      + duration);
   } catch (NumberFormatException nfe) {
    addError("Error while converting [" + scanAttrib + "] to long", nfe);
   }
  }
  rocf.start();
  LoggerContext lc = (LoggerContext) context;
  addInfo("Adding ReconfigureOnChangeFilter as a turbo filter");
  lc.addTurboFilter(rocf);
 }
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

private Duration getDuration(String scanAttrib, String scanPeriodAttrib) {
  Duration duration = null;
  if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
    try {
      duration = Duration.valueOf(scanPeriodAttrib);
    } catch (NumberFormatException nfe) {
      addError("Error while converting [" + scanAttrib + "] to long", nfe);
    }
  }
  return duration;
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

public static Duration valueOf(String durationStr) {
  Matcher matcher = DURATION_PATTERN.matcher(durationStr);
  if (matcher.matches()) {
    String doubleStr = matcher.group(DOUBLE_GROUP);
    String unitStr = matcher.group(UNIT_GROUP);
    double doubleValue = Double.valueOf(doubleStr);
    if (unitStr.equalsIgnoreCase("milli") || unitStr.equalsIgnoreCase("millisecond") || unitStr.length() == 0) {
      return buildByMilliseconds(doubleValue);
    } else if (unitStr.equalsIgnoreCase("second") || unitStr.equalsIgnoreCase("seconde")) {
      return buildBySeconds(doubleValue);
    } else if (unitStr.equalsIgnoreCase("minute")) {
      return buildByMinutes(doubleValue);
    } else if (unitStr.equalsIgnoreCase("hour")) {
      return buildByHours(doubleValue);
    } else if (unitStr.equalsIgnoreCase("day")) {
      return buildByDays(doubleValue);
    } else {
      throw new IllegalStateException("Unexpected " + unitStr);
    }
  } else {
    throw new IllegalArgumentException("String value [" + durationStr + "] is not in the expected format.");
  }
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * {@inheritDoc}
 */
public final void run() {
 signalEntryInRunMethod();
 try {
  while (!Thread.currentThread().isInterrupted()) {
   SocketConnector connector = createConnector(address, port, 0,
       reconnectionDelay.getMilliseconds());
   connectorTask = activateConnector(connector);
   if(connectorTask == null)
    break;
   socket = waitForConnectorToReturnASocket();
   if(socket == null)
    break;
   dispatchEvents();
  }
 } catch (InterruptedException ex) {
  assert true;    // ok... we'll exit now
 }
 addInfo("shutting down");
}

代码示例来源:origin: camunda/camunda-bpm-platform

public static Duration buildUnbounded() {
 return new Duration(Long.MAX_VALUE);
}

代码示例来源:origin: at.bestsolution.efxclipse.eclipse/ch.qos.logback.classic

void processScanAttrib(InterpretationContext ic, Attributes attributes) {
 String scanAttrib = ic.subst(attributes.getValue(SCAN_ATTR));
 if (!OptionHelper.isEmpty(scanAttrib)
     && !"false".equalsIgnoreCase(scanAttrib)) {
  ReconfigureOnChangeFilter rocf = new ReconfigureOnChangeFilter();
  rocf.setContext(context);
  String scanPeriodAttrib = ic.subst(attributes.getValue(SCAN_PERIOD_ATTR));
  if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
   try {
    Duration duration = Duration.valueOf(scanPeriodAttrib);
    rocf.setRefreshPeriod(duration.getMilliseconds());
    addInfo("Setting ReconfigureOnChangeFilter scanning period to "
        + duration);
   } catch (NumberFormatException nfe) {
    addError("Error while converting [" + scanAttrib + "] to long", nfe);
   }
  }
  rocf.start();
  LoggerContext lc = (LoggerContext) context;
  addInfo("Adding ReconfigureOnChangeFilter as a turbo filter");
  lc.addTurboFilter(rocf);
 }
}

代码示例来源:origin: io.virtdata/virtdata-lib-realer

private Duration getDuration(String scanAttrib, String scanPeriodAttrib) {
  Duration duration = null;
  if (!OptionHelper.isEmpty(scanPeriodAttrib)) {
    try {
      duration = Duration.valueOf(scanPeriodAttrib);
    } catch (NumberFormatException nfe) {
      addError("Error while converting [" + scanAttrib + "] to long", nfe);
    }
  }
  return duration;
}

代码示例来源:origin: Nextdoor/bender

public static Duration valueOf(String durationStr) {
  Matcher matcher = DURATION_PATTERN.matcher(durationStr);
  if (matcher.matches()) {
    String doubleStr = matcher.group(DOUBLE_GROUP);
    String unitStr = matcher.group(UNIT_GROUP);
    double doubleValue = Double.valueOf(doubleStr);
    if (unitStr.equalsIgnoreCase("milli") || unitStr.equalsIgnoreCase("millisecond") || unitStr.length() == 0) {
      return buildByMilliseconds(doubleValue);
    } else if (unitStr.equalsIgnoreCase("second") || unitStr.equalsIgnoreCase("seconde")) {
      return buildBySeconds(doubleValue);
    } else if (unitStr.equalsIgnoreCase("minute")) {
      return buildByMinutes(doubleValue);
    } else if (unitStr.equalsIgnoreCase("hour")) {
      return buildByHours(doubleValue);
    } else if (unitStr.equalsIgnoreCase("day")) {
      return buildByDays(doubleValue);
    } else {
      throw new IllegalStateException("Unexpected " + unitStr);
    }
  } else {
    throw new IllegalArgumentException("String value [" + durationStr + "] is not in the expected format.");
  }
}

代码示例来源:origin: tony19/logback-android

/**
 * Gets the maximum history in milliseconds
 * @return the max history in milliseconds
 */
public long getMaxHistoryMs() {
 return maxHistory != null ? maxHistory.getMilliseconds() : 0;
}

相关文章