Skip to content

Commit

Permalink
Re-use EvaluationContext in DefaultSubscriptionRegistry
Browse files Browse the repository at this point in the history
Rather than create a new EvaluationContext instance per evaluation, we
now create a statically shared instance, without the root object in it,
and re-use it for all evalutations.
  • Loading branch information
rstoyanchev committed Mar 22, 2018
1 parent 19293b9 commit e0de912
Showing 1 changed file with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,15 +26,14 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;

import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.SimpleEvaluationContext;

This comment has been minimized.

Copy link
@lomo-dcm

lomo-dcm May 4, 2018

SimpleEvaluationContext 这个是哪个jar包里面的

import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
Expand Down Expand Up @@ -65,6 +64,11 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
/** Default maximum number of entries for the destination cache: 1024 */
public static final int DEFAULT_CACHE_LIMIT = 1024;

/** Static evaluation context to re-use */
private static SimpleEvaluationContext evaluationContext = SimpleEvaluationContext.builder()
.propertyAccessor(new SimpMessageHeaderPropertyAccessor()).build();



private PathMatcher pathMatcher = new AntPathMatcher();

Expand Down Expand Up @@ -192,7 +196,6 @@ private MultiValueMap<String, String> filterSubscriptions(
if (!this.selectorHeaderInUse) {
return allMatches;
}
EvaluationContext context = null;
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(allMatches.size());
for (String sessionId : allMatches.keySet()) {
for (String subId : allMatches.get(sessionId)) {
Expand All @@ -209,12 +212,8 @@ private MultiValueMap<String, String> filterSubscriptions(
result.add(sessionId, subId);
continue;
}
if (context == null) {
context = new StandardEvaluationContext(message);
context.getPropertyAccessors().add(new SimpMessageHeaderPropertyAccessor());
}
try {
if (Boolean.TRUE.equals(expression.getValue(context, Boolean.class))) {
if (Boolean.TRUE.equals(expression.getValue(evaluationContext, message, Boolean.class))) {
result.add(sessionId, subId);
}
}
Expand Down Expand Up @@ -533,7 +532,7 @@ private static class SimpMessageHeaderPropertyAccessor implements PropertyAccess

@Override
public Class<?>[] getSpecificTargetClasses() {
return new Class<?>[] {MessageHeaders.class};
return new Class<?>[] {Message.class, MessageHeaders.class};
}

@Override
Expand All @@ -542,21 +541,29 @@ public boolean canRead(EvaluationContext context, @Nullable Object target, Strin
}

@Override
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException {
Assert.state(target instanceof MessageHeaders, "No MessageHeaders");
MessageHeaders headers = (MessageHeaders) target;
SimpMessageHeaderAccessor accessor =
MessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) {
Object value;
if ("destination".equalsIgnoreCase(name)) {
value = accessor.getDestination();
if (target instanceof Message) {
value = name.equals("headers") ? ((Message) target).getHeaders() : null;
}
else {
value = accessor.getFirstNativeHeader(name);
if (value == null) {
value = headers.get(name);
else if (target instanceof MessageHeaders) {
MessageHeaders headers = (MessageHeaders) target;
SimpMessageHeaderAccessor accessor =
MessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
if ("destination".equalsIgnoreCase(name)) {
value = accessor.getDestination();
}
else {
value = accessor.getFirstNativeHeader(name);
if (value == null) {
value = headers.get(name);
}
}
}
else {
// Should never happen...
throw new IllegalStateException("Expected Message or MessageHeaders.");
}
return new TypedValue(value);
}
Expand Down

0 comments on commit e0de912

Please sign in to comment.