You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Piotr Kaczmarski <Pi...@altkomsoftware.pl> on 2019/01/30 17:33:39 UTC

Dynamic velocity template parsing - MBean problem

Hello.

I'm trying to parse a velocity template using contentCache. My 
requirement is to search for the template in database before searching 
in resources.

If the template is found in the DB it should go into the content cache. 
If not, it should be read from resource file and then inserted into the 
cache. Then it should be parsed.

I've found a way to do it, it's shown in this mini project:

https://github.com/halfdan87/camel-velocity-engine-test


Generally I'm using beans method to load the template:

.to("velocity:bean:myRoute.testMethod('templateName.vm')")

where myRoute is a bean with this testMethod:

     public String testMethod(String template) {
         log.debug("Body in test method: {}", template);

         // here i'll search for the template and return it

     }

It works, but that's not the end of the story:) Other requirement is 
that when we insert or update a template in the DB, we want to clear the 
contentCache.

So we did that with this piece of code:

                     MBeanServer mbeanServer = 
camelContext.getManagementStrategy().getManagementAgent().getMBeanServer();
                     Set<ObjectName> objNameSet = 
mbeanServer.queryNames(new 
ObjectName("org.apache.camel:type=endpoints,name=\"velocity*\",*"), null);
                     ObjectName managedObjName = new 
ArrayList<>(objNameSet).get(0);
                     mbeanServer.invoke(managedObjName, 
"clearContentCache", null, null);
                     Set<ObjectName> objNameSet = 
mbeanServer.queryNames(new 
ObjectName("org.apache.camel:type=*,name=\"*\",*"), null);

                     objNameSet.forEach(objectName -> {
                         try {
                             mbeanServer.invoke(objectName, 
"clearContentCache", null, null);
                         } catch (Exception e) {
                             log.info("fail");
                         }
                     });

It still works!

Then I wanted to change the code so that the template could be 
dynamically set from body or header:

.to("velocity:bean:myRoute.testMethod('${body['template']}')")

Now that didn't work. the method always received null :(

So I tried to use recipientList to create a dynamic endpoint:

.recipientList().simple("velocity:bean:myRoute.testMethod(${body['template']})")

And that worked again!

But then our method that cleared the cache stopped working.

Ho can we get this working?

As I said, the test cases are in the mini-project here: 
https://github.com/halfdan87/camel-velocity-engine-test

To use it, just run:

mvn spring-boot:run

It should run every second and show two examples, the one with .to() ant 
the one with .recipientList().


Best regards,

Piotr Kaczmarski










Re: Dynamic velocity template parsing - MBean problem

Posted by Piotr Kaczmarski <Pi...@altkomsoftware.pl>.
I did that:

     public String testMethod(@Simple("${body['template']}") String 
template) {
         log.debug("Body in test method: {}", template);
         return "test template: " + template;
     }


I made two tests, because i'm not sure how to use this:

         .process(exchange -> {
exchange.getIn().setBody(Collections.singletonMap("template", 
"template.vm"));
         })
         .to("velocity:bean:myRoute.testMethod('template')")
         .log(LoggingLevel.INFO, "Body: ${body}")

         .process(exchange -> {
exchange.getIn().setBody(Collections.singletonMap("template", 
"template.vm"));
         })
         .to("velocity:bean:myRoute.testMethod")
         .log(LoggingLevel.INFO, "Body: ${body}")


The first one just inserts what I statically wrote in the endpoint call:

Body: test template: template

The second one just has null in the parameter:

Body: test template: null

Like the body was not used or emptyed.

I've updated the project.


