1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.ldap.replication.consumer;
21
22
23 import java.util.Queue;
24 import java.util.concurrent.ConcurrentLinkedQueue;
25
26 import org.apache.directory.api.ldap.model.constants.Loggers;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.slf4j.MDC;
30
31
32
33
34
35
36
37 public class PingerThread extends Thread
38 {
39
40 private static final Logger CONSUMER_LOG = LoggerFactory.getLogger( Loggers.CONSUMER_LOG.getName() );
41
42
43 private Queue<ReplicationConsumer> consumers = new ConcurrentLinkedQueue<>();
44
45
46 private boolean stop = false;
47
48
49 private long sleepTime = 5000;
50
51
52
53
54
55
56 public PingerThread( int sleepSec )
57 {
58 if ( sleepSec > 0 )
59 {
60 sleepTime = sleepSec * 1000L;
61 }
62
63 CONSUMER_LOG.info( "Configured pinger thread to sleep for {} seconds", ( sleepTime / 1000 ) );
64
65 setDaemon( true );
66 }
67
68
69
70
71
72 @Override
73 public void run()
74 {
75 try
76 {
77 if ( CONSUMER_LOG.isDebugEnabled() )
78 {
79 MDC.put( "Replica", "Pinger" );
80
81 CONSUMER_LOG.debug( "Starting the provider's pinger" );
82 }
83
84 while ( !stop )
85 {
86 for ( ReplicationConsumer consumer : consumers )
87 {
88 consumer.ping();
89 }
90
91 Thread.sleep( sleepTime );
92 }
93 }
94 catch ( InterruptedException ie )
95 {
96 CONSUMER_LOG.debug( "The pinger has been interrupted" );
97 }
98 }
99
100
101
102
103
104
105
106 public void addConsumer( ReplicationConsumer consumer )
107 {
108 if ( !consumers.contains( consumer ) )
109 {
110 consumers.add( consumer );
111 }
112 }
113
114
115
116
117
118
119 public void removeConsumer( ReplicationConsumer consumer )
120 {
121 consumers.remove( consumer );
122 }
123
124
125
126
127
128 public void stopPinging()
129 {
130 stop = true;
131 }
132 }