0
獲得大量活動連接到您的服務器是真的嗎? 我寫了一個簡單的http服務器,我需要知道他有多少個活動連接。如何獲取活動連接數?
我試過,但它給了我錯誤的結果之後10000個請求
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
StatusData.decreaseConnectionCounter();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
log.info("Channel " + ctx.channel() + " is now active");
StatusData.increaseConnectionCounter();
}
我改變我的課,所以它看起來像這樣StatusData,當我生成100個線程10000個請求它正確計數。
class StatusData{
private AtomicInteger counter = new AtomicInteger();
public void increaseConnectionCounter() {
synchronized (counter){
int newValue = counter.intValue() + 1;
counter.set(newValue);
}
}
public void decreaseConnectionCounter() {
synchronized (counter){
int newValue = counter.intValue() - 1;
counter.set(newValue);
}
}
public int getActiveConnectionCounter() {
return counter.get();
}
}
你是否在StatusData中使用了'AtomicInteger'? –
現在我用戶AtomicInteger,謝謝。 – Belict