W dniu 30.01.2019 o 19:38, Claus Ibsen pisze:
> Hi
>
> Add the dynamic to the bean parameter signature so your velocity
> endpoint uri is static
>
>   public String testMethod(@Simple("${body['template']}'") String template) {
>
>
> On Wed, Jan 30, 2019 at 6:33 PM Piotr Kaczmarski
> <Pi...@altkomsoftware.pl> wrote:
>> Hello.
>>
>> I'm trying to parse a velocity template using contentCache. My
>> requirement is to search for the template in database before searching
>> in resources.
>>
>> If the template is found in the DB it should go into the content cache.
>> If not, it should be read from resource file and then inserted into the
>> cache. Then it should be parsed.
>>
>> I've found a way to do it, it's shown in this mini project:
>>
>> https://github.com/halfdan87/camel-velocity-engine-test
>>
>>
>> Generally I'm using beans method to load the template:
>>
>> .to("velocity:bean:myRoute.testMethod('templateName.vm')")
>>
>> where myRoute is a bean with this testMethod:
>>
>>       public String testMethod(String template) {
>>           log.debug("Body in test method: {}", template);
>>
>>           // here i'll search for the template and return it
>>
>>       }
>>
>> It works, but that's not the end of the story:) Other requirement is
>> that when we insert or update a template in the DB, we want to clear the
>> contentCache.
>>
>> So we did that with this piece of code:
>>
>>                       MBeanServer mbeanServer =
>> camelContext.getManagementStrategy().getManagementAgent().getMBeanServer();
>>                       Set<ObjectName> objNameSet =
>> mbeanServer.queryNames(new
>> ObjectName("org.apache.camel:type=endpoints,name=\"velocity*\",*"), null);
>>                       ObjectName managedObjName = new
>> ArrayList<>(objNameSet).get(0);
>>                       mbeanServer.invoke(managedObjName,
>> "clearContentCache", null, null);
>>                       Set<ObjectName> objNameSet =
>> mbeanServer.queryNames(new
>> ObjectName("org.apache.camel:type=*,name=\"*\",*"), null);
>>
>>                       objNameSet.forEach(objectName -> {
>>                           try {
>>                               mbeanServer.invoke(objectName,
>> "clearContentCache", null, null);
>>                           } catch (Exception e) {
>>                               log.info("fail");
>>                           }
>>                       });
>>
>> It still works!
>>
>> Then I wanted to change the code so that the template could be
>> dynamically set from body or header:
>>
>> .to("velocity:bean:myRoute.testMethod('${body['template']}')")
>>
>> Now that didn't work. the method always received null :(
>>
>> So I tried to use recipientList to create a dynamic endpoint:
>>
>> .recipientList().simple("velocity:bean:myRoute.testMethod(${body['template']})")
>>
>> And that worked again!
>>
>> But then our method that cleared the cache stopped working.
>>
>> Ho can we get this working?
>>
>> As I said, the test cases are in the mini-project here:
>> https://github.com/halfdan87/camel-velocity-engine-test
>>
>> To use it, just run:
>>
>> mvn spring-boot:run
>>
>> It should run every second and show two examples, the one with .to() ant
>> the one with .recipientList().
>>
>>
>> Best regards,
>>
>> Piotr Kaczmarski
>>
>>
>>
>>
>>
>>
>>
>>
>>
>

Re: Dynamic velocity template parsing - MBean problem

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Add the dynamic to the bean parameter signature so your velocity
endpoint uri is static

 public String testMethod(@Simple("${body['template']}'") String template) {


On Wed, Jan 30, 2019 at 6:33 PM Piotr Kaczmarski
<Pi...@altkomsoftware.pl> wrote:
>
> Hello.
>
> I'm trying to parse a velocity template using contentCache. My
> requirement is to search for the template in database before searching
> in resources.
>
> If the template is found in the DB it should go into the content cache.
> If not, it should be read from resource file and then inserted into the
> cache. Then it should be parsed.
>
> I've found a way to do it, it's shown in this mini project:
>
> https://github.com/halfdan87/camel-velocity-engine-test
>
>
> Generally I'm using beans method to load the template:
>
> .to("velocity:bean:myRoute.testMethod('templateName.vm')")
>
> where myRoute is a bean with this testMethod:
>
>      public String testMethod(String template) {
>          log.debug("Body in test method: {}", template);
>
>          // here i'll search for the template and return it
>
>      }
>
> It works, but that's not the end of the story:) Other requirement is
> that when we insert or update a template in the DB, we want to clear the
> contentCache.
>
> So we did that with this piece of code:
>
>                      MBeanServer mbeanServer =
> camelContext.getManagementStrategy().getManagementAgent().getMBeanServer();
>                      Set<ObjectName> objNameSet =
> mbeanServer.queryNames(new
> ObjectName("org.apache.camel:type=endpoints,name=\"velocity*\",*"), null);
>                      ObjectName managedObjName = new
> ArrayList<>(objNameSet).get(0);
>                      mbeanServer.invoke(managedObjName,
> "clearContentCache", null, null);
>                      Set<ObjectName> objNameSet =
> mbeanServer.queryNames(new
> ObjectName("org.apache.camel:type=*,name=\"*\",*"), null);
>
>                      objNameSet.forEach(objectName -> {
>                          try {
>                              mbeanServer.invoke(objectName,
> "clearContentCache", null, null);
>                          } catch (Exception e) {
>                              log.info("fail");
>                          }
>                      });
>
> It still works!
>
> Then I wanted to change the code so that the template could be
> dynamically set from body or header:
>
> .to("velocity:bean:myRoute.testMethod('${body['template']}')")
>
> Now that didn't work. the method always received null :(
>
> So I tried to use recipientList to create a dynamic endpoint:
>
> .recipientList().simple("velocity:bean:myRoute.testMethod(${body['template']})")
>
> And that worked again!
>
> But then our method that cleared the cache stopped working.
>
> Ho can we get this working?
>
> As I said, the test cases are in the mini-project here:
> https://github.com/halfdan87/camel-velocity-engine-test
>
> To use it, just run:
>
> mvn spring-boot:run
>
> It should run every second and show two examples, the one with .to() ant
> the one with .recipientList().
>
>
> Best regards,
>
> Piotr Kaczmarski
>
>
>
>
>
>
>
>
>


-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2