You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Andrea Evangelista (Jira)" <ji...@apache.org> on 2023/02/07 12:55:00 UTC

[jira] [Commented] (CAMEL-19014) SimpleLanguage cache issue

    [ https://issues.apache.org/jira/browse/CAMEL-19014?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17685292#comment-17685292 ] 

Andrea Evangelista commented on CAMEL-19014:
--------------------------------------------

Just an update if in the previous method


 public String resolveTemplate(String template, Exchange exchange) {        var simpleExpression = new SimpleExpression();
        simpleExpression.setExpression(template);        return simpleExpression.evaluate(exchange, String.class);

    }
I introduce a synchronization over the SimpleLanguage class it works as expected:


 public String resolveTemplate(String template, Exchange exchange) \{
    synchronized (SimpleLanguage.class){
        var simpleExpression = new SimpleExpression();
        simpleExpression.setExpression(template);
        return simpleExpression.evaluate(exchange, String.class);
    }
So maybe it's due a concurrency issue somewhere in code.
I hope this can help as well too.
Thanks in advance

> SimpleLanguage cache issue
> --------------------------
>
>                 Key: CAMEL-19014
>                 URL: https://issues.apache.org/jira/browse/CAMEL-19014
>             Project: Camel
>          Issue Type: Bug
>    Affects Versions: 3.19.0
>            Reporter: Andrea Evangelista
>            Priority: Major
>
> Hi guys, I'm attaching a test class that will reproduce the issue that I'm experiencing.
> As summary: The cache inside the SimpleLanguage class looks like it's going to be affected by the evaluation of the simple expression, producing unexpected results.
> When the method *resolveTemplate* is used the result is not the expected one.
> To make it work I have to simulate a new entry in the cache making use of the method *resolveTemplateNoCache.*
> Thanks in advance
> {code:java}
> public class SimpleLanguageTest extends CamelTestSupport {
>     @EndpointInject("mock:result")
>     private MockEndpoint mockResult;
>     @EndpointInject("mock:fail")
>     private MockEndpoint mockWithFailure;
>     @Test
>     public void whenSimpleLanguageNotUseCachedEntriesItWillNotFail() throws Exception {
>         mockResult.await(20, TimeUnit.SECONDS);
>         mockResult.expectedMessageCount(102);
>         assertMockEndpointsSatisfied();
>         List<Exchange> exchanges = mockResult.getExchanges();
>         exchanges.stream()
>                 .forEach(exchange -> assertTrue(exchange.getMessage().getHeader("#CustomHeader", String.class).equals("This is a test a with startLabel: `Document` endLabel: `Document` and label: `ALabel`")));
>     }
>     @Test
>     public void whenSimpleLanguageUseCachedEntriesItWillFail() throws Exception {
>         mockWithFailure.expectedMessageCount(102);
>         assertMockEndpointsSatisfied();
>         List<Exchange> exchanges = mockWithFailure.getExchanges();
>         exchanges.stream()
>                 .forEach(exchange -> assertTrue(exchange.getMessage().getHeader("#CustomHeader", String.class).equals("This is a test a with startLabel: `Document` endLabel: `Document` and label: `ALabel`")));
>     }
>     @Override
>     protected RoutesBuilder createRouteBuilder() throws Exception {
>         return new RouteBuilder() {
>             Map body = new HashMap() {{
>                 put("label", "ALabel");
>                 put("startLabel", "Document");
>                 put("endLabel", "Document");
>             }};
>             String simpleTemplate = "This is a test a with startLabel: `${body.get('startLabel')}` endLabel: `${body.get('endLabel')}` and label: `${body.get('label')}`";
>             @Override
>             public void configure() throws Exception {
>                 from("timer://test-timer?fixedRate=true&period=10&delay=1")
>                         .process(new Processor() {
>                             @Override
>                             public void process(Exchange exchange) throws Exception {
>                                 exchange.getMessage().setBody(body);
>                                 exchange.getMessage().setHeader("#CustomHeader", resolveTemplateNoCache(simpleTemplate, exchange));
>                             }
>                         })
>                         .to("mock:result");
>                 from("timer://test-timer1?fixedRate=true&period=10&delay=1")
>                         .process(new Processor() {
>                             @Override
>                             public void process(Exchange exchange) throws Exception {
>                                 exchange.getMessage().setBody(body);
>                                 exchange.getMessage().setHeader("#CustomHeader", resolveTemplateNoCache(simpleTemplate, exchange));
>                             }
>                         })
>                         .to("mock:result");
>                 from("timer://test-timer2?fixedRate=true&period=10&delay=1")
>                         .process(new Processor() {
>                             @Override
>                             public void process(Exchange exchange) throws Exception {
>                                 exchange.getMessage().setBody(body);
>                                 exchange.getMessage().setHeader("#CustomHeader", resolveTemplateNoCache(simpleTemplate, exchange));
>                             }
>                         })
>                         .to("mock:result");
>                 from("timer://test-timer3?fixedRate=true&period=10&delay=1")
>                         .process(new Processor() {
>                             @Override
>                             public void process(Exchange exchange) throws Exception {
>                                 exchange.getMessage().setBody(body);
>                                 exchange.getMessage().setHeader("#CustomHeader", resolveTemplate(simpleTemplate, exchange));
>                             }
>                         })
>                         .to("mock:fail");
>                 from("timer://test-timer4?fixedRate=true&period=10&delay=1")
>                         .process(new Processor() {
>                             @Override
>                             public void process(Exchange exchange) throws Exception {
>                                 exchange.getMessage().setBody(body);
>                                 exchange.getMessage().setHeader("#CustomHeader", resolveTemplate(simpleTemplate, exchange));
>                             }
>                         })
>                         .to("mock:fail");
>                 from("timer://test-timer5?fixedRate=true&period=10&delay=1")
>                         .process(new Processor() {
>                             @Override
>                             public void process(Exchange exchange) throws Exception {
>                                 exchange.getMessage().setBody(body);
>                                 exchange.getMessage().setHeader("#CustomHeader", resolveTemplate(simpleTemplate, exchange));
>                             }
>                         })
>                         .to("mock:fail");
>             }
>         };
>     }
>     public String resolveTemplate(String template, Exchange exchange) {
>         var simpleExpression = new SimpleExpression();
>         simpleExpression.setExpression(template);
>         return simpleExpression.evaluate(exchange, String.class);
>     }
>     public String resolveTemplateNoCache(String template, Exchange exchange) {
>         var simpleExpression = new SimpleExpression();
>         //This will force to create a new entry in the cache in the SimpleLanguage class
>         String nocache = String.join("-", "-nocache", UUID.randomUUID().toString());
>         simpleExpression.setExpression("%s%s".formatted(template, nocache));
>         return simpleExpression.evaluate(exchange, String.class).replace(nocache, "");
>     }
> }
>  {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)