Date String Creation

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

Date String Creation

Castyn
I am curious what the best way is to create a date string that supports xsl:date format?

For instance if I wanted to create a date string for an hour ago in the format yyyy-MM-ddThh:mm:ss-offset what would the best way to go about it be?  I know there are some simple commands such as ${date:now:...}, or that it could possible be done through a javascript function that just outputs the string, but I am curious if there is a better way or how to implement the simple statement.
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Donald Whytock
Using basic Java, the best I can come up with offhand is

String outdate = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new
Date(System.currentTimeMillis()  - 3600000), new StringBuffer(), new
FieldPosition(0)).toString();

I assume there's simpler.

Don

On Tue, Dec 6, 2011 at 12:07 PM, Castyn <[hidden email]> wrote:

> I am curious what the best way is to create a date string that supports
> xsl:date format?
>
> For instance if I wanted to create a date string for an hour ago in the
> format yyyy-MM-ddThh:mm:ss-offset what would the best way to go about it be?
> I know there are some simple commands such as ${date:now:...}, or that it
> could possible be done through a javascript function that just outputs the
> string, but I am curious if there is a better way or how to implement the
> simple statement.
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5052753.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Castyn
Is there a way to do it simply through the spring DSL though?

I guess I could create a custom processor that just passes on the headers and body after adding a new header with this code in it, but that seems like overkill for this situation.
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Claus Ibsen-2
On Tue, Dec 6, 2011 at 11:57 PM, Castyn <[hidden email]> wrote:
> Is there a way to do it simply through the spring DSL though?
>

Not from Camel itself. You would need to use a scripting language. Or
create a java bean, that returns the date, and then use the
bean/method call expression from the Spring DSL.

> I guess I could create a custom processor that just passes on the headers
> and body after adding a new header with this code in it, but that seems like
> overkill for this situation.
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5053745.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Craig Taylor
Actually, one *ugly* way that springs to mind is to adjust the offset by -1
to remove an hour from the time..

I use a processor which sets a date property (CALC_DATE) which allows
passing in a number of values to manipulate the date ( round to date field,
add date field, add amount ) (concerned about more than just hours).

On Tue, Dec 6, 2011 at 11:31 PM, Claus Ibsen <[hidden email]> wrote:

> On Tue, Dec 6, 2011 at 11:57 PM, Castyn <[hidden email]> wrote:
> > Is there a way to do it simply through the spring DSL though?
> >
>
> Not from Camel itself. You would need to use a scripting language. Or
> create a java bean, that returns the date, and then use the
> bean/method call expression from the Spring DSL.
>
> > I guess I could create a custom processor that just passes on the headers
> > and body after adding a new header with this code in it, but that seems
> like
> > overkill for this situation.
> >
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5053745.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: [hidden email]
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>



--
-------------------------------------------
Craig Taylor
[hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Castyn
In reply to this post by Claus Ibsen-2
So if I declared a bean with id of getLastRunDate that returned a string of the appropriate date format, how do you actually call that in the Spring DSL to set a header?
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Claus Ibsen-2
On Wed, Dec 7, 2011 at 4:47 PM, Castyn <[hidden email]> wrote:
> So if I declared a bean with id of getLastRunDate that returned a string of
> the appropriate date format, how do you actually call that in the Spring DSL
> to set a header?
>

Use the method call expression, something a like this:

<setHeader headerName="myDate">
   <method ref="myBean" method="getLastRunDate"/>
</setHeader>



> --
> View this message in context: http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5056060.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Raul Kripalani-2
Hi,

If you don't mind using XQuery as an expression language for achieving
this, you could use the following instead of using a Bean. IMHO, this is
more natural, as it uses XML languages themselves to spit out an
XML-compliant dateTime format ;)

 <setHeader headerName="myDate">
    <xquery type="java.lang.String" xmlns:xs="
http://www.w3.org/2001/XMLSchema">xs:dateTime(current-dateTime()) -
xs:dayTimeDuration('P1D')</xquery>
  </setHeader>
Couple of things:

   - Providing the type is absolutely necessary (java.lang.String in the
   above example)
   - You need to specify the XML Schema namespace. If you want the
   expression XML element to be lighter, you could push up the namespace
   declaration to the <route>, <camelContext> element or above
   - You can construct the duration dynamically by using the concat()
   function.
   - Make sure that camel-saxon is on your classpath, or that the
   camel-saxon feature is installed if you are using ServiceMix/Karaf -
   whatever is relevant in your case.

Hope this helps.

Regards,
Raúl.

On 7 December 2011 16:00, Claus Ibsen <[hidden email]> wrote:

