本文整理了Java中com.cloudhopper.commons.util.windowing.Window
类的一些代码示例,展示了Window
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Window
类的具体详情如下:
包路径:com.cloudhopper.commons.util.windowing.Window
类名称:Window
[英]A utility class to support "windowed" protocols that permit requests to be sent asynchronously and the responses to be processed at a later time. Responses may be returned in a different order than requests were sent.
Windowed protocols generally provide high throughput over high latency links such as TCP/IP connections since they allow requests one after the other without waiting for a response before sending the next request. This allows the underlying TCP/IP socket to potentially buffer multiple requests in one packet.
The "window" is the amount of unacknowledged requests that are permitted to be outstanding/unacknowledged at any given time. This implementation allows a max window size to be defined during construction. This represents the number of open "slots". When a response is received, it's up to the user of this class to make sure that response is added so that any threads waiting for a response are properly signaled.
The life cycle of a request in a Window has 3 steps:
Request offered
If free slot exists then goto 2
Request accepted (caller may optionally await() on returned future till completion)
Request completed/done (either success, failure, or cancelled)
If monitoring is enabled, it's very important to call "freeExternalResources()" if a Window will no longer be used.
[中]一个实用程序类,支持“窗口化”协议,允许异步发送请求,并在以后处理响应。响应的返回顺序可能与请求的发送顺序不同。
窗口协议通常在高延迟链路(如TCP/IP连接)上提供高吞吐量,因为它们允许一个接一个的请求,而无需在发送下一个请求之前等待响应。这允许底层TCP/IP套接字在一个数据包中潜在地缓冲多个请求。
“窗口”是指在任何给定时间允许未完成/未确认的未确认请求的数量。此实现允许在构建期间定义最大窗口大小。这表示打开的“插槽”数量。当收到响应时,该类的用户需要确保添加了响应,这样等待响应的所有线程都会收到正确的信号。
窗口中请求的生命周期有3个步骤:
*提出的请求
*如果存在空闲插槽,则转到2
*如果没有空闲时间,请立即提供“待定”并在指定时间内阻止。可能是超时,或者如果空闲插槽打开,则转到2
*请求已接受(调用方可以选择在返回的将来等待()直到完成)
*请求完成/完成(成功、失败或取消)
如果启用了监视功能,那么在窗口不再使用时调用“freeExternalResources()”非常重要。
代码示例来源:origin: twitter-archive/cloudhopper-smpp
@Override
public String[] dumpWindow() {
Map<Integer,WindowFuture<Integer,PduRequest,PduResponse>> sortedSnapshot = this.sendWindow.createSortedSnapshot();
String[] dump = new String[sortedSnapshot.size()];
int i = 0;
for (WindowFuture<Integer,PduRequest,PduResponse> future : sortedSnapshot.values()) {
dump[i] = future.getRequest().toString();
i++;
}
return dump;
}
代码示例来源:origin: com.cloudhopper/ch-commons-util
/**
* Offers a request for acceptance, waiting for the specified amount of time
* in case it could not immediately accepted. The "caller state hint" of
* the returned future will be set to "NOT_WAITING". The expireTimestamp of
* the returned future will be set to -1 (infinity/never expires).
* @param key The key for the request. A protocol's sequence number is a
* good choice.
* @param request The request to offer
* @param offerTimeoutMillis The amount of time (in milliseconds) to wait
* for the offer to be accepted.
* @return A future representing pending completion of the request
* @throws DuplicateKeyException Thrown if the key already exists
* @throws PendingOfferAbortedException Thrown if the offer could not be
* immediately accepted and the caller/thread was waiting, but
* the abortPendingOffers() method was called in the meantime.
* @throws OfferTimeoutException Thrown if the offer could not be accepted
* within the specified amount of time.
* @throws InterruptedException Thrown if the calling thread is interrupted
* while waiting to acquire the internal lock.
*/
public WindowFuture offer(K key, R request, long offerTimeoutMillis) throws DuplicateKeyException, OfferTimeoutException, InterruptedException {
return this.offer(key, request, offerTimeoutMillis, -1, false);
}
代码示例来源:origin: twitter-archive/cloudhopper-smpp
@Override
public int getWindowSize() {
return this.sendWindow.getSize();
}
代码示例来源:origin: org.restcomm.smpp/ch-commons-util
/**
* Destroy this window by freeing all resources associated with it. All
* pending offers are cancelled, followed by all outstanding futures,
* then all listeners are removed, and monitoring is cancelled.
*/
public synchronized void destroy() {
try {
this.abortPendingOffers();
} catch (Exception e) { }
this.cancelAll();
this.listeners.clear();
this.stopMonitor();
}
代码示例来源:origin: org.restcomm.smpp/ch-smpp
if (this.sendWindow.getSize() > 0) {
logger.trace("Channel closed and sendWindow has [{}] outstanding requests, some may need cancelled immediately", this.sendWindow.getSize());
Map<Integer,WindowFuture<Integer,PduRequest,PduResponse>> requests = this.sendWindow.createSortedSnapshot();
Throwable cause = new ClosedChannelException();
for (WindowFuture<Integer,PduRequest,PduResponse> future : requests.values()) {
代码示例来源:origin: com.cloudhopper/ch-commons-util
logger.trace("Monitor running... (current window.size [" + window.getSize() + "])");
List<WindowFuture<K,R,P>> expired = window.cancelAllExpired();
if (expired != null && expired.size() > 0) {
if (logger.isTraceEnabled())
for (UnwrappedWeakReference<WindowListener<K,R,P>> listenerRef : window.getListeners()) {
WindowListener<K,R,P> listener = listenerRef.get();
if (listener == null) {
window.removeListener(listener);
} else {
try {
代码示例来源:origin: com.cloudhopper/ch-commons-util
while (getFreeSize() <= 0) {
try {
this.beginPendingOffer();
this.completedCondition.await(remainingOfferTime, TimeUnit.MILLISECONDS);
} finally {
boolean abortPendingOffer = this.endPendingOffer();
if (abortPendingOffer) {
throw new PendingOfferAbortedException("Pending offer aborted (by an explicit call to abortPendingOffers())");
代码示例来源:origin: twitter-archive/cloudhopper-smpp
@Override
public void destroy() {
close();
this.sendWindow.destroy();
if (this.counters != null) {
this.counters.reset();
}
// make sure to lose the reference to to the session handler - many
// users of this class will probably pass themselves as the reference
// and this may help to prevent a circular reference
this.sessionHandler = null;
}
代码示例来源:origin: twitter-archive/cloudhopper-smpp
@Override
public int getMaxWindowSize() {
return this.sendWindow.getMaxSize();
}
代码示例来源:origin: twitter-archive/cloudhopper-smpp
this.sendWindow = new Window<Integer,PduRequest,PduResponse>(configuration.getWindowSize(), monitorExecutor, configuration.getWindowMonitorInterval(), this, configuration.getName() + ".Monitor");
} else {
this.sendWindow = new Window<Integer,PduRequest,PduResponse>(configuration.getWindowSize());
代码示例来源:origin: twitter-archive/cloudhopper-smpp
WindowFuture<Integer,PduRequest,PduResponse> future = this.sendWindow.complete(receivedPduSeqNum, responsePdu);
if (future != null) {
logger.trace("Found a future in the window for seqNum [{}]", receivedPduSeqNum);
代码示例来源:origin: twitter-archive/cloudhopper-smpp
if (this.sendWindow.getSize() > 0) {
logger.trace("Channel closed and sendWindow has [{}] outstanding requests, some may need cancelled immediately", this.sendWindow.getSize());
Map<Integer,WindowFuture<Integer,PduRequest,PduResponse>> requests = this.sendWindow.createSortedSnapshot();
Throwable cause = new ClosedChannelException();
for (WindowFuture<Integer,PduRequest,PduResponse> future : requests.values()) {
代码示例来源:origin: org.restcomm.smpp/ch-commons-util
logger.trace("Monitor running... (current window.size [" + window.getSize() + "])");
List<WindowFuture<K,R,P>> expired = window.cancelAllExpired();
if (expired != null && expired.size() > 0) {
if (logger.isTraceEnabled())
for (UnwrappedWeakReference<WindowListener<K,R,P>> listenerRef : window.getListeners()) {
WindowListener<K,R,P> listener = listenerRef.get();
if (listener == null) {
window.removeListener(listener);
} else {
try {
代码示例来源:origin: com.cloudhopper/ch-commons-util
/**
* Destroy this window by freeing all resources associated with it. All
* pending offers are cancelled, followed by all outstanding futures,
* then all listeners are removed, and monitoring is cancelled.
*/
public synchronized void destroy() {
try {
this.abortPendingOffers();
} catch (Exception e) { }
this.cancelAll();
this.listeners.clear();
this.stopMonitor();
}
代码示例来源:origin: org.restcomm.smpp/ch-commons-util
while (getFreeSize() <= 0) {
try {
this.beginPendingOffer();
this.completedCondition.await(remainingOfferTime, TimeUnit.MILLISECONDS);
} finally {
boolean abortPendingOffer = this.endPendingOffer();
if (abortPendingOffer) {
throw new PendingOfferAbortedException("Pending offer aborted (by an explicit call to abortPendingOffers())");
代码示例来源:origin: com.cloudhopper/ch-smpp
@Override
public void destroy() {
close();
this.sendWindow.destroy();
if (this.counters != null) {
this.counters.reset();
}
// make sure to lose the reference to to the session handler - many
// users of this class will probably pass themselves as the reference
// and this may help to prevent a circular reference
this.sessionHandler = null;
}
代码示例来源:origin: com.cloudhopper/ch-smpp
@Override
public int getMaxWindowSize() {
return this.sendWindow.getMaxSize();
}
代码示例来源:origin: org.restcomm.smpp/ch-smpp
this.sendWindow = new Window<Integer,PduRequest,PduResponse>(configuration.getWindowSize(), monitorExecutor, configuration.getWindowMonitorInterval(), this, configuration.getName() + ".Monitor");
} else {
this.sendWindow = new Window<Integer,PduRequest,PduResponse>(configuration.getWindowSize());
代码示例来源:origin: com.cloudhopper/ch-smpp
WindowFuture<Integer,PduRequest,PduResponse> future = this.sendWindow.complete(receivedPduSeqNum, responsePdu);
if (future != null) {
logger.trace("Found a future in the window for seqNum [{}]", receivedPduSeqNum);
代码示例来源:origin: com.cloudhopper/ch-smpp
if (this.sendWindow.getSize() > 0) {
logger.trace("Channel closed and sendWindow has [{}] outstanding requests, some may need cancelled immediately", this.sendWindow.getSize());
Map<Integer,WindowFuture<Integer,PduRequest,PduResponse>> requests = this.sendWindow.createSortedSnapshot();
Throwable cause = new ClosedChannelException();
for (WindowFuture<Integer,PduRequest,PduResponse> future : requests.values()) {
内容来源于网络,如有侵权,请联系作者删除!