Skip to content

Improve performance of StompCommand.getMessageType() #1148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@

package org.springframework.messaging.simp.stomp;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.springframework.messaging.simp.SimpMessageType;

/**
Expand All @@ -32,62 +27,55 @@
public enum StompCommand {

// client
CONNECT,
STOMP,
DISCONNECT,
SUBSCRIBE,
UNSUBSCRIBE,
SEND,
ACK,
NACK,
BEGIN,
COMMIT,
ABORT,
CONNECT(SimpMessageType.CONNECT, 0),
STOMP(SimpMessageType.CONNECT, 0),
DISCONNECT(SimpMessageType.DISCONNECT, 0),
SUBSCRIBE(SimpMessageType.SUBSCRIBE, 3),
UNSUBSCRIBE(SimpMessageType.UNSUBSCRIBE, 2),
SEND(SimpMessageType.MESSAGE, 13),
ACK(SimpMessageType.OTHER, 0),
NACK(SimpMessageType.OTHER, 0),
BEGIN(SimpMessageType.OTHER, 0),
COMMIT(SimpMessageType.OTHER, 0),
ABORT(SimpMessageType.OTHER, 0),

// server
CONNECTED,
MESSAGE,
RECEIPT,
ERROR;


private static Map<StompCommand, SimpMessageType> messageTypes = new HashMap<>();
static {
messageTypes.put(StompCommand.CONNECT, SimpMessageType.CONNECT);
messageTypes.put(StompCommand.STOMP, SimpMessageType.CONNECT);
messageTypes.put(StompCommand.SEND, SimpMessageType.MESSAGE);
messageTypes.put(StompCommand.MESSAGE, SimpMessageType.MESSAGE);
messageTypes.put(StompCommand.SUBSCRIBE, SimpMessageType.SUBSCRIBE);
messageTypes.put(StompCommand.UNSUBSCRIBE, SimpMessageType.UNSUBSCRIBE);
messageTypes.put(StompCommand.DISCONNECT, SimpMessageType.DISCONNECT);
CONNECTED(SimpMessageType.OTHER, 0),
MESSAGE(SimpMessageType.MESSAGE, 15),
RECEIPT(SimpMessageType.OTHER, 0),
ERROR(SimpMessageType.OTHER, 12);

private static final int DESTINATION_REQUIRED = 1;
private static final int SUBSCRIPTION_ID_REQUIRED = 2;
private static final int CONTENT_LENGTH_REQUIRED = 4;
private static final int BODY_ALLOWED = 8;

private final SimpMessageType simpMessageType;
private final int flags;

StompCommand(final SimpMessageType simpMessageType, final int flags) {
this.simpMessageType = simpMessageType;
this.flags = flags;
}

private static Collection<StompCommand> destinationRequired = Arrays.asList(SEND, SUBSCRIBE, MESSAGE);
private static Collection<StompCommand> subscriptionIdRequired = Arrays.asList(SUBSCRIBE, UNSUBSCRIBE, MESSAGE);
private static Collection<StompCommand> contentLengthRequired = Arrays.asList(SEND, MESSAGE, ERROR);
private static Collection<StompCommand> bodyAllowed = Arrays.asList(SEND, MESSAGE, ERROR);



public SimpMessageType getMessageType() {
SimpMessageType type = messageTypes.get(this);
return (type != null) ? type : SimpMessageType.OTHER;
return simpMessageType;
}

public boolean requiresDestination() {
return destinationRequired.contains(this);
return (flags & DESTINATION_REQUIRED) != 0;
}

public boolean requiresSubscriptionId() {
return subscriptionIdRequired.contains(this);
return (flags & SUBSCRIPTION_ID_REQUIRED) != 0;
}

public boolean requiresContentLength() {
return contentLengthRequired.contains(this);
return (flags & CONTENT_LENGTH_REQUIRED) != 0;
}

public boolean isBodyAllowed() {
return bodyAllowed.contains(this);
return (flags & BODY_ALLOWED) != 0;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.springframework.messaging.simp.stomp;

import org.junit.Test;
import org.springframework.messaging.simp.SimpMessageType;

import java.util.Arrays;
import java.util.Collection;
import java.util.EnumMap;
import java.util.Map;

import static org.junit.Assert.*;

public class StompCommandTest {

private static final Collection<StompCommand> destinationRequired = Arrays.asList(StompCommand.SEND, StompCommand.SUBSCRIBE, StompCommand.MESSAGE);
private static final Collection<StompCommand> subscriptionIdRequired = Arrays.asList(StompCommand.SUBSCRIBE, StompCommand.UNSUBSCRIBE, StompCommand.MESSAGE);
private static final Collection<StompCommand> contentLengthRequired = Arrays.asList(StompCommand.SEND, StompCommand.MESSAGE, StompCommand.ERROR);
private static final Collection<StompCommand> bodyAllowed = Arrays.asList(StompCommand.SEND, StompCommand.MESSAGE, StompCommand.ERROR);

private static final Map<StompCommand, SimpMessageType> messageTypes = new EnumMap<>(StompCommand.class);

static {
messageTypes.put(StompCommand.CONNECT, SimpMessageType.CONNECT);
messageTypes.put(StompCommand.STOMP, SimpMessageType.CONNECT);
messageTypes.put(StompCommand.SEND, SimpMessageType.MESSAGE);
messageTypes.put(StompCommand.MESSAGE, SimpMessageType.MESSAGE);
messageTypes.put(StompCommand.SUBSCRIBE, SimpMessageType.SUBSCRIBE);
messageTypes.put(StompCommand.UNSUBSCRIBE, SimpMessageType.UNSUBSCRIBE);
messageTypes.put(StompCommand.DISCONNECT, SimpMessageType.DISCONNECT);
}

@Test
public void getMessageType() throws Exception {
for (final Map.Entry<StompCommand, SimpMessageType> stompToSimp : messageTypes.entrySet()) {
assertEquals(stompToSimp.getKey().getMessageType(), stompToSimp.getValue());
}
}

@Test
public void requiresDestination() throws Exception {
for (final StompCommand stompCommand : StompCommand.values()) {
if (destinationRequired.contains(stompCommand)) {
assertTrue(stompCommand.requiresDestination());
} else {
assertFalse(stompCommand.requiresDestination());
}
}
}

@Test
public void requiresSubscriptionId() throws Exception {
for (final StompCommand stompCommand : StompCommand.values()) {
if (subscriptionIdRequired.contains(stompCommand)) {
assertTrue(stompCommand.requiresSubscriptionId());
} else {
assertFalse(stompCommand.requiresSubscriptionId());
}
}
}

@Test
public void requiresContentLength() throws Exception {
for (final StompCommand stompCommand : StompCommand.values()) {
if (contentLengthRequired.contains(stompCommand)) {
assertTrue(stompCommand.requiresContentLength());
} else {
assertFalse(stompCommand.requiresContentLength());
}
}
}

@Test
public void isBodyAllowed() throws Exception {
for (final StompCommand stompCommand : StompCommand.values()) {
if (bodyAllowed.contains(stompCommand)) {
assertTrue(stompCommand.isBodyAllowed());
} else {
assertFalse(stompCommand.isBodyAllowed());
}
}
}
}