NIFI-5867 - add thread() EL function to get thread name

moved thread name evaluation in evaluate method

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #3198
This commit is contained in:
Pierre Villard 2018-12-04 22:28:33 +01:00 committed by Matthew Burgess
parent 8e777203a0
commit 8a3d7a453a
6 changed files with 68 additions and 1 deletions

View File

@ -107,6 +107,7 @@ IP : 'ip';
UUID : 'UUID';
HOSTNAME : 'hostname'; // requires boolean arg: prefer FQDN
NOW : 'now';
THREAD : 'thread';
// 0 arg functions

View File

@ -128,7 +128,7 @@ functionCall : functionRef ->
^(FUNCTION_CALL functionRef);
booleanLiteral : TRUE | FALSE;
zeroArgStandaloneFunction : (IP | UUID | NOW | NEXT_INT | HOSTNAME | RANDOM) LPAREN! RPAREN!;
zeroArgStandaloneFunction : (IP | UUID | NOW | NEXT_INT | HOSTNAME | THREAD | RANDOM) LPAREN! RPAREN!;
oneArgStandaloneFunction : ((TO_LITERAL | MATH | GET_STATE_VALUE)^ LPAREN! anyArg RPAREN!) |
(HOSTNAME^ LPAREN! booleanLiteral RPAREN!);
standaloneFunction : zeroArgStandaloneFunction | oneArgStandaloneFunction;

View File

@ -91,6 +91,7 @@ import org.apache.nifi.attribute.expression.language.evaluation.functions.Substr
import org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeEvaluator;
import org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeLastEvaluator;
import org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringEvaluator;
import org.apache.nifi.attribute.expression.language.evaluation.functions.ThreadEvaluator;
import org.apache.nifi.attribute.expression.language.evaluation.functions.ToLowerEvaluator;
import org.apache.nifi.attribute.expression.language.evaluation.functions.ToRadixEvaluator;
import org.apache.nifi.attribute.expression.language.evaluation.functions.ToStringEvaluator;
@ -199,6 +200,7 @@ import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpre
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.SUBSTRING_AFTER_LAST;
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.SUBSTRING_BEFORE;
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.SUBSTRING_BEFORE_LAST;
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.THREAD;
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.TO_DATE;
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.TO_DECIMAL;
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.TO_LITERAL;
@ -1009,6 +1011,8 @@ public class ExpressionCompiler {
throw new AttributeExpressionLanguageException(e);
}
}
case THREAD:
return addToken(new ThreadEvaluator(), "thread");
case HOSTNAME: {
if (tree.getChildCount() == 0) {
try {

View File

@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.attribute.expression.language.evaluation.functions;
import java.util.Map;
import org.apache.nifi.attribute.expression.language.evaluation.Evaluator;
import org.apache.nifi.attribute.expression.language.evaluation.QueryResult;
import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator;
import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult;
public class ThreadEvaluator extends StringEvaluator {
@Override
public QueryResult<String> evaluate(final Map<String, String> attributes) {
// See org.apache.nifi.engine.FlowEngine
return new StringQueryResult(Thread.currentThread().getName());
}
@Override
public Evaluator<?> getSubjectEvaluator() {
return null;
}
}

View File

@ -1810,6 +1810,11 @@ public class TestQuery {
verifyEquals("${literal(true):ifElse(false, 'b')}", attributes, "false");
}
@Test
public void testThread() {
final Map<String, String> attributes = new HashMap<>();
verifyEquals("${thread()}", attributes, "main");
}
private void verifyEquals(final String expression, final Map<String, String> attributes, final Object expectedResult) {
verifyEquals(expression,attributes, null, expectedResult);

View File

@ -2062,6 +2062,24 @@ names begin with the letter `a`.
*Examples*: UpdateAttribute processor has stored the key "count" with value "20" in state. '${getStateValue("count")}` returns `20`.
[.function]
=== thread
*Description*: [.description]#Returns the name of the thread used by the processor when evaluating the Expression. This can be useful
when using a processor with multiple concurrent tasks and where some data uniqueness is required.#
*Subject Type*: [.subjectless]#No Subject#
*Arguments*: No arguments
*Return Type*: [.returnType]#String#
*Examples*: `${thread()}` could return something like `Timer-Driven Process Thread-4`.
[[multi]]
== Evaluating Multiple Attributes