blob: 42ae5a7b174025154e3531113c05d6fb100460d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.qpid.client.state;
import org.apache.log4j.Logger;
import org.apache.qpid.AMQException;
/**
* Waits for a particular state to be reached.
*
*/
public class StateWaiter implements StateListener
{
private static final Logger _logger = Logger.getLogger(StateWaiter.class);
private final AMQState _state;
private volatile boolean _newStateAchieved;
private volatile Throwable _throwable;
private final Object _monitor = new Object();
public StateWaiter(AMQState state)
{
_state = state;
}
public void waituntilStateHasChanged() throws AMQException
{
synchronized (_monitor)
{
//
// The guard is required in case we are woken up by a spurious
// notify().
//
while (!_newStateAchieved && _throwable == null)
{
try
{
_logger.debug("State " + _state + " not achieved so waiting...");
_monitor.wait();
}
catch (InterruptedException e)
{
_logger.debug("Interrupted exception caught while waiting: " + e, e);
}
}
}
if (_throwable != null)
{
_logger.debug("Throwable reached state waiter: " + _throwable);
if (_throwable instanceof AMQException)
{
throw (AMQException) _throwable;
}
else
{
throw new AMQException("Error: " + _throwable, _throwable); // FIXME: this will wrap FailoverException in throwable which will prevent it being caught.
}
}
}
public void stateChanged(AMQState oldState, AMQState newState)
{
synchronized (_monitor)
{
if (_logger.isDebugEnabled())
{
_logger.debug("stateChanged called");
}
if (_state == newState)
{
_newStateAchieved = true;
if (_logger.isDebugEnabled())
{
_logger.debug("New state reached so notifying monitor");
}
_monitor.notifyAll();
}
}
}
public void error(Throwable t)
{
synchronized (_monitor)
{
if (_logger.isDebugEnabled())
{
_logger.debug("exceptionThrown called");
}
_throwable = t;
_monitor.notifyAll();
}
}
}
|