1919import static mqttloader .Constants .PUB_CLIENT_ID_PREFIX ;
2020
2121import java .util .ArrayList ;
22+ import java .util .concurrent .Executors ;
23+ import java .util .concurrent .ScheduledExecutorService ;
2224import java .util .concurrent .ScheduledFuture ;
23- import java .util .concurrent .ScheduledThreadPoolExecutor ;
2425import java .util .concurrent .TimeUnit ;
2526
2627import mqttloader .Loader ;
3536public class Publisher implements Runnable , IClient {
3637 private MqttClient client ;
3738 private final String clientId ;
38- private String topic ;
39- private int payloadSize ;
39+ private final String topic ;
40+ private final int payloadSize ;
4041 private int numMessage ;
41- private int pubInterval ;
42+ private final int pubInterval ;
4243 private MqttMessage message = new MqttMessage ();
43- private boolean hasInterval ;
4444
45+ // If change publisher to be multi-threaded, throughputs (and others) should be thread-safe.
4546 private ArrayList <Throughput > throughputs = new ArrayList <>();
4647
47- private ScheduledThreadPoolExecutor service ;
48+ private ScheduledExecutorService service ;
4849 private ScheduledFuture future ;
4950
51+ private volatile boolean cancelled = false ;
52+
5053 public Publisher (int clientNumber , String broker , int qos , boolean retain , String topic , int payloadSize , int numMessage , int pubInterval ) {
5154 message .setQos (qos );
5255 message .setRetained (retain );
5356 this .topic = topic ;
5457 this .payloadSize = payloadSize ;
5558 this .numMessage = numMessage ;
5659 this .pubInterval = pubInterval ;
57- hasInterval = pubInterval > 0 ;
5860
5961 clientId = PUB_CLIENT_ID_PREFIX + String .format ("%06d" , clientNumber );
6062 MqttConnectionOptions options = new MqttConnectionOptions ();
@@ -68,20 +70,50 @@ public Publisher(int clientNumber, String broker, int qos, boolean retain, Strin
6870 }
6971
7072 @ Override
71- public void start () {
72- service = new ScheduledThreadPoolExecutor ( 1 );
73+ public void start (long delay ) {
74+ service = Executors . newSingleThreadScheduledExecutor ( );
7375 if (pubInterval ==0 ){
74- future = service .schedule (this , 0 , TimeUnit .MILLISECONDS );
76+ future = service .schedule (this , delay , TimeUnit .MILLISECONDS );
7577 }else {
76- future = service .scheduleAtFixedRate (this , 0 , pubInterval , TimeUnit .MILLISECONDS );
78+ future = service .scheduleAtFixedRate (this , delay , pubInterval , TimeUnit .MILLISECONDS );
7779 }
7880 }
7981
80- public void terminate () {
81- service .shutdown ();
82+ @ Override
83+ public void run () {
84+ if (!client .isConnected ()) {
85+ Loader .countDownLatch .countDown ();
86+ } else {
87+ if (pubInterval ==0 ){
88+ continuousRun ();
89+ }else {
90+ periodicalRun ();
91+ }
92+ }
93+ }
94+
95+ public void continuousRun () {
96+ for (int i =0 ;i <numMessage ;i ++){
97+ if (cancelled ) {
98+ break ;
99+ }
100+ publish ();
101+ }
102+
82103 Loader .countDownLatch .countDown ();
83104 }
84105
106+ public void periodicalRun () {
107+ if (numMessage > 0 ) {
108+ publish ();
109+
110+ numMessage --;
111+ if (numMessage ==0 ){
112+ Loader .countDownLatch .countDown ();
113+ }
114+ }
115+ }
116+
85117 public void publish () {
86118 message .setPayload (Util .genPayloads (payloadSize ));
87119 try {
@@ -105,42 +137,18 @@ public void publish() {
105137 Loader .logger .fine ("Published a message (" + topic + "): " +clientId );
106138 }
107139
108- public void periodicalRun () {
109- publish ();
110-
111- numMessage --;
112- if (numMessage ==0 ){
113- terminate ();
114- }
115- }
116-
117- public void continuousRun () {
118- for (int i =0 ;i <numMessage ;i ++){
119- if (future .isCancelled ()) break ;
120- publish ();
121- }
122-
123- terminate ();
124- }
125-
126- @ Override
127- public void run () {
128- if (!client .isConnected ()) {
129- terminate ();
130- }
131-
132- if (pubInterval ==0 ){
133- continuousRun ();
134- }else {
135- periodicalRun ();
136- }
137- }
138-
139140 @ Override
140141 public void disconnect () {
141142 if (!future .isDone ()) {
143+ cancelled = true ;
142144 future .cancel (false );
143- service .shutdown ();
145+ }
146+
147+ service .shutdown ();
148+ try {
149+ service .awaitTermination (1 , TimeUnit .SECONDS );
150+ } catch (InterruptedException e ) {
151+ e .printStackTrace ();
144152 }
145153
146154 try {
0 commit comments