Skip to content

Commit

Permalink
[CAMEL-19479] Add threadId to simple language (#10475)
Browse files Browse the repository at this point in the history
* [CAMEL-19479] Add `threadId` to simple language

* [CAMEL-19479] Adding documentation

* [CAMEL-19479] Code formatting

---------

Co-authored-by: Adriano Machado <[email protected]>
  • Loading branch information
ammachado and ammachado authored Jun 27, 2023
1 parent e689d6f commit b474dcf
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,13 @@ public void testTrimSimpleExpressions() {
assertExpression("\n${in.body}\r".trim(), "<hello id='m123'>world!</hello>");
}

@Test
public void testSimpleThreadId() {
long id = Thread.currentThread().getId();
assertExpression("${threadId}", id);
assertExpression("The id is ${threadId}", "The id is " + id);
}

@Test
public void testSimpleThreadName() {
String name = Thread.currentThread().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ Exchange is being routed.
|stepId |String |Returns the id of the current step the
Exchange is being routed.

|threadId |String |Returns the id of the current thread. Can be used for
logging purpose.

|threadName |String |Returns the name of the current thread. Can be used for
logging purpose.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ public static String threadName() {
return Thread.currentThread().getName();
}

public static long threadId() {
return Thread.currentThread().getId();
}

public static String hostName() {
return InetAddressUtil.getLocalHostNameSafe();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ private Expression createSimpleExpressionDirectly(CamelContext camelContext, Str
return ExpressionBuilder.exchangeExceptionMessageExpression();
} else if (ObjectHelper.equal(expression, "exception.stacktrace")) {
return ExpressionBuilder.exchangeExceptionStackTraceExpression();
} else if (ObjectHelper.equal(expression, "threadId")) {
return ExpressionBuilder.threadIdExpression();
} else if (ObjectHelper.equal(expression, "threadName")) {
return ExpressionBuilder.threadNameExpression();
} else if (ObjectHelper.equal(expression, "hostname")) {
Expand Down Expand Up @@ -846,6 +848,8 @@ public String createCodeDirectly(String expression) throws SimpleParserException
return "exceptionMessage(exchange)";
} else if (ObjectHelper.equal(expression, "exception.stacktrace")) {
return "exceptionStacktrace(exchange)";
} else if (ObjectHelper.equal(expression, "threadId")) {
return "threadId()";
} else if (ObjectHelper.equal(expression, "threadName")) {
return "threadName()";
} else if (ObjectHelper.equal(expression, "hostname")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ public void testTrimSimpleExpressions() throws Exception {
assertExpression("\n${in.body}\r".trim(), "<hello id='m123'>world!</hello>");
}

@Test
public void testSimpleThreadId() throws Exception {
long id = Thread.currentThread().getId();
assertExpression("${threadId}", id);
assertExpression("The id is ${threadId}", "The id is " + id);
}

@Test
public void testSimpleThreadName() throws Exception {
String name = Thread.currentThread().getName();
Expand Down Expand Up @@ -337,7 +344,7 @@ public void testOGNLBodyListAndMap() throws Exception {
@Test
public void testOGNLBodyEmptyList() throws Exception {
Map<String, List<String>> map = new HashMap<>();
map.put("list", new ArrayList<String>());
map.put("list", new ArrayList<>());

exchange.getIn().setBody(map);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,24 @@ public static Expression singleInputExpression(String headerName, String propert
return exp;
}

/**
* Returns the expression for the current thread id
*/

public static Expression threadIdExpression() {
return new ExpressionAdapter() {
@Override
public Object evaluate(Exchange exchange) {
return Thread.currentThread().getId();
}

@Override
public String toString() {
return "threadId";
}
};
}

/**
* Returns the expression for the current thread name
*/
Expand Down
2 changes: 1 addition & 1 deletion tooling/maven/camel-package-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-shared-utils</artifactId>
<version>${maven-shared-utils-plugin-version}</version>
Expand Down

0 comments on commit b474dcf

Please sign in to comment.