JAX-WS and servlet integration with Guice

368 views
Skip to first unread message

Chris

unread,
Aug 29, 2009, 10:38:03 PM8/29/09
to google-guice
Is there a way to inject JAX-WS web services and servlets using the
same, single instance of my Guice module?

Say I have a web service:

@GuiceManaged(module=MyModule.class)
@WebService
class MyWebService {
}

and a GuiceServletContextListener

class MyGuiceServletContextListener extends
GuiceServletContextListener {
protected Injector getInjector() { return Guice.createInjector(new
MyModule()); }
}

My web services are injected using the guicemanaged.jar by annotating
the SIB class with a @GuiceManaged(module=MyModule.class) annotation
and my servlets are injected using
GuiceServletContextListener.getInjector() method and each instantiates
its own copy of MyModule.

Is there a way to have both the GuiceManaged and
GuiceServletContextListener use the same instance of MyModule? Can/
should I make MyModule a static class?

Stuart McCulloch

unread,
Aug 30, 2009, 6:24:03 AM8/30/09
to google...@googlegroups.com
2009/8/30 Chris <csto...@gmail.com>

just wondering why you need a single instance of your module? does it have some sort of state?
 

--
Cheers, Stuart

Chris Stockton

unread,
Aug 30, 2009, 10:38:27 AM8/30/09
to google...@googlegroups.com
No, there's no state in the module.  But if I have a class bound in Singleton scope in my module and the module is loaded twice, each injector will have a separate instance of the class.

Stuart McCulloch

unread,
Aug 30, 2009, 11:00:28 AM8/30/09
to google...@googlegroups.com
2009/8/30 Chris Stockton <csto...@gmail.com>
No, there's no state in the module.  But if I have a class bound in Singleton scope in my module and the module is loaded twice, each injector will have a separate instance of the class.

note that Guice singletons are per-injector, so you would get two instances
of the class even if you passed the same module instance into each injector:

   http://groups.google.com/group/google-guice/msg/9350be0b7d596795

( unless of course you bound the class using 'toInstance()' to an object that
  you created in the module, rather than binding it in the singleton scope )

imho it looks more like you need to use the same injector, rather than the
same module instance - is there any way you could get the injector from
JAX-WS and re-use that in the servlet context listener? (or vice-versa)

On Sun, Aug 30, 2009 at 3:24 AM, Stuart McCulloch <mcc...@gmail.com> wrote:
2009/8/30 Chris <csto...@gmail.com>

Is there a way to inject JAX-WS web services and servlets using the
same, single instance of my Guice module?

Say I have a web service:

@GuiceManaged(module=MyModule.class)
@WebService
class MyWebService {
}

and a GuiceServletContextListener

class MyGuiceServletContextListener extends
GuiceServletContextListener {
   protected Injector getInjector() { return Guice.createInjector(new
MyModule()); }
}

My web services are injected using the guicemanaged.jar by annotating
the SIB class with a @GuiceManaged(module=MyModule.class) annotation
and my servlets are injected using
GuiceServletContextListener.getInjector() method and each instantiates
its own copy of MyModule.

Is there a way to have both the GuiceManaged and
GuiceServletContextListener use the same instance of MyModule?  Can/
should I make MyModule a static class?

just wondering why you need a single instance of your module? does it have some sort of state?
 

--
Cheers, Stuart









--
Cheers, Stuart

Marcus Eriksson

unread,
Aug 30, 2009, 12:27:57 PM8/30/09
to google...@googlegroups.com
Currently there is no way of getting the injector from jax-ws guicemanaged (or setting it)

The best fix is probably to pin the injector to the WebServiceContext/ServletContext like guice servlet is doing, I'll experiment with ways of exposing the injector and get back real soon

I'm also currently doing a better jax-ws - guice servlet integration (minimal web.xml and no sun-jaxws.xml), but i've run into some problems there and getting answers from the jax-ws authors is quite difficult. If anyone knows an answer to this question, i'd be happy:
http://forums.java.net/jive/thread.jspa?threadID=63821&tstart=15

--
Marcus Eriksson

Dhanji R. Prasanna

unread,
Aug 30, 2009, 6:49:38 PM8/30/09
to google...@googlegroups.com
IIRC you can specify it as part of the @WebService annotation. A lot of it is confounding black magic though, so different providers seem to have different defaults.

Dhanji.

Bahri Gençsoy

unread,
Dec 29, 2009, 9:32:37 AM12/29/09
to google...@googlegroups.com

I couldn't manage to implement a fix. I removed "module" from GuiceManaged annotation, and hard coded the module instance into GuiceManagedInstanceResolver. But when I tried to register the created injector to servlet context with this code:

Injector injector = Guice.createInjector(moduleInstances);
ServletContext servletContext = (ServletContext) webServiceContext.getMessageContext().
  get(MessageContext.SERVLET_CONTEXT);
servletContext.setAttribute(Injector.class.getName(), injector);

 it throws an IllegalStateException with "getMessageContext() can only be called while servicing a request".

I am not sure that I am on the correct path either. I'll try to retrieve the injector within the servlets' init methods and call injector.injectDependencies(this).


Currently there is no way of getting the injector from jax-ws
guicemanaged
(or setting it)

The best fix is probably to pin the injector to the
WebServiceContext/ServletContext like guice servlet is doing, I'll
experiment with ways of exposing the injector and get back real soon

I'm also currently doing a better jax-ws - guice servlet integration
(minimal web.xml and no sun-jaxws.xml), but i've run into some
problems
there and getting answers from the jax-ws authors is quite difficult.
If
anyone knows an answer to this question, i'd be happy:http://
forums.java.net/jive/thread.jspa?threadID=63821&tstart=15

--
Marcus Eriksson

On Sun, Aug 30, 2009 at 5:00 PM, Stuart McCulloch <mccu...@gmail.com>
wrote:
> 2009/8/30 Chris Stockton <cstock...@gmail.com>


>> No, there's no state in the module.  But if I have a class bound in
>> Singleton scope in my module and the module is loaded twice, each injector
>> will have a separate instance of the class.

> note that Guice singletons are per-injector, so you would get two instances
> of the class even if you passed the same module instance into each
> injector:

>    http://groups.google.com/group/google-guice/msg/9350be0b7d596795

> ( unless of course you bound the class using 'toInstance()' to an object
> that
>   you created in the module, rather than binding it in the singleton scope
> )

> imho it looks more like you need to use the same injector, rather than the
> same module instance - is there any way you could get the injector from
> JAX-WS and re-use that in the servlet context listener? (or vice-versa)

> On Sun, Aug 30, 2009 at 3:24 AM, Stuart McCulloch <mccu...@gmail.com>wrote:

>>>  2009/8/30 Chris <cstock...@gmail.com>
Reply all
Reply to author
Forward
0 new messages