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

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

{code:java}
// code placeholder
{code}
Hi [~rhuanrocha] ,

Thanks a lot for your time, yep it's the same for 4.0.0-M1.

Let me share with you a quick summary after a conversation here:
[https://camel.zulipchat.com/#narrow/stream/257298-camel/topic/Simple.20language]

This is going to happen due the fact that I'm going to create and invoke the 
evaluation of the expression inside a processor, and it look like it's not the 
standard way to it.
The standard way to it's to use the simple expression by the dsl, with 
something like that:
{code:java}
 from("timer://test-timer5?fixedRate=true&period=10&delay=1")
                    .setBody(constant(body))
                    
.setHeader("#CustomHeader",simple(simpleTemplate,String.class))
                    .to("mock:fail"); {code}
and not in this way (directly inside a processor):
{code:java}
 public String resolveTemplate(String template, Exchange exchange) {
        var simpleExpression = new SimpleExpression();
        simpleExpression.setExpression(template);
        return simpleExpression.evaluate(exchange, String.class);

    } {code}
due the fact that's not thread safe.

We will see what will be decided for this ticket.
To me, by the user point of view, with your fix the user will have more 
advantages like avoid to write multiple time something like


{code:java}
....
.process(MyProcessor)
.xxxxx(.....,simple(xxxxxx))
.process
.(.....,simple(xxxxxx)){code}
I mean multiple evaluation that at the end could be done in the processor 
itself directly.
So we will see what will be decided around this.
Thanks again and have a greet weekend.

> 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, 3.20.1, 3.20.2, 4.0-M1
>            Reporter: Andrea Evangelista
>            Assignee: Rhuan Rocha
>            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)

Reply via email to