001package com.avaje.ebean.config;
002
003import java.util.ArrayList;
004import java.util.List;
005import java.util.Properties;
006
007/**
008 * Configuration for the container that holds the EbeanServer instances.
009 * <p>
010 * Provides configuration for cluster communication (if clustering is used). The cluster communication is
011 * used to invalidate appropriate parts of the L2 cache across the cluster.
012 */
013public class ContainerConfig {
014
015  /**
016   * Communication mode used for clustering.
017   */
018  public enum ClusterMode {
019
020    /**
021     * No clustering.
022     */
023    NONE,
024
025    /**
026     * Use Multicast networking for cluster wide communication.
027     */
028    MULTICAST,
029
030    /**
031     * Use TCP Sockets for cluster wide communication.
032     */
033    SOCKET
034  }
035
036  /**
037   * The cluster mode to use.
038   */
039  ClusterMode mode = ClusterMode.NONE;
040
041  /**
042   * Configuration if using TCP sockets for clustering communication.
043   */
044  SocketConfig socketConfig = new SocketConfig();
045
046  /**
047   * Configuration if using Multicast for clustering communication.
048   */
049  MulticastConfig multicastConfig = new MulticastConfig();
050
051
052  // -------------------------------------------------------------------------------------------
053  // MulticastConfig
054
055  /**
056   * The configuration for clustering using Multicast networking.
057   */
058  public static class MulticastConfig {
059
060    int managerSleepMillis = 80;
061    int lastSendTimeFreqSecs = 300;//5mins
062    int lastStatusTimeFreqSecs = 600;//10mins
063    int maxResendOutgoingAttempts = 200;
064    int maxResendIncomingRequests = 50;
065
066    int listenPort;
067    String listenAddress;
068    int sendPort;
069    String sendAddress;
070
071    // Note 1500 is Ethernet MTU and this must be less than UDP max packet size of 65507
072    int maxSendPacketSize = 1500;
073
074    // Whether to send packets even when there are no other members online
075    boolean sendWithNoMembers = true;
076
077    // When multiple instances are on same box you need to broadcast back locally
078    boolean disableLoopback;
079    int listenTimeToLive = -1;
080    int listenTimeout = 1000;
081    int listenBufferSize = 65500;
082    // For multihomed environment the address the listener should bind to
083    String listenBindAddress;
084
085    /**
086     * Return the manager sleep millis.
087     */
088    public int getManagerSleepMillis() {
089      return managerSleepMillis;
090    }
091
092    /**
093     * Set the manager sleep millis.
094     */
095    public void setManagerSleepMillis(int managerSleepMillis) {
096      this.managerSleepMillis = managerSleepMillis;
097    }
098
099    /**
100     * Return the last send time frequency.
101     */
102    public int getLastSendTimeFreqSecs() {
103      return lastSendTimeFreqSecs;
104    }
105
106    /**
107     * Set the last send time frequency.
108     */
109    public void setLastSendTimeFreqSecs(int lastSendTimeFreqSecs) {
110      this.lastSendTimeFreqSecs = lastSendTimeFreqSecs;
111    }
112
113    /**
114     * Return the last status time frequency.
115     */
116    public int getLastStatusTimeFreqSecs() {
117      return lastStatusTimeFreqSecs;
118    }
119
120    /**
121     * Set the last status time frequency.
122     */
123    public void setLastStatusTimeFreqSecs(int lastStatusTimeFreqSecs) {
124      this.lastStatusTimeFreqSecs = lastStatusTimeFreqSecs;
125    }
126
127    /**
128     * Return the maximum number of times we will try to re-send a given packet before giving up sending
129     */
130    public int getMaxResendOutgoingAttempts() {
131      return maxResendOutgoingAttempts;
132    }
133
134    /**
135     * Set the maximum retry attempts for outgoing messages.
136     */
137    public void setMaxResendOutgoingAttempts(int maxResendOutgoingAttempts) {
138      this.maxResendOutgoingAttempts = maxResendOutgoingAttempts;
139    }
140
141    /**
142     * Return the maximum number of times we will ask for a packet to be resent to us before giving up asking.
143     */
144    public int getMaxResendIncomingRequests() {
145      return maxResendIncomingRequests;
146    }
147
148    /**
149     * Set the maximum retry attempts for incoming messages.
150     */
151    public void setMaxResendIncomingRequests(int maxResendIncomingRequests) {
152      this.maxResendIncomingRequests = maxResendIncomingRequests;
153    }
154
155    /**
156     * Return the listen port.
157     */
158    public int getListenPort() {
159      return listenPort;
160    }
161
162    /**
163     * Set the listen port.
164     */
165    public void setListenPort(int port) {
166      this.listenPort = port;
167    }
168
169    /**
170     * Return the listen address.
171     */
172    public String getListenAddress() {
173      return listenAddress;
174    }
175
176    /**
177     * Set the listen address.
178     */
179    public void setListenAddress(String listenAddress) {
180      this.listenAddress = listenAddress;
181    }
182
183    /**
184     * Return the send port.
185     */
186    public int getSendPort() {
187      return sendPort;
188    }
189
190    /**
191     * Set the send port.
192     */
193    public void setSendPort(int sendPort) {
194      this.sendPort = sendPort;
195    }
196
197    /**
198     * Return the send address.
199     */
200    public String getSendAddress() {
201      return sendAddress;
202    }
203
204    /**
205     * Set the send address.
206     */
207    public void setSendAddress(String sendAddress) {
208      this.sendAddress = sendAddress;
209    }
210
211    /**
212     * Return the maximum send packet size.
213     */
214    public int getMaxSendPacketSize() {
215      return maxSendPacketSize;
216    }
217
218    /**
219     * Set the maximum send packet size.  Note 1500 is Ethernet MTU and this must be less than UDP max packet size of 65507.
220     */
221    public void setMaxSendPacketSize(int maxSendPacketSize) {
222      this.maxSendPacketSize = maxSendPacketSize;
223    }
224
225    /**
226     * Return true if send messages when no other members in the cluster are up.
227     */
228    public boolean isSendWithNoMembers() {
229      return sendWithNoMembers;
230    }
231
232    /**
233     * Set true if send messages when no other members in the cluster are up.
234     */
235    public void setSendWithNoMembers(boolean sendWithNoMembers) {
236      this.sendWithNoMembers = sendWithNoMembers;
237    }
238
239    /**
240     * Return true if loopback is disabled. When multiple instances are on same box you need to broadcast back locally.
241     */
242    public boolean isDisableLoopback() {
243      return disableLoopback;
244    }
245
246    /**
247     * Set if loopback is disabled. When multiple instances are on same box you need to broadcast back locally.
248     */
249    public void setDisableLoopback(boolean disableLoopback) {
250      this.disableLoopback = disableLoopback;
251    }
252
253    /**
254     * Return the listen time to live.
255     */
256    public int getListenTimeToLive() {
257      return listenTimeToLive;
258    }
259
260    /**
261     * Set the listen time to live.
262     */
263    public void setListenTimeToLive(int listenTimeToLive) {
264      this.listenTimeToLive = listenTimeToLive;
265    }
266
267    /**
268     * Return the listen timeout.
269     */
270    public int getListenTimeout() {
271      return listenTimeout;
272    }
273
274    /**
275     * set the listen timeout.
276     */
277    public void setListenTimeout(int listenTimeout) {
278      this.listenTimeout = listenTimeout;
279    }
280
281    /**
282     * Return the listen buffer size.
283     */
284    public int getListenBufferSize() {
285      return listenBufferSize;
286    }
287
288    /**
289     * Set the listen buffer size.
290     */
291    public void setListenBufferSize(int listenBufferSize) {
292      this.listenBufferSize = listenBufferSize;
293    }
294
295    /**
296     * Return the listener bind address (optional). For multihomed environment the address the listener should bind to.
297     */
298    public String getListenBindAddress() {
299      return listenBindAddress;
300    }
301
302    /**
303     * Set the listener bind address (optional). For multihomed environment the address the listener should bind to.
304     */
305    public void setListenBindAddress(String listenBindAddress) {
306      this.listenBindAddress = listenBindAddress;
307    }
308  }
309
310  // -------------------------------------------------------------------------------------------
311  // SocketConfig
312
313  /**
314   * Configuration for clustering using TCP sockets.
315   * <p>
316   * This is good for when there are relatively small number of cluster members.
317   */
318  public static class SocketConfig {
319
320    /**
321     * This local server in host:port format.
322     */
323    String localHostPort;
324
325    /**
326     * All the cluster members in host:port format.
327     */
328    List<String> members = new ArrayList<String>();
329
330    /**
331     * core threads for the associated thread pool.
332     */
333    int coreThreads = 2;
334
335    /**
336     * Max threads for the associated thread pool.
337     */
338    int maxThreads = 16;
339
340    String threadPoolName = "EbeanCluster";
341
342    /**
343     * Return the host and port for this server instance.
344     */
345    public String getLocalHostPort() {
346      return localHostPort;
347    }
348
349    /**
350     * Set the host and port for this server instance.
351     */
352    public void setLocalHostPort(String localHostPort) {
353      this.localHostPort = localHostPort;
354    }
355
356    /**
357     * Return all the host and port for all the members of the cluster.
358     */
359    public List<String> getMembers() {
360      return members;
361    }
362
363    /**
364     * Set all the host and port for all the members of the cluster.
365     */
366    public void setMembers(List<String> members) {
367      this.members = members;
368    }
369
370    /**
371     * Return the number of core threads to use.
372     */
373    public int getCoreThreads() {
374      return coreThreads;
375    }
376
377    /**
378     * Set the number of core threads to use.
379     */
380    public void setCoreThreads(int coreThreads) {
381      this.coreThreads = coreThreads;
382    }
383
384    /**
385     * Return the number of max threads to use.
386     */
387    public int getMaxThreads() {
388      return maxThreads;
389    }
390
391    /**
392     * Set the number of max threads to use.
393     */
394    public void setMaxThreads(int maxThreads) {
395      this.maxThreads = maxThreads;
396    }
397
398    /**
399     * Return the thread pool name.
400     */
401    public String getThreadPoolName() {
402      return threadPoolName;
403    }
404
405    /**
406     * Set the thread pool name.
407     */
408    public void setThreadPoolName(String threadPoolName) {
409      this.threadPoolName = threadPoolName;
410    }
411  }
412
413  // -------------------------------------------------------------------------------------------
414  // Members
415
416  /**
417   * Load the settings from properties.
418   */
419  public void loadFromProperties(Properties properties) {
420    //TODO
421  }
422
423  /**
424   * Return the cluster mode.
425   */
426  public ClusterMode getMode() {
427    return mode;
428  }
429
430  /**
431   * Set the cluster mode.
432   */
433  public void setMode(ClusterMode mode) {
434    this.mode = mode;
435  }
436
437  /**
438   * Return the socket communication configuration.
439   */
440  public SocketConfig getSocketConfig() {
441    return socketConfig;
442  }
443
444  /**
445   * Set the socket communication configuration.
446   */
447  public void setSocketConfig(SocketConfig socketConfig) {
448    this.socketConfig = socketConfig;
449  }
450
451  /**
452   * Return the multicast communication configuration.
453   */
454  public MulticastConfig getMulticastConfig() {
455    return multicastConfig;
456  }
457
458  /**
459   * Set the multicast communication configuration.
460   */
461  public void setMulticastConfig(MulticastConfig multicastConfig) {
462    this.multicastConfig = multicastConfig;
463  }
464}