1 package org.apache.commons.jcs3.utils.discovery;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.concurrent.ConcurrentHashMap;
23 import java.util.concurrent.ConcurrentMap;
24
25 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheManager;
26 import org.apache.commons.jcs3.engine.behavior.IElementSerializer;
27 import org.apache.commons.jcs3.engine.behavior.IProvideScheduler;
28 import org.apache.commons.jcs3.log.Log;
29 import org.apache.commons.jcs3.log.LogManager;
30 import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
31
32
33
34
35
36 public class UDPDiscoveryManager
37 {
38
39 private static final Log log = LogManager.getLog( UDPDiscoveryManager.class );
40
41
42 private static final UDPDiscoveryManager INSTANCE = new UDPDiscoveryManager();
43
44
45 private final ConcurrentMap<String, UDPDiscoveryService> services = new ConcurrentHashMap<>();
46
47
48 private UDPDiscoveryManager()
49 {
50
51 }
52
53
54
55
56
57
58 public static UDPDiscoveryManager getInstance()
59 {
60 return INSTANCE;
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @Deprecated
78 public UDPDiscoveryService getService( final String discoveryAddress, final int discoveryPort, final int servicePort,
79 final ICompositeCacheManager cacheMgr )
80 {
81 return getService(discoveryAddress, discoveryPort, null, servicePort, 0,
82 cacheMgr, new StandardSerializer());
83 }
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public UDPDiscoveryService getService( final String discoveryAddress, final int discoveryPort,
103 final String serviceAddress, final int servicePort, final int updTTL,
104 final ICompositeCacheManager cacheMgr, final IElementSerializer serializer )
105 {
106 final String key = String.join(":", discoveryAddress, String.valueOf(discoveryPort), String.valueOf(servicePort));
107
108 final UDPDiscoveryService service = services.computeIfAbsent(key, k -> {
109 log.info( "Creating service for address:port:servicePort [{0}]", key );
110
111 final UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
112 attributes.setUdpDiscoveryAddr(discoveryAddress);
113 attributes.setUdpDiscoveryPort(discoveryPort);
114 attributes.setServiceAddress(serviceAddress);
115 attributes.setServicePort(servicePort);
116 attributes.setUdpTTL(updTTL);
117
118 final UDPDiscoveryService newService = new UDPDiscoveryService(attributes, serializer);
119
120
121 cacheMgr.registerShutdownObserver( newService );
122
123
124 if ( cacheMgr instanceof IProvideScheduler)
125 {
126 newService.setScheduledExecutorService(((IProvideScheduler)cacheMgr)
127 .getScheduledExecutorService());
128 }
129
130 newService.startup();
131 return newService;
132 });
133
134 log.debug( "Returning service [{0}] for key [{1}]", service, key );
135
136 return service;
137 }
138 }