summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/RetryQueue.java
blob: 828315295456b1ef4637c3d0d16712b59980cdc0 (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
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package IceInternal;

public class RetryQueue
{
    RetryQueue(Instance instance)
    {
        _instance = instance;
    }

    synchronized public void add(ProxyOutgoingAsyncBase outAsync, int interval)
    {
        if(_instance == null)
        {
            throw new Ice.CommunicatorDestroyedException();
        }
        RetryTask task = new RetryTask(this, outAsync);
        task.setFuture(_instance.timer().schedule(task, interval, java.util.concurrent.TimeUnit.MILLISECONDS));
        _requests.add(task);
        outAsync.cancelable(task);
    }

    synchronized public void destroy()
    {
         java.util.HashSet<RetryTask> keep = new java.util.HashSet<RetryTask>();
        for(RetryTask task : _requests)
        {
            if(!task.destroy())
            {
                keep.add(task);
            }
        }
        _requests = keep;
        _instance = null;

        //
        // Wait for the tasks to be executed, it shouldn't take long
        // since they couldn't be canceled. If interrupted, we
        // preserve the interrupt.
        //
        boolean interrupted = false;
        while(!_requests.isEmpty())
        {
            try
            {
                wait();
            }
            catch(InterruptedException ex)
            {
                interrupted = true;
            }
        }
        if(interrupted)
        {
            Thread.currentThread().interrupt();
        }
    }

    synchronized boolean remove(RetryTask task)
    {
        boolean removed = _requests.remove(task);
        if(_instance == null && _requests.isEmpty())
        {
            notify();
        }
        return removed;
    }

    private Instance _instance;
    private java.util.HashSet<RetryTask> _requests = new java.util.HashSet<RetryTask>();
}