Skip to content

Commit fe92486

Browse files
committed
Expose status in SubProtocolWebSocketHandler
Closes gh-22807
1 parent 87dd62a commit fe92486

File tree

1 file changed

+75
-9
lines changed

1 file changed

+75
-9
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/messaging/SubProtocolWebSocketHandler.java

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -99,7 +99,7 @@ public class SubProtocolWebSocketHandler
9999

100100
private final ReentrantLock sessionCheckLock = new ReentrantLock();
101101

102-
private final Stats stats = new Stats();
102+
private final DefaultStats stats = new DefaultStats();
103103

104104
private volatile boolean running = false;
105105

@@ -253,6 +253,15 @@ public String getStatsInfo() {
253253
return this.stats.toString();
254254
}
255255

256+
/**
257+
* Return a {@link Stats} object that containers various session counters.
258+
* @since 5.2
259+
*/
260+
public Stats getStats() {
261+
return this.stats;
262+
}
263+
264+
256265

257266
@Override
258267
public final void start() {
@@ -560,7 +569,28 @@ public String toString() {
560569
}
561570

562571

563-
private class Stats {
572+
/**
573+
* Contract for access to session counters.
574+
* @since 5.2
575+
*/
576+
public interface Stats {
577+
578+
int getTotalSessions();
579+
580+
int getWebSocketSessions();
581+
582+
int getHttpStreamingSessions();
583+
584+
int getHttpPollingSessions();
585+
586+
int getLimitExceededSessions();
587+
588+
int getNoMessagesReceivedSessions();
589+
590+
int getTransportErrorSessions();
591+
}
592+
593+
private class DefaultStats implements Stats {
564594

565595
private final AtomicInteger total = new AtomicInteger();
566596

@@ -576,28 +606,64 @@ private class Stats {
576606

577607
private final AtomicInteger transportError = new AtomicInteger();
578608

579-
public void incrementSessionCount(WebSocketSession session) {
609+
610+
@Override
611+
public int getTotalSessions() {
612+
return this.total.get();
613+
}
614+
615+
@Override
616+
public int getWebSocketSessions() {
617+
return this.webSocket.get();
618+
}
619+
620+
@Override
621+
public int getHttpStreamingSessions() {
622+
return this.httpStreaming.get();
623+
}
624+
625+
@Override
626+
public int getHttpPollingSessions() {
627+
return this.httpPolling.get();
628+
}
629+
630+
@Override
631+
public int getLimitExceededSessions() {
632+
return this.limitExceeded.get();
633+
}
634+
635+
@Override
636+
public int getNoMessagesReceivedSessions() {
637+
return this.noMessagesReceived.get();
638+
}
639+
640+
@Override
641+
public int getTransportErrorSessions() {
642+
return this.transportError.get();
643+
}
644+
645+
void incrementSessionCount(WebSocketSession session) {
580646
getCountFor(session).incrementAndGet();
581647
this.total.incrementAndGet();
582648
}
583649

584-
public void decrementSessionCount(WebSocketSession session) {
650+
void decrementSessionCount(WebSocketSession session) {
585651
getCountFor(session).decrementAndGet();
586652
}
587653

588-
public void incrementLimitExceededCount() {
654+
void incrementLimitExceededCount() {
589655
this.limitExceeded.incrementAndGet();
590656
}
591657

592-
public void incrementNoMessagesReceivedCount() {
658+
void incrementNoMessagesReceivedCount() {
593659
this.noMessagesReceived.incrementAndGet();
594660
}
595661

596-
public void incrementTransportError() {
662+
void incrementTransportError() {
597663
this.transportError.incrementAndGet();
598664
}
599665

600-
private AtomicInteger getCountFor(WebSocketSession session) {
666+
AtomicInteger getCountFor(WebSocketSession session) {
601667
if (session instanceof PollingSockJsSession) {
602668
return this.httpPolling;
603669
}

0 commit comments

Comments
 (0)