Skip to main content
summaryrefslogtreecommitdiffstats
blob: db219eb0ec6b99b98dabdef22a4a9de7c9aa996f (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
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Created on Jan 26, 2010
 *
 * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE
 */
package org.eclipse.osee.framework.messaging.services.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.eclipse.osee.framework.jdk.core.type.CompositeKeyHashMap;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.messaging.services.ServiceNotification;

/**
 * @author b1528444
 *
 */
class MonitorTimedOutServices implements Runnable {

	private CompositeKeyHashMap<String, String, Map<String, ServiceHealthPlusTimeout>> map;
	private CompositeKeyHashMap<String, String, List<ServiceNotification>> callbacks;
	
	public MonitorTimedOutServices(
			CompositeKeyHashMap<String, String, Map<String, ServiceHealthPlusTimeout>> map, CompositeKeyHashMap<String, String, List<ServiceNotification>> callbacks) {
		this.map = map;
		this.callbacks = callbacks;
	}

	@Override
	public void run() {
		List<ThreeItems> toRemove = new ArrayList<ThreeItems>();
		long currentSystemTime = System.currentTimeMillis();
		Set<Pair<String, String>> keySet = map.keySet();
		for(Pair<String, String> pair:keySet){
			Map<String, ServiceHealthPlusTimeout> items = map.get(pair.getFirst(), pair.getSecond());
			for(Entry<String, ServiceHealthPlusTimeout> key:items.entrySet()){
				if(key.getValue().isTimedOut(currentSystemTime)){
					toRemove.add(new ThreeItems(pair.getFirst(), pair.getSecond(), key.getKey()));
					List<ServiceNotification> list = callbacks.get(pair.getFirst(), pair.getSecond());
					for(ServiceNotification notify:list){
						notify.onServiceGone(key.getValue().getServiceHealth());
					}
				}
			}
		}
		for(ThreeItems item:toRemove){
			Map<String, ServiceHealthPlusTimeout> innerMap = map.get(item.first, item.second);
			innerMap.remove(item.key);
			System.out.println(item.key);
			if(innerMap.size() == 0){
				map.remove(item.first, item.second);
				System.out.println("removed " + item.first + item.second);
			}
		}
	}

	private static class ThreeItems {
		
		String first;
		String second;
		String key;
		
		ThreeItems(String first, String second, String key){
			this.first = first;
			this.second = second;
			this.key = key;
		}
	}

}

Back to the top