Skip to content

Commit 61034a5

Browse files
committed
CAMEL-23279: fix SamplingDefinition.description() NumberFormatException with property placeholders
Avoid premature parsing of samplePeriod through TimeUtils.toDuration() in description(), which fails when the value contains unresolved property placeholders like {{sample.period}}. Return the raw string value instead, consistent with ThrottleDefinition.description().
1 parent 4970138 commit 61034a5

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

core/camel-core-model/src/main/java/org/apache/camel/model/SamplingDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected String description() {
8686
if (messageFrequency != null) {
8787
return "1 Exchange per " + getMessageFrequency() + " messages received";
8888
} else {
89-
return "1 Exchange per " + TimeUtils.printDuration(TimeUtils.toDuration(samplePeriod));
89+
return "1 Exchange per " + getSamplePeriod() + " millis";
9090
}
9191
}
9292

core/camel-core/src/test/java/org/apache/camel/processor/SamplingThrottlerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.Collections;
2222
import java.util.List;
23+
import java.util.Properties;
2324
import java.util.concurrent.ExecutorService;
2425
import java.util.concurrent.Executors;
2526

@@ -100,6 +101,17 @@ public void run() {
100101
executor.shutdownNow();
101102
}
102103

104+
@Test
105+
public void testSamplingWithPropertyPlaceholder() throws Exception {
106+
MockEndpoint mock = getMockEndpoint("mock:result");
107+
mock.expectedMinimumMessageCount(1);
108+
mock.setResultWaitTime(3000);
109+
110+
template.sendBody("direct:sample-placeholder", "<message>placeholder test</message>");
111+
112+
mock.assertIsSatisfied();
113+
}
114+
103115
@Test
104116
public void testSamplingUsingMessageFrequency() throws Exception {
105117
long totalMessages = 100;
@@ -156,6 +168,13 @@ private void validateDroppedExchanges(List<Exchange> sentExchanges, int expected
156168
assertEquals(expectedNotDroppedCount, notDropped);
157169
}
158170

171+
@Override
172+
protected Properties useOverridePropertiesWithPropertiesComponent() {
173+
Properties props = new Properties();
174+
props.put("sample.period", "1000");
175+
return props;
176+
}
177+
159178
@Override
160179
protected RouteBuilder createRouteBuilder() {
161180
return new RouteBuilder() {
@@ -171,6 +190,8 @@ public void configure() {
171190

172191
from("direct:sample-messageFrequency-via-dsl").sample().sampleMessageFrequency(5).to("mock:result");
173192

193+
from("direct:sample-placeholder").sample("{{sample.period}}").to("mock:result");
194+
174195
// END SNIPPET: e1
175196
}
176197
};

0 commit comments

Comments
 (0)