How to copy an exchange property to the global options?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to copy an exchange property to the global options?

Ron Cecchini
I'm trying to pass back the # of things I've processed in a route back to the calling Java.

This log() displays the correct number:

.log(LoggingLevel.INFO, "Processing ${body.size} Things ...")

I then tried putting that ${body.size} in a GlobalOption on the CamelContext so I can access it later:

.process(exchange -> exchange.getContext().getGlobalOptions().put("NumThings", simple("${body.size}").toString()) )

However, the above, and a few variations of the simple(), causes this:

System.out.println(String.format("*** Processed %s Things", camContext.getGlobalOption("NumThings")));

to gives this:

*** Processed Simple: ${body.size} Things

I just can't figure out how to do the evaluations and get that value in the GlobalOptions.  (I googled and googled...)

Or maybe there's a better way to store it in the CamelContext?

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: How to copy an exchange property to the global options?

Ron Cecchini
p.s.  While I'm at it, is there a way to *access* a GlobalOption from inside a route using Simple?

> On May 1, 2020 at 12:24 AM Ron Cecchini <[hidden email]> wrote:
>
>
> I'm trying to pass back the # of things I've processed in a route back to the calling Java.
>
> This log() displays the correct number:
>
> .log(LoggingLevel.INFO, "Processing ${body.size} Things ...")
>
> I then tried putting that ${body.size} in a GlobalOption on the CamelContext so I can access it later:
>
> .process(exchange -> exchange.getContext().getGlobalOptions().put("NumThings", simple("${body.size}").toString()) )
>
> However, the above, and a few variations of the simple(), causes this:
>
> System.out.println(String.format("*** Processed %s Things", camContext.getGlobalOption("NumThings")));
>
> to gives this:
>
> *** Processed Simple: ${body.size} Things
>
> I just can't figure out how to do the evaluations and get that value in the GlobalOptions.  (I googled and googled...)
>
> Or maybe there's a better way to store it in the CamelContext?
>
> Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: How to copy an exchange property to the global options?

Claus Ibsen-2
In reply to this post by Ron Cecchini
Global options are not for storing arbitrary end user stuff really.
Its for some global options for Camel itself for some logging max
sizes, and some other bits.
That said the options are stored as key/values as-is and not evaluated
during routing of an exchange as they are not intended for that.

But if you want to do it anyway, then your process method can store
the body size as a value instead of a simple text.

Something ala .put("xxx", exchange.getMessage.getBody.size)


On Fri, May 1, 2020 at 6:24 AM Ron Cecchini <[hidden email]> wrote:

>
> I'm trying to pass back the # of things I've processed in a route back to the calling Java.
>
> This log() displays the correct number:
>
> .log(LoggingLevel.INFO, "Processing ${body.size} Things ...")
>
> I then tried putting that ${body.size} in a GlobalOption on the CamelContext so I can access it later:
>
> .process(exchange -> exchange.getContext().getGlobalOptions().put("NumThings", simple("${body.size}").toString()) )
>
> However, the above, and a few variations of the simple(), causes this:
>
> System.out.println(String.format("*** Processed %s Things", camContext.getGlobalOption("NumThings")));
>
> to gives this:
>
> *** Processed Simple: ${body.size} Things
>
> I just can't figure out how to do the evaluations and get that value in the GlobalOptions.  (I googled and googled...)
>
> Or maybe there's a better way to store it in the CamelContext?
>
> Thanks.



--
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2
Reply | Threaded
Open this post in threaded view
|

Re: How to copy an exchange property to the global options?

Ron Cecchini
Thanks, Claus.

I should've mentioned that the body is a list of objects...

So unfortunately I can't use exchange.getMessage().getBody().toString().length() as it's returning the full text size of everything in the list.

I really didn't want to do any string manipulations in the processor... but since there was an easy way to do it, I couldn't resist and did something like the following, which now works perfectly:

    .process(new Processor() {
        public void process(Exchange e) throws Exception {
            int num = StringUtils.countMatches(e.getMessage().getBody().toString(), "CLASSNAME");
            e.getContext().getGlobalOptions().put("NumThings", Integer.toString(num));
        }
    })

BTW, if GlobalOptions isn't recommended, is there a better way to hang a value on the CamelContext so the caller can see it?

Thanks again.

