下面列出了怎么用org.apache.commons.lang3.concurrent.TimedSemaphore的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Create a bandwith cap, estimating the size of the first row returned by the resultset
* and using it as permits in the rate limit.
*
* @param resultSet the resultset cursor moved to the first row (resultSet.next())
* @param rsmd the result set metadata object
* @throws SQLException
*/
protected void bandwidthThrottlingCreate(ResultSet resultSet, ResultSetMetaData rsmd) throws SQLException {
int kilobytesPerSecond = options.getBandwidthThrottling();
if (kilobytesPerSecond > 0) {
// Stimate the Row Size
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
if (rsmd.getColumnType(i) != Types.BLOB) {
String columnValue = resultSet.getString(i);
if (columnValue != null && !resultSet.getString(i).isEmpty())
rowSize = rowSize + resultSet.getString(i).length();
}
}
double limit = ((1.0 * kilobytesPerSecond) / rowSize) / (options.getFetchSize() * 1.0 / 1000);
if (limit == 0) limit = 1;
this.bandwidthRateLimiter = new TimedSemaphore(1, TimeUnit.SECONDS, (int) Math.round(limit));
LOG.info("Estimated Row Size: {} KB. Estimated limit of fetchs per second: {} ", rowSize, limit);
}
}
public LimitedRendezvousListener(final Set<RendezvousListener> listeners) {
this(new TimedSemaphore(
1L, TimeUnit.MINUTES, PreferencesFactory.get().getInteger("rendezvous.notification.limit")), listeners);
}
public LimitedRendezvousListener(final TimedSemaphore limit, final Set<RendezvousListener> listeners) {
this.limit = limit;
this.listeners = listeners;
}
DelayQueueUsingTimedSemaphore(long period, int slotLimit) {
semaphore = new TimedSemaphore(period, TimeUnit.SECONDS, slotLimit);
}