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

Raymond commented on CAMEL-23652:
---------------------------------

Note that with variable and the extended Simple language it's easy to do this 
without beans:


{code:java}
template:
  from:
    uri: timer:counter
    parameters:
      period: "{{period}}"
      repeatCount: "{{?numbers}}"
    steps:
      - choice:
          when:
            - simple: "${variable.route:counter} == null"
              steps:
                - setVariable:
                    name: route:counter
                    simple:
                      expression: "{{start}}"
                      resultType: java.lang.Integer
          otherwise:
            steps:
              - setVariable:
                  name: route:counter
                  simple:
                    expression: "${variable.route:counter}++"
                    resultType: java.lang.Integer
      - setBody:
          simple: "${variable.route:counter}"
      - setHeader:
          name: "Content-Type"
          constant: "text/plain" {code}

Note 2:

The code in note 1 works flawlessly, however I first did it like this:


{code:java}
template:
  from:
    uri: timer:counter
    parameters:
      period: "{{period}}"
      repeatCount: "{{?numbers}}"
    steps:
      - choice:
          when:
            - simple: "${variable.route:counter} == null"
              steps:
                - setVariable:
                    name: route:counter
                    simple:
                      expression: "{{start}}"
                      resultType: java.lang.Integer
          otherwise:
            steps:
              - setVariable:
                  name: route:counter
                  simple:
                    expression: "${variable.route:counter} + 1"
                    resultType: java.lang.Integer
      - setBody:
          simple: "${variable.route:counter}"
      - setHeader:
          name: "Content-Type"
          constant: "text/plain"
      - to: "{{out}}" {code}

I forgot that + and - etc, aren't operators in Simple (only used to concat 
values). So you can't do like Java or Groovy:


{code:java}
 int x = 3;
 System.out.println(x + 3);  {code}
Maybe when all parameters are int than you could use it as an operator like 
Java.




 

> [kamelet] Bean is not registered
> --------------------------------
>
>                 Key: CAMEL-23652
>                 URL: https://issues.apache.org/jira/browse/CAMEL-23652
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-kamelet
>    Affects Versions: 4.20.0
>            Reporter: Raymond
>            Assignee: Claus Ibsen
>            Priority: Minor
>             Fix For: 4.21.0
>
>         Attachments: routetemplate-xml.zip
>
>
> I like to use the following Kamelet:
> [https://github.com/apache/camel-kamelets/blob/main/kamelets/counter-source.kamelet.yaml]
> I slightly altered the Kamelet route to this:
> {code:java}
> template:
>   beans:
>     - name: counter
>       type: java.util.concurrent.atomic.AtomicInteger
>       constructors:
>         "0": "{{start}}"
>   from:
>     uri: timer:counter
>     parameters:
>       period: "{{period}}"
>       repeatCount: "{{?numbers}}"
>     steps:
>       - bean:
>           ref: "{{counter}}"
>           method: getAndIncrement
>       - setHeader:
>           name: "Content-Type"
>           constant: "text/plain"
>       - to: "{{out}}" {code}
> However when I call this the bean is not recognized, and a property is 
> expected. This is the error I see:
> {code:java}
> Failed to create route: 
> 69b3f52efbdb4c0011000147-7cee7a5b-d34c-450e-a8d0-02972b6ad07c at: >>> 
> Bean[ref:{{counter}} method:getAndIncrement] <<< in route: 
> Route(69b3f52efbdb4c0011000147-7cee7a5b-d34c-450e-a8d0-02972... because: 
> Property with key [counter] not found in properties from text: {{counter}}" 
> {code}
> I loaded the Kamelets like this:
> {code:java}
> public void setRouteTemplates() {
>     List<String> resourceNames = getKamelets();
>     RoutesLoader routesLoader = PluginHelper.getRoutesLoader(context);
>     for (String resourceName : resourceNames) {
>         Resource resource = 
> ResourceHelper.resolveResource(context,"classpath:kamelets/" + resourceName);
>         try {
>             routesLoader.loadRoutes(resource);
>         } catch (Exception e) {
>             log.warn("Could not load Kamelet: {}. Reason: {}", resourceName, 
> e.getMessage());
>         }
>     }
> } {code}
> I have around 200 Kamelets, and they load and function correctly. The only 
> thing that have issues is with the dynamically registered beans that locally 
> scoped with the Kamelet. Registering the bean programmatically, and then 
> referencing it (both hardcoded or with a property) works fine. 
> Is this because they are loaded throught the routesLoader (both the Kamelet, 
> as the templatedRoute)? Is this bug or I am missing something?



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

Reply via email to