You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by John Logan <Jo...@texture.com> on 2017/03/17 15:39:11 UTC

Question about performing background processing triggered by a request.

Hi,

I was wondering whether it's possible for a resource request to
invoke a service that performs background processing, such that
the worker thread spawned by the service has a resource
resolver and JCR session based on the authentication info for
the requesting user.

I tried using ResourceResolverFactory.getResourceResolver()
along with the AuthenticationInfo object from the request
attributes, and this yielded a LoginException.

Invoking requestedResource.getResourceResolver.clone(authInfo)
from inside the worker thread seems like an incorrect
approach due to race conditions.

Is there a way to create a resourceResolver for the user in
this scenario, or should I really be looking to the service
authentication stuff for this?

Thanks!  John

RE: Question about performing background processing triggered by a request.

Posted by Olaf <ol...@x100.de>.
Hi John!

Are you aware of the Sling Background Servlets Engine (see
https://sling.apache.org/downloads.cgi /
https://issues.apache.org/jira/browse/SLING-550)?
There is little documentation on this, but IMO it might be what you need or
at least provide a good blueprint.

Kind regards,
Olaf

-----Original Message-----
From: John Logan [mailto:John.Logan@texture.com] 
Sent: Freitag, 17. März 2017 16:39
To: users@sling.apache.org
Subject: Question about performing background processing triggered by a
request.

Hi,

I was wondering whether it's possible for a resource request to invoke a
service that performs background processing, such that the worker thread
spawned by the service has a resource resolver and JCR session based on the
authentication info for the requesting user.

I tried using ResourceResolverFactory.getResourceResolver()
along with the AuthenticationInfo object from the request attributes, and
this yielded a LoginException.

Invoking requestedResource.getResourceResolver.clone(authInfo)
from inside the worker thread seems like an incorrect approach due to race
conditions.

Is there a way to create a resourceResolver for the user in this scenario,
or should I really be looking to the service authentication stuff for this?

Thanks!  John


Re: Question about performing background processing triggered by a request.

Posted by John Logan <Jo...@texture.com>.
Thanks, Stefan.  I did wind up getting things working with getServiceResourceResolver(); setting up the service user mapping turned out to be quite easy.

For the background work I'm just using the Sling ThreadPoolManager service to make a thread pool for now, though I had looked at [1] and the job support may be useful later.

John

On Monday, March 20, 2017 1:23 AM, Stefan Seifert <ss...@pro-vision.de> wrote:
> 
> for background jobs you should have a look at [1].
> 
> if you have code running in the background without a requrest you
> have to create a resourceresolver instance yourself, and close it 
> when you're done.
> 
> you should always use getServiceResourceResolver.

[snip]

> [1]  https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html


RE: Question about performing background processing triggered by a request.

Posted by Stefan Seifert <ss...@pro-vision.de>.
for background jobs you should have a look at [1].

if you have code running in the background without a requrest you have to create a resourceresolver instance yourself, and close it when you're done.

you should always use getServiceResourceResolver. additionally you have to create a service user and a osgi mapping that maps your bundle to this use. the ACLs of this service user control which content the bundle can accces. see [2].

this is a bit complicated to setup, but you have a save setup as the code does not run with "admin" privileges.

stefan


[1] https://sling.apache.org/documentation/bundles/apache-sling-eventing-and-job-handling.html
[2] https://sling.apache.org/documentation/the-sling-engine/service-authentication.html



>-----Original Message-----
>From: John Logan [mailto:John.Logan@texture.com]
>Sent: Friday, March 17, 2017 4:39 PM
>To: users@sling.apache.org
>Subject: Question about performing background processing triggered by a
>request.
>
>Hi,
>
>I was wondering whether it's possible for a resource request to
>invoke a service that performs background processing, such that
>the worker thread spawned by the service has a resource
>resolver and JCR session based on the authentication info for
>the requesting user.
>
>I tried using ResourceResolverFactory.getResourceResolver()
>along with the AuthenticationInfo object from the request
>attributes, and this yielded a LoginException.
>
>Invoking requestedResource.getResourceResolver.clone(authInfo)
>from inside the worker thread seems like an incorrect
>approach due to race conditions.
>
>Is there a way to create a resourceResolver for the user in
>this scenario, or should I really be looking to the service
>authentication stuff for this?
>
>Thanks!  John


Re: Question about performing background processing triggered by a request.

Posted by John Logan <Jo...@texture.com>.
It appears that the LoginException was because I was
mistakenly invoking the resolver factory
getServiceResourceResolver() method instead of
using getResourceResolver() with the request authInfo.

With this change I now don't see a LoginException, but
I get a JCR AccessDeniedException when the thread
tries to perform the first write.

Is this approach feasible, and I just need to see if there's
an issue with users or permissions?


From: John Logan
On Friday, March 17, 2017 8:39 AM, John Logan wrote:

> I tried using ResourceResolverFactory.getResourceResolver()
> along with the AuthenticationInfo object from the request
> attributes, and this yielded a LoginException.