You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Aaron Dixon <at...@gmail.com> on 2009/06/15 22:34:06 UTC

using a guice-injected service in a created thread

I'm using Guice component injection with Wicket and it works grreat:

MyPanel {

    @Inject
    private MyService myService;

    MyPanel(String id) {
        super(id);
        myService.doSomething();
    }

    //...
}

HOWEVER, now I'm tryin' to send my service to a thread that I create, like
so:

MyPanel {

    @Inject
    private MyService myService;

    MyPanel(String id) {
        super(id);
        myService.doSomething();
    }

    void onEvent() {
        Thread t = new MyThread(myService) {

            // ...uses passed-in myService in run() method

        }
    }
}

BUT of course Guice is proxying my service and it doesn't expect it in a
non-Wicket thread. At runtime I get:

org.apache.wicket.WicketRuntimeException: There is no application attached
to current thread pool-1-thread-1
    at org.apache.wicket.Application.get(Application.java:166)
    at
org.apache.wicket.guice.GuiceProxyTargetLocator.locateProxyTarget(GuiceProxyTargetLocator.java:48)
    at
org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:316)
    at
WICKET_com.conducive.data.dao.MyService$$EnhancerByCGLIB$$6fc4e9bf.getManagedSession(<generated>)
    at
com.conducive.ui.userPages.monitor.result.MyThread.run(MonitorRetrieverTask.java:33)
    at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619)

This makes sense to me why this is happening (Guice/Wicket need a Wicket
context to deserialize the instance).

SO, I guess this means I have to pull-and-set those services on the thread
myself...unless there is a better way?

Re: using a guice-injected service in a created thread

Posted by Igor Vaynberg <ig...@gmail.com>.
also you can look it up from the servlet context (if that is where you keep it)

-igor

On Mon, Jun 15, 2009 at 3:06 PM, Aaron Dixon<at...@gmail.com> wrote:
> But then I need an injector in that context...I suppose the suggestion is to
> inject the injector into MyPanel?
>
>    @Inject
>    Injector injector;
>
> It looks funny, but probably cleaner than pulling the services.
>
> On Mon, Jun 15, 2009 at 3:56 PM, Igor Vaynberg <ig...@gmail.com>wrote:
>
>> why not simply have MyThread injected by guice...
>>
>> Thread t=injector.getinstance(MyThread.class);
>>
>> -igor
>>
>> On Mon, Jun 15, 2009 at 1:34 PM, Aaron Dixon<at...@gmail.com> wrote:
>> > I'm using Guice component injection with Wicket and it works grreat:
>> >
>> > MyPanel {
>> >
>> >    @Inject
>> >    private MyService myService;
>> >
>> >    MyPanel(String id) {
>> >        super(id);
>> >        myService.doSomething();
>> >    }
>> >
>> >    //...
>> > }
>> >
>> > HOWEVER, now I'm tryin' to send my service to a thread that I create,
>> like
>> > so:
>> >
>> > MyPanel {
>> >
>> >    @Inject
>> >    private MyService myService;
>> >
>> >    MyPanel(String id) {
>> >        super(id);
>> >        myService.doSomething();
>> >    }
>> >
>> >    void onEvent() {
>> >        Thread t = new MyThread(myService) {
>> >
>> >            // ...uses passed-in myService in run() method
>> >
>> >        }
>> >    }
>> > }
>> >
>> > BUT of course Guice is proxying my service and it doesn't expect it in a
>> > non-Wicket thread. At runtime I get:
>> >
>> > org.apache.wicket.WicketRuntimeException: There is no application
>> attached
>> > to current thread pool-1-thread-1
>> >    at org.apache.wicket.Application.get(Application.java:166)
>> >    at
>> >
>> org.apache.wicket.guice.GuiceProxyTargetLocator.locateProxyTarget(GuiceProxyTargetLocator.java:48)
>> >    at
>> >
>> org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:316)
>> >    at
>> >
>> WICKET_com.conducive.data.dao.MyService$$EnhancerByCGLIB$$6fc4e9bf.getManagedSession(<generated>)
>> >    at
>> >
>> com.conducive.ui.userPages.monitor.result.MyThread.run(MonitorRetrieverTask.java:33)
>> >    at
>> > java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
>> >    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
>> >    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
>> >    at
>> >
>> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
>> >    at
>> >
>> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
>> >    at java.lang.Thread.run(Thread.java:619)
>> >
>> > This makes sense to me why this is happening (Guice/Wicket need a Wicket
>> > context to deserialize the instance).
>> >
>> > SO, I guess this means I have to pull-and-set those services on the
>> thread
>> > myself...unless there is a better way?
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>

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


Re: using a guice-injected service in a created thread

Posted by Aaron Dixon <at...@gmail.com>.
But then I need an injector in that context...I suppose the suggestion is to
inject the injector into MyPanel?

    @Inject
    Injector injector;

It looks funny, but probably cleaner than pulling the services.

On Mon, Jun 15, 2009 at 3:56 PM, Igor Vaynberg <ig...@gmail.com>wrote:

> why not simply have MyThread injected by guice...
>
> Thread t=injector.getinstance(MyThread.class);
>
> -igor
>
> On Mon, Jun 15, 2009 at 1:34 PM, Aaron Dixon<at...@gmail.com> wrote:
> > I'm using Guice component injection with Wicket and it works grreat:
> >
> > MyPanel {
> >
> >    @Inject
> >    private MyService myService;
> >
> >    MyPanel(String id) {
> >        super(id);
> >        myService.doSomething();
> >    }
> >
> >    //...
> > }
> >
> > HOWEVER, now I'm tryin' to send my service to a thread that I create,
> like
> > so:
> >
> > MyPanel {
> >
> >    @Inject
> >    private MyService myService;
> >
> >    MyPanel(String id) {
> >        super(id);
> >        myService.doSomething();
> >    }
> >
> >    void onEvent() {
> >        Thread t = new MyThread(myService) {
> >
> >            // ...uses passed-in myService in run() method
> >
> >        }
> >    }
> > }
> >
> > BUT of course Guice is proxying my service and it doesn't expect it in a
> > non-Wicket thread. At runtime I get:
> >
> > org.apache.wicket.WicketRuntimeException: There is no application
> attached
> > to current thread pool-1-thread-1
> >    at org.apache.wicket.Application.get(Application.java:166)
> >    at
> >
> org.apache.wicket.guice.GuiceProxyTargetLocator.locateProxyTarget(GuiceProxyTargetLocator.java:48)
> >    at
> >
> org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:316)
> >    at
> >
> WICKET_com.conducive.data.dao.MyService$$EnhancerByCGLIB$$6fc4e9bf.getManagedSession(<generated>)
> >    at
> >
> com.conducive.ui.userPages.monitor.result.MyThread.run(MonitorRetrieverTask.java:33)
> >    at
> > java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
> >    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
> >    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
> >    at
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
> >    at
> >
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
> >    at java.lang.Thread.run(Thread.java:619)
> >
> > This makes sense to me why this is happening (Guice/Wicket need a Wicket
> > context to deserialize the instance).
> >
> > SO, I guess this means I have to pull-and-set those services on the
> thread
> > myself...unless there is a better way?
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: using a guice-injected service in a created thread

Posted by Igor Vaynberg <ig...@gmail.com>.
why not simply have MyThread injected by guice...

Thread t=injector.getinstance(MyThread.class);

-igor

On Mon, Jun 15, 2009 at 1:34 PM, Aaron Dixon<at...@gmail.com> wrote:
> I'm using Guice component injection with Wicket and it works grreat:
>
> MyPanel {
>
>    @Inject
>    private MyService myService;
>
>    MyPanel(String id) {
>        super(id);
>        myService.doSomething();
>    }
>
>    //...
> }
>
> HOWEVER, now I'm tryin' to send my service to a thread that I create, like
> so:
>
> MyPanel {
>
>    @Inject
>    private MyService myService;
>
>    MyPanel(String id) {
>        super(id);
>        myService.doSomething();
>    }
>
>    void onEvent() {
>        Thread t = new MyThread(myService) {
>
>            // ...uses passed-in myService in run() method
>
>        }
>    }
> }
>
> BUT of course Guice is proxying my service and it doesn't expect it in a
> non-Wicket thread. At runtime I get:
>
> org.apache.wicket.WicketRuntimeException: There is no application attached
> to current thread pool-1-thread-1
>    at org.apache.wicket.Application.get(Application.java:166)
>    at
> org.apache.wicket.guice.GuiceProxyTargetLocator.locateProxyTarget(GuiceProxyTargetLocator.java:48)
>    at
> org.apache.wicket.proxy.LazyInitProxyFactory$CGLibInterceptor.intercept(LazyInitProxyFactory.java:316)
>    at
> WICKET_com.conducive.data.dao.MyService$$EnhancerByCGLIB$$6fc4e9bf.getManagedSession(<generated>)
>    at
> com.conducive.ui.userPages.monitor.result.MyThread.run(MonitorRetrieverTask.java:33)
>    at
> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
>    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
>    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
>    at
> java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
>    at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
>    at java.lang.Thread.run(Thread.java:619)
>
> This makes sense to me why this is happening (Guice/Wicket need a Wicket
> context to deserialize the instance).
>
> SO, I guess this means I have to pull-and-set those services on the thread
> myself...unless there is a better way?
>

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