aboutsummaryrefslogtreecommitdiff
path: root/juick-xmpp/src/main/java/com/juick/components/s2s/CleaningUp.java
blob: 52d2f3a6c9e2a70e852fceb4e379f37c0c4645aa (plain) (blame)
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
package com.juick.components.s2s;

import com.juick.components.XMPPServer;

import java.util.Iterator;

/**
 *
 * @author ugnich
 */
public class CleaningUp implements Runnable {
    XMPPServer xmpp;
    
    public CleaningUp(XMPPServer xmpp) {
        this.xmpp = xmpp;
    }

    @Override
    public void run() {
        while (true) {
            long now = System.currentTimeMillis();

            synchronized (xmpp.getOutConnections()) {
                for (Iterator<ConnectionOut> i = xmpp.getOutConnections().iterator(); i.hasNext(); ) {
                    ConnectionOut c = i.next();
                    int inactive = (int) ((double) (now - c.tsLocalData) / 1000.0);
                    if (inactive > 900) {
                        c.closeConnection();
                        i.remove();
                    }
                }
            }

            synchronized (xmpp.getInConnections()) {
                for (Iterator<ConnectionIn> i = xmpp.getInConnections().iterator(); i.hasNext(); ) {
                    ConnectionIn c = i.next();
                    int inactive = (int) ((double) (now - c.tsRemoteData) / 1000.0);
                    if (inactive > 900) {
                        c.closeConnection();
                        i.remove();
                    }
                }
            }

            synchronized (xmpp.getOutCache()) {
                for (Iterator<CacheEntry> i = xmpp.getOutCache().iterator(); i.hasNext(); ) {
                    CacheEntry c = i.next();
                    int inactive = (int) ((double) (now - c.tsCreated) / 1000.0);
                    if (inactive > 600) {
                        i.remove();
                    }
                }
            }
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
            }

        }
    }
}