35
35
import org .ros .node .topic .PublisherListener ;
36
36
import org .ros .node .topic .Subscriber ;
37
37
38
+ import java .util .concurrent .CountDownLatch ;
38
39
import java .util .concurrent .ScheduledExecutorService ;
39
40
import java .util .concurrent .TimeUnit ;
40
41
@@ -63,6 +64,7 @@ public class DefaultPublisher<T> extends DefaultTopicParticipant implements Publ
63
64
private final ListenerGroup <PublisherListener <T >> listeners ;
64
65
private final NodeIdentifier nodeIdentifier ;
65
66
private final MessageFactory messageFactory ;
67
+ private CountDownLatch shutdownLatch ;
66
68
67
69
public DefaultPublisher (NodeIdentifier nodeIdentifier , TopicDeclaration topicDeclaration ,
68
70
MessageSerializer <T > serializer , MessageFactory messageFactory ,
@@ -105,9 +107,20 @@ public boolean getLatchMode() {
105
107
return outgoingMessageQueue .getLatchMode ();
106
108
}
107
109
110
+ /**
111
+ * Sends shutdown signals and awaits for them to be received by
112
+ * {@link DefaultPublisher#signalOnMasterUnregistrationSuccess()} or
113
+ * {@link DefaultPublisher#signalOnMasterUnregistrationFailure()} before continuing shutdown
114
+ */
108
115
@ Override
109
116
public void shutdown (long timeout , TimeUnit unit ) {
117
+ shutdownLatch = new CountDownLatch (listeners .size ());
110
118
signalOnShutdown (timeout , unit );
119
+ try {
120
+ shutdownLatch .await (timeout , unit );
121
+ } catch (InterruptedException e ) {
122
+ log .error (e .getMessage (), e );
123
+ }
111
124
outgoingMessageQueue .shutdown ();
112
125
listeners .shutdown ();
113
126
}
@@ -252,6 +265,7 @@ public void signalOnMasterUnregistrationSuccess() {
252
265
@ Override
253
266
public void run (PublisherListener <T > listener ) {
254
267
listener .onMasterUnregistrationSuccess (publisher );
268
+ shutdownLatch .countDown ();
255
269
}
256
270
});
257
271
}
@@ -269,6 +283,7 @@ public void signalOnMasterUnregistrationFailure() {
269
283
@ Override
270
284
public void run (PublisherListener <T > listener ) {
271
285
listener .onMasterUnregistrationFailure (publisher );
286
+ shutdownLatch .countDown ();
272
287
}
273
288
});
274
289
}
0 commit comments