Skip to content

Commit 2201f6d

Browse files
committed
Improve performance of StompCommand.getMessageType()
Closes SPR-14636
1 parent 57cb7c7 commit 2201f6d

File tree

2 files changed

+114
-44
lines changed

2 files changed

+114
-44
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompCommand.java

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package org.springframework.messaging.simp.stomp;
1818

19-
import java.util.Arrays;
20-
import java.util.Collection;
21-
import java.util.HashMap;
22-
import java.util.Map;
23-
2419
import org.springframework.messaging.simp.SimpMessageType;
2520

2621
/**
@@ -32,62 +27,55 @@
3227
public enum StompCommand {
3328

3429
// client
35-
CONNECT,
36-
STOMP,
37-
DISCONNECT,
38-
SUBSCRIBE,
39-
UNSUBSCRIBE,
40-
SEND,
41-
ACK,
42-
NACK,
43-
BEGIN,
44-
COMMIT,
45-
ABORT,
30+
CONNECT(SimpMessageType.CONNECT, 0),
31+
STOMP(SimpMessageType.CONNECT, 0),
32+
DISCONNECT(SimpMessageType.DISCONNECT, 0),
33+
SUBSCRIBE(SimpMessageType.SUBSCRIBE, 3),
34+
UNSUBSCRIBE(SimpMessageType.UNSUBSCRIBE, 2),
35+
SEND(SimpMessageType.MESSAGE, 13),
36+
ACK(SimpMessageType.OTHER, 0),
37+
NACK(SimpMessageType.OTHER, 0),
38+
BEGIN(SimpMessageType.OTHER, 0),
39+
COMMIT(SimpMessageType.OTHER, 0),
40+
ABORT(SimpMessageType.OTHER, 0),
4641

4742
// server
48-
CONNECTED,
49-
MESSAGE,
50-
RECEIPT,
51-
ERROR;
52-
53-
54-
private static Map<StompCommand, SimpMessageType> messageTypes = new HashMap<>();
55-
static {
56-
messageTypes.put(StompCommand.CONNECT, SimpMessageType.CONNECT);
57-
messageTypes.put(StompCommand.STOMP, SimpMessageType.CONNECT);
58-
messageTypes.put(StompCommand.SEND, SimpMessageType.MESSAGE);
59-
messageTypes.put(StompCommand.MESSAGE, SimpMessageType.MESSAGE);
60-
messageTypes.put(StompCommand.SUBSCRIBE, SimpMessageType.SUBSCRIBE);
61-
messageTypes.put(StompCommand.UNSUBSCRIBE, SimpMessageType.UNSUBSCRIBE);
62-
messageTypes.put(StompCommand.DISCONNECT, SimpMessageType.DISCONNECT);
43+
CONNECTED(SimpMessageType.OTHER, 0),
44+
MESSAGE(SimpMessageType.MESSAGE, 15),
45+
RECEIPT(SimpMessageType.OTHER, 0),
46+
ERROR(SimpMessageType.OTHER, 12);
47+
48+
private static final int DESTINATION_REQUIRED = 1;
49+
private static final int SUBSCRIPTION_ID_REQUIRED = 2;
50+
private static final int CONTENT_LENGTH_REQUIRED = 4;
51+
private static final int BODY_ALLOWED = 8;
52+
53+
private final SimpMessageType simpMessageType;
54+
private final int flags;
55+
56+
StompCommand(final SimpMessageType simpMessageType, final int flags) {
57+
this.simpMessageType = simpMessageType;
58+
this.flags = flags;
6359
}
6460

65-
private static Collection<StompCommand> destinationRequired = Arrays.asList(SEND, SUBSCRIBE, MESSAGE);
66-
private static Collection<StompCommand> subscriptionIdRequired = Arrays.asList(SUBSCRIBE, UNSUBSCRIBE, MESSAGE);
67-
private static Collection<StompCommand> contentLengthRequired = Arrays.asList(SEND, MESSAGE, ERROR);
68-
private static Collection<StompCommand> bodyAllowed = Arrays.asList(SEND, MESSAGE, ERROR);
69-
70-
71-
7261
public SimpMessageType getMessageType() {
73-
SimpMessageType type = messageTypes.get(this);
74-
return (type != null) ? type : SimpMessageType.OTHER;
62+
return simpMessageType;
7563
}
7664

7765
public boolean requiresDestination() {
78-
return destinationRequired.contains(this);
66+
return (flags & DESTINATION_REQUIRED) != 0;
7967
}
8068

8169
public boolean requiresSubscriptionId() {
82-
return subscriptionIdRequired.contains(this);
70+
return (flags & SUBSCRIPTION_ID_REQUIRED) != 0;
8371
}
8472

8573
public boolean requiresContentLength() {
86-
return contentLengthRequired.contains(this);
74+
return (flags & CONTENT_LENGTH_REQUIRED) != 0;
8775
}
8876

8977
public boolean isBodyAllowed() {
90-
return bodyAllowed.contains(this);
78+
return (flags & BODY_ALLOWED) != 0;
9179
}
9280

9381
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.springframework.messaging.simp.stomp;
2+
3+
import org.junit.Test;
4+
import org.springframework.messaging.simp.SimpMessageType;
5+
6+
import java.util.Arrays;
7+
import java.util.Collection;
8+
import java.util.EnumMap;
9+
import java.util.Map;
10+
11+
import static org.junit.Assert.*;
12+
13+
public class StompCommandTest {
14+
15+
private static final Collection<StompCommand> destinationRequired = Arrays.asList(StompCommand.SEND, StompCommand.SUBSCRIBE, StompCommand.MESSAGE);
16+
private static final Collection<StompCommand> subscriptionIdRequired = Arrays.asList(StompCommand.SUBSCRIBE, StompCommand.UNSUBSCRIBE, StompCommand.MESSAGE);
17+
private static final Collection<StompCommand> contentLengthRequired = Arrays.asList(StompCommand.SEND, StompCommand.MESSAGE, StompCommand.ERROR);
18+
private static final Collection<StompCommand> bodyAllowed = Arrays.asList(StompCommand.SEND, StompCommand.MESSAGE, StompCommand.ERROR);
19+
20+
private static final Map<StompCommand, SimpMessageType> messageTypes = new EnumMap<>(StompCommand.class);
21+
22+
static {
23+
messageTypes.put(StompCommand.CONNECT, SimpMessageType.CONNECT);
24+
messageTypes.put(StompCommand.STOMP, SimpMessageType.CONNECT);
25+
messageTypes.put(StompCommand.SEND, SimpMessageType.MESSAGE);
26+
messageTypes.put(StompCommand.MESSAGE, SimpMessageType.MESSAGE);
27+
messageTypes.put(StompCommand.SUBSCRIBE, SimpMessageType.SUBSCRIBE);
28+
messageTypes.put(StompCommand.UNSUBSCRIBE, SimpMessageType.UNSUBSCRIBE);
29+
messageTypes.put(StompCommand.DISCONNECT, SimpMessageType.DISCONNECT);
30+
}
31+
32+
@Test
33+
public void getMessageType() throws Exception {
34+
for (final Map.Entry<StompCommand, SimpMessageType> stompToSimp : messageTypes.entrySet()) {
35+
assertEquals(stompToSimp.getKey().getMessageType(), stompToSimp.getValue());
36+
}
37+
}
38+
39+
@Test
40+
public void requiresDestination() throws Exception {
41+
for (final StompCommand stompCommand : StompCommand.values()) {
42+
if (destinationRequired.contains(stompCommand)) {
43+
assertTrue(stompCommand.requiresDestination());
44+
} else {
45+
assertFalse(stompCommand.requiresDestination());
46+
}
47+
}
48+
}
49+
50+
@Test
51+
public void requiresSubscriptionId() throws Exception {
52+
for (final StompCommand stompCommand : StompCommand.values()) {
53+
if (subscriptionIdRequired.contains(stompCommand)) {
54+
assertTrue(stompCommand.requiresSubscriptionId());
55+
} else {
56+
assertFalse(stompCommand.requiresSubscriptionId());
57+
}
58+
}
59+
}
60+
61+
@Test
62+
public void requiresContentLength() throws Exception {
63+
for (final StompCommand stompCommand : StompCommand.values()) {
64+
if (contentLengthRequired.contains(stompCommand)) {
65+
assertTrue(stompCommand.requiresContentLength());
66+
} else {
67+
assertFalse(stompCommand.requiresContentLength());
68+
}
69+
}
70+
}
71+
72+
@Test
73+
public void isBodyAllowed() throws Exception {
74+
for (final StompCommand stompCommand : StompCommand.values()) {
75+
if (bodyAllowed.contains(stompCommand)) {
76+
assertTrue(stompCommand.isBodyAllowed());
77+
} else {
78+
assertFalse(stompCommand.isBodyAllowed());
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)