0
votes

I want to perform a simple task ! but dont know if it is possible or not!

I have a groovy class which implements Runnable and it has been running using an ThreadPool! what I want to do in my Runnable class is the following:

    public void run() {
          EventPhoto.withTransaction { status ->

                EventPhoto photo = new EventPhoto(event:eventInstance)

                photo.imageUrl = "${resource(dir:'images/uploads',file:image.name, absolute:true)}"
                photo.thumbnailUrl = "${resource(dir:'images/uploads',file:thumb.name, absolute:true)}"
          }
    }

The thing is, as my thread is not running inside the webrequest I am getting the following error:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

So Is there anyway to still use $resource() ??

thanks

2
Are you sure it's not the call to save() that's generating this erroe?Dónal
@Don yep! i tried the same code w/o ${resource()} and it works!Arthur Neves
I removed the call to save() from your post to avoid this confusionDónal

2 Answers

1
votes

Here is some information that will show you how to accomplish what you are trying to do. Also note that it advises that what you are doing is bad design.

0
votes

Your background thread does not, by default, have access to the Hibernate session used to persist your Photo. You can use a plugin like Executor to save domain objects in a background thread.