CompletableFuture and HstRequestContext

24 views
Skip to first unread message

Kah Tang

unread,
May 23, 2018, 5:17:12 AM5/23/18
to Hippo Community
I'm having some trouble concerning CompletableFuture and HstRequestContext. 
I have a Component that creates a new CompletableFuture in the prepareBeforeRender() method and sets it as an attribute in the requestContext, so it can be executed in the doBeforeRender().
This CompletableFuture is supplied with a task that needs access to HippoRequestContextService to get a certain request parameter.

The problem however is that HippoRequestContextService needs access to the HstRequestContext to retrieve this parameter. But it seems that when you try to execute the CompletableFuture it will give you an error, because HippoRequestContextService cannot access HstRequestContext.

What is the best way to solve this?

Thanks in advance

Marijan Milicevic

unread,
May 23, 2018, 5:19:38 AM5/23/18
to hippo-c...@googlegroups.com
HstRequestContext is request based and you cannot  use it in background tasks. You'll need to retrieve that parameter upfront and pass it to CompletableFuture task,
cheers
marijan

Thanks in advance

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-community@googlegroups.com
RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-community+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/hippo-community.
For more options, visit https://groups.google.com/d/optout.

Michiel Rop

unread,
May 25, 2018, 2:56:18 AM5/25/18
to hippo-c...@googlegroups.com
Hi Kah,

Some more elaboration on Marijan's spot on answer. 

The HstRequestContext contains many parameters that are only valid during the request. The most important one is the jcr session, that will be used to retrieve most of the data of your component, is closed once the request completes. If you want to perform any background task, make sure to create ( and close ) a new session *inside* your background task.

It depends on your requirements, but in general a "Repository-Managed Component' or daemon module [1] is better suited to run longer running background task. They can be called from a component as well.


On Wed, May 23, 2018 at 11:19 AM Marijan Milicevic <marijan....@bloomreach.com> wrote:
On Wed, May 23, 2018 at 11:17 AM, 'Kah Tang' via Hippo Community <hippo-c...@googlegroups.com> wrote:
I'm having some trouble concerning CompletableFuture and HstRequestContext. 
I have a Component that creates a new CompletableFuture in the prepareBeforeRender() method and sets it as an attribute in the requestContext, so it can be executed in the doBeforeRender().
This CompletableFuture is supplied with a task that needs access to HippoRequestContextService to get a certain request parameter.

The problem however is that HippoRequestContextService needs access to the HstRequestContext to retrieve this parameter. But it seems that when you try to execute the CompletableFuture it will give you an error, because HippoRequestContextService cannot access HstRequestContext.

What is the best way to solve this?

HstRequestContext is request based and you cannot  use it in background tasks. You'll need to retrieve that parameter upfront and pass it to CompletableFuture task,
cheers
marijan

Thanks in advance

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com

RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.

--
Hippo Community Group: The place for all discussions and announcements about Hippo CMS (and HST, repository etc. etc.)
 
To post to this group, send email to hippo-c...@googlegroups.com

RSS: https://groups.google.com/group/hippo-community/feed/rss_v2_0_msgs.xml?num=50
---
You received this message because you are subscribed to the Google Groups "Hippo Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hippo-communi...@googlegroups.com.

Ard Schrijvers

unread,
May 25, 2018, 4:43:05 AM5/25/18
to hippo-c...@googlegroups.com
Hey,

On Fri, May 25, 2018 at 8:56 AM, Michiel Rop <michi...@bloomreach.com> wrote:
> Hi Kah,
>
> Some more elaboration on Marijan's spot on answer.
>
> The HstRequestContext contains many parameters that are only valid during
> the request. The most important one is the jcr session, that will be used to
> retrieve most of the data of your component, is closed once the request
> completes. If you want to perform any background task, make sure to create (
> and close ) a new session *inside* your background task.
>
> It depends on your requirements, but in general a "Repository-Managed
> Component' or daemon module [1] is better suited to run longer running
> background task. They can be called from a component as well.

This is most likely not what Kah is after, he most likely wants to
make use of parallel hst component preprocessing, see [1]. This is
typically useful when for example one or more component use an
external call to a rest service (or use CRISP). In general, JCR
related code you do not in the prepareBeforeRender phase: Namely, when
using a CompletableFuture, the work in the future is done with another
Thread, and a JCR session is not thread safe. You could use a
different JCR session, but this typically does not really improve your
performance. Instead, in prepareBeforeRender with a CompletableFuture
use things that take long and typically have a large i/o (like doing
an external micro service request over http).

Daemon modules serve a complete different purpose and run in the cms
webapp instead of the site delivery webapp, which is quite unlikely
what Kah is after.

Last thing: when using a CompletableFuture,
RequestContextProvider.get() won't return the HstRequestContext any
more since the RequestContextProvider uses Thread locals. Therefor, in
general, in your CompletableFuture you cannot use code from the HST
that relies on fetching the HstRequestContext via
RequestContextProvider.get().

HTH,

Regards Ard

[1] https://www.onehippo.org/library/concepts/component-development/parallel-hstcomponent-preprocessing.html
Hippo Netherlands, Oosteinde 11, 1017 WT Amsterdam, Netherlands
Hippo USA, Inc. 71 Summer Street, 2nd Floor Boston, MA 02110, United
states of America.

US +1 877 414 4776 (toll free)
Europe +31(0)20 522 4466
www.onehippo.com
Reply all
Reply to author
Forward
0 new messages