summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/ConnectionFlushBatch.java
blob: 4b3da0bcb5ed4f59a019224e877d890a46c457d8 (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
118
119
120
121
122
123
124
125
126
127
128
// **********************************************************************
//
// 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;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;

import Ice.CommunicatorDestroyedException;

public class ConnectionFlushBatch extends OutgoingAsyncBase
{
    public static ConnectionFlushBatch check(Ice.AsyncResult r, Ice.Connection con, String operation)
    {
        check(r, operation);
        if(!(r instanceof ConnectionFlushBatch))
        {
            throw new IllegalArgumentException("Incorrect AsyncResult object for end_" + operation + " method");
        }
        if(r.getConnection() != con)
        {
            throw new IllegalArgumentException("Connection for call to end_" + operation +
                                               " does not match connection that was used to call corresponding " +
                                               "begin_" + operation + " method");
        }
        return (ConnectionFlushBatch)r;
    }

    public ConnectionFlushBatch(Ice.ConnectionI con, Ice.Communicator communicator, Instance instance,
                                String operation, CallbackBase callback)
    {
        super(communicator, instance, operation, callback);
        _connection = con;
    }

    @Override
    public Ice.Connection getConnection()
    {
        return _connection;
    }
    
    public void invoke()
    {
        try
        {
            int status;
            if(_instance.queueRequests())
            {
                Future<Integer> future = _instance.getQueueExecutor().submit(
                    new Callable<Integer>()
                    {
                        @Override
                        public Integer call() throws RetryException
                        {
                            return _connection.flushAsyncBatchRequests(ConnectionFlushBatch.this);
                        }
                    });
                
                boolean interrupted = false;
                while(true)
                {
                    try 
                    {
                        status = future.get();
                        if(interrupted)
                        {
                            Thread.currentThread().interrupt();
                        }
                        break;
                    }
                    catch(InterruptedException ex)
                    {
                        interrupted = true;
                    }
                    catch(RejectedExecutionException e)
                    {
                        throw new CommunicatorDestroyedException();
                    }
                    catch(ExecutionException e)
                    {
                        try
                        {
                            throw e.getCause();
                        }
                        catch(RuntimeException ex)
                        {
                            throw ex;
                        }
                        catch(Throwable ex)
                        {
                            assert(false);
                        }
                    }
                }
            }
            else
            {
                status = _connection.flushAsyncBatchRequests(this);
            }
            
            if((status & AsyncStatus.Sent) > 0)
            {
                _sentSynchronously = true;
                if((status & AsyncStatus.InvokeSentCallback) > 0)
                {
                    invokeSent();
                }
            }
        }
        catch(Ice.Exception ex)
        {
            if(completed(ex))
            {
                invokeCompletedAsync();
            }
        }
    }

    private Ice.ConnectionI _connection;
}