> On Wed, Dec 7, 2011 at 4:47 PM, Castyn <[hidden email]> wrote:
> > So if I declared a bean with id of getLastRunDate that returned a string
> of
> > the appropriate date format, how do you actually call that in the Spring
> DSL
> > to set a header?
> >
>
> Use the method call expression, something a like this:
>
> <setHeader headerName="myDate">
>   <method ref="myBean" method="getLastRunDate"/>
> </setHeader>
>
>
>
> > --
> > View this message in context:
> http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5056060.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: [hidden email]
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/
>
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Castyn
In reply to this post by Claus Ibsen-2
When using a method call expression, how do you use parameters to the bean methods within the method tag?
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Castyn
For example, lets say I had a value in a route that I could get with some simple xpath, but I needed to pass that as a parameter to a bean that would do some calculation on that value and return it, so that I could then set it as a header or such.  Is there a way this could be done without relying on a custom processor?

For instance, I know I can do the following to get a value from a bean method:
<setHeader headerName="lastRunDate">                                                                       
         <method ref="lastRun" method="getLastRunDate"/>
</setHeader>

But what if I want to have a bean that will take in a value thatis not the entire message body and do something with it?  Such as a value resolved from this expression:
<xpath resultType="java.lang.String">//s:randomInfo</xpath>


How can that be best accomplished?  I know a custom processor allows you full access to the headers and body, as would a bean with annotations of the headers and body, but it seems like overkill if all I would want to do is set and retrieve a value from a bean such as a singleton to be utilized by the route.
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Claus Ibsen-2
See the @XPath annotation you can use with Camel.
http://camel.apache.org/bean-binding.html
http://camel.apache.org/xpath.html



On Thu, Dec 8, 2011 at 6:55 AM, Castyn <[hidden email]> wrote:

> For example, lets say I had a value in a route that I could get with some
> simple xpath, but I needed to pass that as a parameter to a bean that would
> do some calculation on that value and return it, so that I could then set it
> as a header or such.  Is there a way this could be done without relying on a
> custom processor?
>
> For instance, I know I can do the following to get a value from a bean
> method:
> <setHeader headerName="lastRunDate">
>         <method ref="lastRun" method="getLastRunDate"/>
> </setHeader>
>
> But what if I want to have a bean that will take in a value thatis not the
> entire message body and do something with it?  Such as a value resolved from
> this expression:
> <xpath resultType="java.lang.String">//s:randomInfo</xpath>
>
>
> How can that be best accomplished?  I know a custom processor allows you
> full access to the headers and body, as would a bean with annotations of the
> headers and body, but it seems like overkill if all I would want to do is
> set and retrieve a value from a bean such as a singleton to be utilized by
> the route.
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5057766.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Claus Ibsen-2
In reply to this post by Castyn
On Thu, Dec 8, 2011 at 6:55 AM, Castyn <[hidden email]> wrote:

> For example, lets say I had a value in a route that I could get with some
> simple xpath, but I needed to pass that as a parameter to a bean that would
> do some calculation on that value and return it, so that I could then set it
> as a header or such.  Is there a way this could be done without relying on a
> custom processor?
>
> For instance, I know I can do the following to get a value from a bean
> method:
> <setHeader headerName="lastRunDate">
>         <method ref="lastRun" method="getLastRunDate"/>
> </setHeader>
>
> But what if I want to have a bean that will take in a value thatis not the
> entire message body and do something with it?  Such as a value resolved from
> this expression:
> <xpath resultType="java.lang.String">//s:randomInfo</xpath>
>
>
> How can that be best accomplished?  I know a custom processor allows you
> full access to the headers and body, as would a bean with annotations of the
> headers and body, but it seems like overkill if all I would want to do is
> set and retrieve a value from a bean such as a singleton to be utilized by
> the route.
>

Btw we talked some time ago about letting the method call expression
accept a list of expressions in the XML DSL,
so each expression maps to the given parameter index.

That way you can embed an xpath expression to map the parameter at
that index. And for other parameters you can use <simple> <constant>
etc.


> --
> View this message in context: http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5057766.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Claus Ibsen-2
On Thu, Dec 8, 2011 at 1:51 PM, Claus Ibsen <[hidden email]> wrote:

> On Thu, Dec 8, 2011 at 6:55 AM, Castyn <[hidden email]> wrote:
>> For example, lets say I had a value in a route that I could get with some
>> simple xpath, but I needed to pass that as a parameter to a bean that would
>> do some calculation on that value and return it, so that I could then set it
>> as a header or such.  Is there a way this could be done without relying on a
>> custom processor?
>>
>> For instance, I know I can do the following to get a value from a bean
>> method:
>> <setHeader headerName="lastRunDate">
>>         <method ref="lastRun" method="getLastRunDate"/>
>> </setHeader>
>>
>> But what if I want to have a bean that will take in a value thatis not the
>> entire message body and do something with it?  Such as a value resolved from
>> this expression:
>> <xpath resultType="java.lang.String">//s:randomInfo</xpath>
>>
>>
>> How can that be best accomplished?  I know a custom processor allows you
>> full access to the headers and body, as would a bean with annotations of the
>> headers and body, but it seems like overkill if all I would want to do is
>> set and retrieve a value from a bean such as a singleton to be utilized by
>> the route.
>>
>
> Btw we talked some time ago about letting the method call expression
> accept a list of expressions in the XML DSL,
> so each expression maps to the given parameter index.
>
> That way you can embed an xpath expression to map the parameter at
> that index. And for other parameters you can use <simple> <constant>
> etc.
>

