Skip to content

Commit b5b5778

Browse files
author
Hubert Plociniczak
committed
Use -1 for Infinity rather than 0.
1 parent f46c27f commit b5b5778

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/com/rabbitmq/client/Connection.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public interface Connection extends ShutdownNotifier { // rename to AMQPConnecti
136136
* This method will wait with the given timeout for all the close
137137
* operations to complete. If timeout is reached then socket is forced
138138
* to close
139-
* @param timeout timeout (in milioseconds) for completing all the close-related operations, use 0 for infinity
139+
* @param timeout timeout (in milioseconds) for completing all the close-related
140+
* operations, use -1 for infinity
140141
* @throws IOException if an I/O problem is encountered
141142
*/
142143
void close(int timeout) throws IOException;
@@ -155,6 +156,9 @@ public interface Connection extends ShutdownNotifier { // rename to AMQPConnecti
155156
* This method behaves in a similar way as abort(), with the only difference
156157
* that it will wait with a provided timeout for all the close operations to
157158
* complete. If timeout is reached socket is forced to close.
159+
*
160+
* @param timeout timeout (in miliseconds) for completing all the close-related
161+
* operations, use -1 for infinity
158162
*/
159163
void abort(int timeout);
160164
}

src/com/rabbitmq/client/impl/AMQConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ public void shutdown(Object reason,
607607
public void close()
608608
throws IOException
609609
{
610-
close(0);
610+
close(-1);
611611
}
612612

613613
/**
@@ -625,7 +625,7 @@ public void close(int timeout)
625625
*/
626626
public void abort()
627627
{
628-
abort(0);
628+
abort(-1);
629629
}
630630

631631
public void abort(int timeout)

src/com/rabbitmq/utility/BlockingCell.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class BlockingCell<T> {
3838
private T _value;
3939

4040
private static final long NANOS_IN_MILLI = 1000 * 1000;
41+
42+
private static final long INFINITY = -1;
4143

4244
/** Instantiate a new BlockingCell waiting for a value of the specified type. */
4345
public BlockingCell() {
@@ -63,14 +65,19 @@ public synchronized T get() throws InterruptedException {
6365
* already a value present, there's no need to wait - the existing value is returned.
6466
* If timeout is reached and value hasn't arrived, TimeoutException is thrown
6567
*
66-
* @param timeout timeout in miliseconds.Value less than zero effectively means infinity
68+
* @param timeout timeout in miliseconds. -1 effectively means infinity
6769
* @return the waited-for value
6870
* @throws InterruptedException if this thread is interrupted
6971
*/
7072
public synchronized T get(long timeout) throws InterruptedException, TimeoutException {
71-
synchronized(this) {
72-
wait(timeout);
73-
}
73+
if (timeout < 0 && timeout != INFINITY)
74+
throw new AssertionError("Timeout cannot be less than zero");
75+
76+
if (timeout != 0) {
77+
synchronized(this) {
78+
wait(timeout == INFINITY ? 0 : timeout);
79+
}
80+
}
7481

7582
if (!_filled)
7683
throw new TimeoutException();
@@ -92,24 +99,18 @@ public synchronized T uninterruptibleGet() {
9299
}
93100
}
94101

95-
96102
/**
97103
* As get(long timeout), but catches and ignores InterruptedException, retrying until
98104
* a value appears or until specified timeout is reached. If timeout is reached,
99105
* TimeoutException it thrown.
100106
* We also use System.nanoTime() to behave correctly when system clock jumps around.
101107
*
102-
* @param timeout timeout in miliseconds. 0 effectively means infinity
108+
* @param timeout timeout in miliseconds. -1 effectively means infinity
103109
* @return the waited-for value
104110
*/
105111
public synchronized T uninterruptibleGet(int timeout) throws TimeoutException {
106-
long now = System.nanoTime() / NANOS_IN_MILLI;
112+
long now = System.nanoTime() / NANOS_IN_MILLI;
107113
long runTime = now + timeout;
108-
109-
110-
if (timeout < 0) {
111-
throw new AssertionError("Timeout cannot be less than zero");
112-
}
113114

114115
do {
115116
try {
@@ -119,7 +120,7 @@ public synchronized T uninterruptibleGet(int timeout) throws TimeoutException {
119120
} catch (InterruptedException e) {
120121
// Ignore.
121122
}
122-
} while ((timeout == 0) || ((now = System.nanoTime() / NANOS_IN_MILLI) < runTime));
123+
} while ((timeout == INFINITY) || ((now = System.nanoTime() / NANOS_IN_MILLI) < runTime));
123124

124125
throw new TimeoutException();
125126
}

0 commit comments

Comments
 (0)