-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add #12 未処理のメッセージ数と1メッセージの処理時間を送信するPingSenderの実装
- Loading branch information
1 parent
d933c54
commit 59aaab9
Showing
4 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...ho.sample.mqttv5app/src/main/java/org/eclipse/paho/sample/mqttv5app/StatusPingSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.eclipse.paho.sample.mqttv5app; | ||
|
||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import org.eclipse.paho.mqttv5.client.ExtendedPingSender; | ||
import org.eclipse.paho.mqttv5.client.logging.Logger; | ||
import org.eclipse.paho.mqttv5.client.logging.LoggerFactory; | ||
import org.eclipse.paho.mqttv5.common.packet.MqttPingReq; | ||
|
||
import java.util.ArrayList; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class StatusPingSender extends ExtendedPingSender { | ||
private static final String CLASS_NAME = StatusPingSender.class.getName(); | ||
|
||
private int bufferSize; | ||
private ArrayList<Double> processingTimeBuffer; | ||
private double processingTimePerMsg; | ||
|
||
private ObjectMapper mapper; | ||
|
||
public StatusPingSender(int bufferSize) { | ||
this.bufferSize = bufferSize; | ||
processingTimeBuffer = new ArrayList<>(bufferSize); | ||
processingTimePerMsg = 0; | ||
mapper = new ObjectMapper(); | ||
} | ||
|
||
public void updateProcessingTimePerMsg(double processingTime){ | ||
final String methodName = "updateProcessingTimePerMsg"; | ||
|
||
double totalProcessingTime = processingTimePerMsg * processingTimeBuffer.size(); | ||
if (processingTimeBuffer.size() == bufferSize) { | ||
double element = processingTimeBuffer.remove(0); | ||
totalProcessingTime -= element; | ||
log.info(CLASS_NAME, methodName, "Buffer is full so removed first element({0}).", new Object[]{element}); | ||
} | ||
totalProcessingTime += processingTime; | ||
processingTimeBuffer.add(new Double(processingTime)); | ||
log.info(CLASS_NAME, methodName, "Added {0} to the end of buffer.", new Object[]{processingTime}); | ||
|
||
processingTimePerMsg = totalProcessingTime / processingTimeBuffer.size(); | ||
} | ||
|
||
@Override | ||
protected MqttPingReq createPingreq() { | ||
final String methodName = "cratePingreq"; | ||
|
||
Payload payload = new Payload(comms.getNumberOfMsgsUnprocessed(), processingTimePerMsg); | ||
try { | ||
String jsonPayload = mapper.writeValueAsString(payload); | ||
MqttPingReq pingReq = new MqttPingReq(jsonPayload.getBytes()); | ||
log.info(CLASS_NAME, methodName, "Create PINGREQ payload: {0}.", new Object[]{jsonPayload}); | ||
return pingReq; | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
class Payload { | ||
@JsonProperty("numberOfMsgsInQueue") | ||
public int numberOfMsgsInQueue; | ||
|
||
@JsonProperty("processingTimerPerMsg") | ||
public double processingTimerPerMsg; | ||
|
||
public Payload(int numberOfMsgsInQueue, double processingTimerPerMsg){ | ||
this.numberOfMsgsInQueue = numberOfMsgsInQueue; | ||
this.processingTimerPerMsg = processingTimerPerMsg; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters