You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Peter Henderson <pe...@starjar.com> on 2015/05/28 11:51:34 UTC

Render a component in a background thread.

Hi

I am trying to render a component in a background thread.

My first attempt fails with an Exception
"There is no application attached to current thread Thread-4"



So I link the application with the background thread and now a different
exception.
java.lang.IllegalArgumentException: Argument 'requestCycle' may not be null.
    at org.apache.wicket.util.lang.Args.notNull(Args.java:41)
    at
org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1568)
    at org.apache.wicket.Session.get(Session.java:171)


Is component renderer designed to work from a background thread?


I've put a quick start on git hub.
https://github.com/bollinger/wicket-ComponentRenderer




-- 
Peter Henderson

Re: Render a component in a background thread.

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

I was going to say that you can use ComponentRenderer for this but looking
at its source [1] I see that we create temporary RequestCycle only for
#renderPage() but not for #renderComponent().
Also there is no way to use Application#get(name).
Please file a ticket for improvement.

In the meantime you can roll your own class by reusing code.


Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Thu, May 28, 2015 at 12:51 PM, Peter Henderson <
peter.henderson@starjar.com> wrote:

> Hi
>
> I am trying to render a component in a background thread.
>
> My first attempt fails with an Exception
> "There is no application attached to current thread Thread-4"
>
>
>
> So I link the application with the background thread and now a different
> exception.
> java.lang.IllegalArgumentException: Argument 'requestCycle' may not be
> null.
>     at org.apache.wicket.util.lang.Args.notNull(Args.java:41)
>     at
>
> org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1568)
>     at org.apache.wicket.Session.get(Session.java:171)
>
>
> Is component renderer designed to work from a background thread?
>
>
> I've put a quick start on git hub.
> https://github.com/bollinger/wicket-ComponentRenderer
>
>
>
>
> --
> Peter Henderson
>

Re: Render a component in a background thread.

Posted by Don Ferguson <do...@gmail.com>.
From your description of the problem, it doesn’t really seem like you need a renderer — perhaps abstracting out the search criterion as a pure java (non-wicket-specific) object that can be used both in the UI and in data export would be a better approach.

That said, here’s what I’m doing to render pages (batch emails) in a background thread:
public static void sendMail(EmailBatch batch) {
    Application app = ThreadContext.getApplication();
    RequestCycle requestCycle = ThreadContext.getRequestCycle();
    Session session = ThreadContext.getSession();

    Runnable r = () -> {
        ThreadContext.setApplication(app);
        ThreadContext.setRequestCycle(requestCycle);
        ThreadContext.setSession(session);
<blah blah blah>
	ComponentRenderer.renderPage(new PageProvider(EmailMergeProcessor.class, pp)

There are some restrictions on what the pages can contain (for example, I had to disable bootstrap), but eventually I got the above to work.  Your mileage may vary.  Good luck.

-Don



> On May 29, 2015, at 1:49 AM, Peter Henderson <pe...@starjar.com> wrote:
> 
> On Fri, May 29, 2015 at 9:22 AM, Christoph Läubrich <laeubi@googlemail.com <ma...@googlemail.com>>
> wrote:
> 
>> Can you explain what is the reason for using a background-thread?
>> Depending on your container you might want to use e.g. Continuations or
>> Servlet-Async to compute lengthy values in the background and then let
>> render the page as normal in the RequestCycle depending on that result.
>> 
>> 
> In my real application the background thread is a data export routine. I
> want to run a list panel with saved page parameters and instead of
> rendering the html I'll access the underlying list model to extract the
> data, export to csv (or json,etc) and pass it out for other processing.
> This way the user can fiddle about with the search filters on the list to
> see exactly what they want and later get the raw data (accountants love
> spreadsheets).
> 
> For example.
> This page [1] I'd like to have a plain servlet which can run the component
> outside of wicket.
> 
> 
> Peter.
> 
> 
> 
> [1]
> https://demo.starjar.com:25000/Starjar/protected/project/1210005?rpp=25&fn1=Description+Contains&fn2=Date+%5Bmonth+year%5D&fv2=2015+1+1&tab=Time+Sheets <https://demo.starjar.com:25000/Starjar/protected/project/1210005?rpp=25&fn1=Description+Contains&fn2=Date+%5Bmonth+year%5D&fv2=2015+1+1&tab=Time+Sheets>
> 
> 
> 
>> Am 28.05.2015 11:51, schrieb Peter Henderson:
>> 
>>> Hi
>>> 
>>> I am trying to render a component in a background thread.
>>> 
>>> My first attempt fails with an Exception
>>> "There is no application attached to current thread Thread-4"
>>> 
>>> 
>>> 
>>> So I link the application with the background thread and now a different
>>> exception.
>>> java.lang.IllegalArgumentException: Argument 'requestCycle' may not be
>>> null.
>>>     at org.apache.wicket.util.lang.Args.notNull(Args.java:41)
>>>     at
>>> 
>>> org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1568)
>>>     at org.apache.wicket.Session.get(Session.java:171)
>>> 
>>> 
>>> Is component renderer designed to work from a background thread?
>>> 
>>> 
>>> I've put a quick start on git hub.
>>> https://github.com/bollinger/wicket-ComponentRenderer
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>> 
>> 
> 
> 
> -- 
> Peter Henderson
> 
> Director
> Starjar Ltd.
> www.starjar.com <http://www.starjar.com/>
> 0330 088 1662


Re: Render a component in a background thread.

Posted by Peter Henderson <pe...@starjar.com>.
On Fri, May 29, 2015 at 9:22 AM, Christoph Läubrich <la...@googlemail.com>
wrote:

> Can you explain what is the reason for using a background-thread?
> Depending on your container you might want to use e.g. Continuations or
> Servlet-Async to compute lengthy values in the background and then let
> render the page as normal in the RequestCycle depending on that result.
>
>
In my real application the background thread is a data export routine. I
want to run a list panel with saved page parameters and instead of
rendering the html I'll access the underlying list model to extract the
data, export to csv (or json,etc) and pass it out for other processing.
This way the user can fiddle about with the search filters on the list to
see exactly what they want and later get the raw data (accountants love
spreadsheets).

For example.
This page [1] I'd like to have a plain servlet which can run the component
outside of wicket.


Peter.



[1]
https://demo.starjar.com:25000/Starjar/protected/project/1210005?rpp=25&fn1=Description+Contains&fn2=Date+%5Bmonth+year%5D&fv2=2015+1+1&tab=Time+Sheets



> Am 28.05.2015 11:51, schrieb Peter Henderson:
>
>> Hi
>>
>> I am trying to render a component in a background thread.
>>
>> My first attempt fails with an Exception
>> "There is no application attached to current thread Thread-4"
>>
>>
>>
>> So I link the application with the background thread and now a different
>> exception.
>> java.lang.IllegalArgumentException: Argument 'requestCycle' may not be
>> null.
>>      at org.apache.wicket.util.lang.Args.notNull(Args.java:41)
>>      at
>>
>> org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1568)
>>      at org.apache.wicket.Session.get(Session.java:171)
>>
>>
>> Is component renderer designed to work from a background thread?
>>
>>
>> I've put a quick start on git hub.
>> https://github.com/bollinger/wicket-ComponentRenderer
>>
>>
>>
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Peter Henderson

Director
Starjar Ltd.
www.starjar.com
0330 088 1662

Re: Render a component in a background thread.

Posted by Christoph Läubrich <la...@googlemail.com>.
Can you explain what is the reason for using a background-thread? 
Depending on your container you might want to use e.g. Continuations or 
Servlet-Async to compute lengthy values in the background and then let 
render the page as normal in the RequestCycle depending on that result.

Am 28.05.2015 11:51, schrieb Peter Henderson:
> Hi
>
> I am trying to render a component in a background thread.
>
> My first attempt fails with an Exception
> "There is no application attached to current thread Thread-4"
>
>
>
> So I link the application with the background thread and now a different
> exception.
> java.lang.IllegalArgumentException: Argument 'requestCycle' may not be null.
>      at org.apache.wicket.util.lang.Args.notNull(Args.java:41)
>      at
> org.apache.wicket.Application.fetchCreateAndSetSession(Application.java:1568)
>      at org.apache.wicket.Session.get(Session.java:171)
>
>
> Is component renderer designed to work from a background thread?
>
>
> I've put a quick start on git hub.
> https://github.com/bollinger/wicket-ComponentRenderer
>
>
>
>
>    


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org