I created a ticket to track this.
https://issues.apache.org/jira/browse/CAMEL-4753

>
>> --
>> View this message in context: http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5057766.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
>
> --
> Claus Ibsen
> -----------------
> FuseSource
> Email: [hidden email]
> Web: http://fusesource.com
> Twitter: davsclaus, fusenews
> Blog: http://davsclaus.blogspot.com/
> Author of Camel in Action: http://www.manning.com/ibsen/



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Raul Kripalani-2
In reply to this post by Castyn
Once again, if your messages are XML and you want to manipulate
dateTime types, I suggest you use the standard XPath/XQuery functions.
It's gonna be simpler and probably faster.

Both the XQuery example or the XPath language (with Saxon as a Factory
as support for XPath 2.0 is needed) would do the trick.

On 8 Dec 2011, at 05:56, Castyn <[hidden email]> wrote:

> For example, lets say I had a value in a route that I could get with some
> simple xpath, but I needed to pass that as a parameter to a bean that would
> do some calculation on that value and return it, so that I could then set it
> as a header or such.  Is there a way this could be done without relying on a
> custom processor?
>
> For instance, I know I can do the following to get a value from a bean
> method:
> <setHeader headerName="lastRunDate">
>    <method ref="lastRun" method="getLastRunDate"/>
> </setHeader>
>
> But what if I want to have a bean that will take in a value thatis not the
> entire message body and do something with it?  Such as a value resolved from
> this expression:
> <xpath resultType="java.lang.String">//s:randomInfo</xpath>
>
>
> How can that be best accomplished?  I know a custom processor allows you
> full access to the headers and body, as would a bean with annotations of the
> headers and body, but it seems like overkill if all I would want to do is
> set and retrieve a value from a bean such as a singleton to be utilized by
> the route.
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5057766.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Castyn
Raul Kripalani wrote
Once again, if your messages are XML and you want to manipulate
dateTime types, I suggest you use the standard XPath/XQuery functions.
It's gonna be simpler and probably faster.

Both the XQuery example or the XPath language (with Saxon as a Factory
as support for XPath 2.0 is needed) would do the trick.
Well, after thinking on what I actually need to do I don't think it will be that simple.  I need to track whenever the last time the route was run.  So basically the route would run such as

Timer -> get Last Successful Run Time -> Do More Route Stuff -> If successful, set Last Successful Run Time

At first I thought a simple delta on a run time would work, but given the possiblity for errors, needing the ability to set a default Last Run Time and such, I think it needs to be a bit more complex.  I have considered a singleton on the osgi container that would track the state a bit, but I am not sure that is the best option here.  Still trying to figure out what route I should go in order to manage this run date.
Reply | Threaded
Open this post in threaded view
|

Re: Date String Creation

Claus Ibsen-2
On Thu, Dec 8, 2011 at 3:28 PM, Castyn <[hidden email]> wrote:

>
> Raul Kripalani wrote
>>
>> Once again, if your messages are XML and you want to manipulate
>> dateTime types, I suggest you use the standard XPath/XQuery functions.
>> It's gonna be simpler and probably faster.
>>
>> Both the XQuery example or the XPath language (with Saxon as a Factory
>> as support for XPath 2.0 is needed) would do the trick.
>>
>>
>
> Well, after thinking on what I actually need to do I don't think it will be
> that simple.  I need to track whenever the last time the route was run.  So
> basically the route would run such as
>
> Timer -> get Last Successful Run Time -> Do More Route Stuff -> If
> successful, set Last Successful Run Time
>
> At first I thought a simple delta on a run time would work, but given the
> possiblity for errors, needing the ability to set a default Last Run Time
> and such, I think it needs to be a bit more complex.  I have considered a
> singleton on the osgi container that would track the state a bit, but I am
> not sure that is the best option here.  Still trying to figure out what
> route I should go in order to manage this run date.
>

Each Camel route is registered in JMX, where you got a number of JMX stats.
Of course this is only avail if JMX is not disabled (JMX is enabled by default).

You could then query the JMX for the route stats.

Just boot up a Camel app, and start jconsole, then you can connect to
the Camel app.
And then you can browse the JMX tree and see what details you have.


> --
> View this message in context: http://camel.465427.n5.nabble.com/Date-String-Creation-tp5052753p5058954.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



--
Claus Ibsen
-----------------
FuseSource
Email: [hidden email]
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/