summaryrefslogtreecommitdiff
path: root/java/demo/Freeze/casino/BetResolver.java
blob: 145d4a8402b093122691eca0e82f8f99eaf9ae20 (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
// **********************************************************************
//
// Copyright (c) 2003-2009 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.
//
// **********************************************************************

class BetResolver
{
    BetResolver()
    {
        //
        // We create a little pool of timers
        //
        for(int i = 0; i < _timers.length; ++i)
        {
            _timers[i] = new java.util.Timer();
        }
    }

    void
    add(final CasinoStore.PersistentBetPrx bet, long closeTime)
    {
        java.util.TimerTask task = new java.util.TimerTask()
            {
                public void 
                run()
                {
                    //
                    // Note that this is a collocated call, from a non-dispatch
                    // thread; even then Freeze will properly create/commit the
                    // transaction.
                    //

                    try
                    {
                        bet.complete(_random.nextInt());
                    }
                    catch(Ice.ObjectNotExistException one)
                    {
                        //
                        // Looks like this bet was never saved (committed)
                        //
                    }
                    finally
                    {
                        synchronized(BetResolver.this)
                        {
                            _betCount--;
                        }
                    }
                }
            };

        _timers[_random.nextInt(_timers.length)].schedule(task, new java.util.Date(closeTime));

        synchronized(this)
        {
            _betCount++;
        }
    }

    void
    cancel()
    {
        for(java.util.Timer timer : _timers)
        {
            timer.cancel();
        }
    }

    synchronized int
    getBetCount()
    {
        return _betCount;
    }

    private java.util.Timer[] _timers = new java.util.Timer[3];
    private java.util.Random _random = new java.util.Random();
    private int _betCount = 0;
}