001
014
015 package com.liferay.portal.kernel.nio.intraband;
016
017 import java.io.IOException;
018
019 import java.nio.channels.Selector;
020
021 import java.util.concurrent.CountDownLatch;
022 import java.util.concurrent.TimeUnit;
023
024
027 public class RecordCompletionHandler<A> implements CompletionHandler<A> {
028
029 @Override
030 public void delivered(A attachment) {
031 _attachment = attachment;
032
033 _deliveredCountDownLatch.countDown();
034 }
035
036 @Override
037 public void failed(A attachment, IOException ioe) {
038 _attachment = attachment;
039 _ioe = ioe;
040
041 _failedCountDownLatch.countDown();
042 }
043
044 public A getAttachment() {
045 return _attachment;
046 }
047
048 public IOException getIOException() {
049 return _ioe;
050 }
051
052 @Override
053 public void replied(A attachment, Datagram datagram) {
054 _attachment = attachment;
055
056 _repliedCountDownLatch.countDown();
057 }
058
059 @Override
060 public void submitted(A attachment) {
061 _attachment = attachment;
062
063 _submittedCountDownLatch.countDown();
064 }
065
066 @Override
067 public void timedOut(A attachment) {
068 _attachment = attachment;
069
070 _timeoutedCountDownLatch.countDown();
071 }
072
073 public void waitUntilDelivered() throws InterruptedException {
074 _deliveredCountDownLatch.await();
075 }
076
077 public void waitUntilFailed() throws InterruptedException {
078 _failedCountDownLatch.await();
079 }
080
081 public void waitUntilReplied() throws InterruptedException {
082 _repliedCountDownLatch.await();
083 }
084
085 public void waitUntilSubmitted() throws InterruptedException {
086 _submittedCountDownLatch.await();
087 }
088
089 public void waitUntilTimeouted() throws InterruptedException {
090 _timeoutedCountDownLatch.await();
091 }
092
093 public void waitUntilTimeouted(Selector selector)
094 throws InterruptedException {
095
096 while (!_timeoutedCountDownLatch.await(10, TimeUnit.MILLISECONDS)) {
097 selector.wakeup();
098 }
099 }
100
101 private volatile A _attachment;
102 private final CountDownLatch _deliveredCountDownLatch = new CountDownLatch(
103 1);
104 private final CountDownLatch _failedCountDownLatch = new CountDownLatch(1);
105 private volatile IOException _ioe;
106 private final CountDownLatch _repliedCountDownLatch = new CountDownLatch(1);
107 private final CountDownLatch _submittedCountDownLatch = new CountDownLatch(
108 1);
109 private final CountDownLatch _timeoutedCountDownLatch = new CountDownLatch(
110 1);
111
112 }