mirror of https://github.com/apache/nifi.git
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:
parent
8e777203a0
commit
8a3d7a453a
|
@ -107,6 +107,7 @@ IP : 'ip';
|
||||||
UUID : 'UUID';
|
UUID : 'UUID';
|
||||||
HOSTNAME : 'hostname'; // requires boolean arg: prefer FQDN
|
HOSTNAME : 'hostname'; // requires boolean arg: prefer FQDN
|
||||||
NOW : 'now';
|
NOW : 'now';
|
||||||
|
THREAD : 'thread';
|
||||||
|
|
||||||
|
|
||||||
// 0 arg functions
|
// 0 arg functions
|
||||||
|
|
|
@ -128,7 +128,7 @@ functionCall : functionRef ->
|
||||||
^(FUNCTION_CALL functionRef);
|
^(FUNCTION_CALL functionRef);
|
||||||
|
|
||||||
booleanLiteral : TRUE | FALSE;
|
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!) |
|
oneArgStandaloneFunction : ((TO_LITERAL | MATH | GET_STATE_VALUE)^ LPAREN! anyArg RPAREN!) |
|
||||||
(HOSTNAME^ LPAREN! booleanLiteral RPAREN!);
|
(HOSTNAME^ LPAREN! booleanLiteral RPAREN!);
|
||||||
standaloneFunction : zeroArgStandaloneFunction | oneArgStandaloneFunction;
|
standaloneFunction : zeroArgStandaloneFunction | oneArgStandaloneFunction;
|
||||||
|
|
|
@ -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.SubstringBeforeEvaluator;
|
||||||
import org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeLastEvaluator;
|
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.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.ToLowerEvaluator;
|
||||||
import org.apache.nifi.attribute.expression.language.evaluation.functions.ToRadixEvaluator;
|
import org.apache.nifi.attribute.expression.language.evaluation.functions.ToRadixEvaluator;
|
||||||
import org.apache.nifi.attribute.expression.language.evaluation.functions.ToStringEvaluator;
|
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_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;
|
||||||
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.SUBSTRING_BEFORE_LAST;
|
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_DATE;
|
||||||
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.TO_DECIMAL;
|
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.TO_DECIMAL;
|
||||||
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.TO_LITERAL;
|
import static org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser.TO_LITERAL;
|
||||||
|
@ -1009,6 +1011,8 @@ public class ExpressionCompiler {
|
||||||
throw new AttributeExpressionLanguageException(e);
|
throw new AttributeExpressionLanguageException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case THREAD:
|
||||||
|
return addToken(new ThreadEvaluator(), "thread");
|
||||||
case HOSTNAME: {
|
case HOSTNAME: {
|
||||||
if (tree.getChildCount() == 0) {
|
if (tree.getChildCount() == 0) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1810,6 +1810,11 @@ public class TestQuery {
|
||||||
verifyEquals("${literal(true):ifElse(false, 'b')}", attributes, "false");
|
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) {
|
private void verifyEquals(final String expression, final Map<String, String> attributes, final Object expectedResult) {
|
||||||
verifyEquals(expression,attributes, null, expectedResult);
|
verifyEquals(expression,attributes, null, expectedResult);
|
||||||
|
|
|
@ -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`.
|
*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]]
|
[[multi]]
|
||||||
== Evaluating Multiple Attributes
|
== Evaluating Multiple Attributes
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue