Reputation: 13
In jgroups 4.x, the methods getSentMessages()
and getReceivedMessages()
were available in the JChannel
class. But in jgroups 5.x, those methods do not exist on the class.
After several searches, I have implemented the following code. As you can see, the STATS protocol do not allow me to get directly the numbers of messages sent and received.
I have looked in the STATS source code and saw there is an attribute with name mstats
and type MsgStats
that seems to be the global statistics, but that attribute is protected, so reflection is needed to access it.
My goal is to get the total number of sent and received messages using jgroups 5. I think that what I wrote is not the correct way to do that. Any suggestions?
int vSent = 0;
int vRec = 0;
for (Protocol vProt : iChannel.getProtocolStack().getProtocols())
{
// not all protocols have the methods to get the messages numbers
// so I try to get infos from protols that has such methods
if (vProt instanceof FRAG)
{
vRec += ((FRAG) vProt).getNumberOfReceivedMessages();
vSent += ((FRAG) vProt).getNumberOfSentMessages();
}
else if (vProt instanceof NAKACK2)
{
vRec += ((NAKACK2) vProt).getNumMessagesReceived();
vSent += ((NAKACK2) vProt).getNumMessagesSent();
}
else if (vProt instanceof UNICAST3)
{
vRec += ((UNICAST3) vProt).getNumMessagesReceived();
vSent += ((UNICAST3) vProt).getNumMessagesSent();
}
else if (vProt instanceof STATS)
{
// NOT clear for me if the below code replaces at all the code above
try
{
Field[] vFields = vProt.getClass().getDeclaredFields();
for (Field vField : vFields)
{
vField.setAccessible(true);
Object vValue = vField.get(vProt);
if (vValue instanceof MsgStats)
{
MsgStats vStats = (MsgStats) vValue;
vRec += vStats.getNumMsgsReceived();
vSent += vStats.getNumMsgsSent();
break;
}
}
}
catch (Throwable e)
{
}
}
}
Upvotes: 0
Views: 14