> On May 1, 2020 at 1:04 AM Claus Ibsen <[hidden email]> wrote:
>
>
> Global options are not for storing arbitrary end user stuff really.
> Its for some global options for Camel itself for some logging max
> sizes, and some other bits.
> That said the options are stored as key/values as-is and not evaluated
> during routing of an exchange as they are not intended for that.
>
> But if you want to do it anyway, then your process method can store
> the body size as a value instead of a simple text.
>
> Something ala .put("xxx", exchange.getMessage.getBody.size)
>
>
> On Fri, May 1, 2020 at 6:24 AM Ron Cecchini <[hidden email]> wrote:
> >
> > I'm trying to pass back the # of things I've processed in a route back to the calling Java.
> >
> > This log() displays the correct number:
> >
> > .log(LoggingLevel.INFO, "Processing ${body.size} Things ...")
> >
> > I then tried putting that ${body.size} in a GlobalOption on the CamelContext so I can access it later:
> >
> > .process(exchange -> exchange.getContext().getGlobalOptions().put("NumThings", simple("${body.size}").toString()) )
> >
> > However, the above, and a few variations of the simple(), causes this:
> >
> > System.out.println(String.format("*** Processed %s Things", camContext.getGlobalOption("NumThings")));
> >
> > to gives this:
> >
> > *** Processed Simple: ${body.size} Things
> >
> > I just can't figure out how to do the evaluations and get that value in the GlobalOptions.  (I googled and googled...)
> >
> > Or maybe there's a better way to store it in the CamelContext?
> >
> > Thanks.
>
>
>
> --
> Claus Ibsen
> -----------------
> http://davsclaus.com @davsclaus
> Camel in Action 2: https://www.manning.com/ibsen2
Reply | Threaded
Open this post in threaded view
|

Re: How to copy an exchange property to the global options?

Claus Ibsen-2
On Fri, May 1, 2020 at 5:41 PM Ron Cecchini <[hidden email]> wrote:

>
> Thanks, Claus.
>
> I should've mentioned that the body is a list of objects...
>
> So unfortunately I can't use exchange.getMessage().getBody().toString().length() as it's returning the full text size of everything in the list.
>
> I really didn't want to do any string manipulations in the processor... but since there was an easy way to do it, I couldn't resist and did something like the following, which now works perfectly:
>
>     .process(new Processor() {
>         public void process(Exchange e) throws Exception {
>             int num = StringUtils.countMatches(e.getMessage().getBody().toString(), "CLASSNAME");
>             e.getContext().getGlobalOptions().put("NumThings", Integer.toString(num));
>         }
>     })
>
> BTW, if GlobalOptions isn't recommended, is there a better way to hang a value on the CamelContext so the caller can see it?
>

No not on camel context, you can register something in the registry
and use that.

> Thanks again.
>
> > On May 1, 2020 at 1:04 AM Claus Ibsen <[hidden email]> wrote:
> >
> >
> > Global options are not for storing arbitrary end user stuff really.
> > Its for some global options for Camel itself for some logging max
> > sizes, and some other bits.
> > That said the options are stored as key/values as-is and not evaluated
> > during routing of an exchange as they are not intended for that.
> >
> > But if you want to do it anyway, then your process method can store
> > the body size as a value instead of a simple text.
> >
> > Something ala .put("xxx", exchange.getMessage.getBody.size)
> >
> >
> > On Fri, May 1, 2020 at 6:24 AM Ron Cecchini <[hidden email]> wrote:
> > >
> > > I'm trying to pass back the # of things I've processed in a route back to the calling Java.
> > >
> > > This log() displays the correct number:
> > >
> > > .log(LoggingLevel.INFO, "Processing ${body.size} Things ...")
> > >
> > > I then tried putting that ${body.size} in a GlobalOption on the CamelContext so I can access it later:
> > >
> > > .process(exchange -> exchange.getContext().getGlobalOptions().put("NumThings", simple("${body.size}").toString()) )
> > >
> > > However, the above, and a few variations of the simple(), causes this:
> > >
> > > System.out.println(String.format("*** Processed %s Things", camContext.getGlobalOption("NumThings")));
> > >
> > > to gives this:
> > >
> > > *** Processed Simple: ${body.size} Things
> > >
> > > I just can't figure out how to do the evaluations and get that value in the GlobalOptions.  (I googled and googled...)
> > >
> > > Or maybe there's a better way to store it in the CamelContext?
> > >
> > > Thanks.
> >
> >
> >
> > --
> > Claus Ibsen
> > -----------------
> > http://davsclaus.com @davsclaus
> > Camel in Action 2: https://www.manning.com/ibsen2



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