You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Christopher Dodunski <Ch...@christopher.net.nz> on 2019/04/07 06:16:55 UTC

Injecting one service into another 'ignored path' service

Hi team,

I've implemented a WebSocket server endpoint inside a Tapestry application
by placing its class inside the 'services' folder, and adding its path to
'contributeIgnoredPathsFilter()' as below.

    public static void contributeIgnoredPathsFilter(Configuration<String>
configuration) {
        configuration.add("/websocket/.*");
    }

Tomcat successfully connects client apps calling this web socket URI. 
Having the server endpoint as a Tapestry service ought to work well,
allowing me to PUSH messages out to connected clients.  So far so good. 
The slight hiccup is that this server endpoint (service) depends on
another service, which I've tried injecting into a field of the endpoint
class:

    @InjectService("VesselStateTracker")
    private VesselStateTracker vesselStateTracker;

The problem is that when Tomcat invokes this endpoint class, the injected
service is not instantiated (makes sense, as Tapestry is configured to
ignore calls to the endpoint).  So a null pointer exception occurs as soon
as the endpoint tries to use 'vesselStateTracker' internally.

In AppModule I've tried eagerly loading both services after binding them,
and making the VesselStateTracker field a static field inside the
endpoint, but this wasn't successful.

Is there a simple solution or work-around to this almost expected problem?

Thanks & regards,

Chris.


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


Re: Injecting one service into another 'ignored path' service

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Sun, Apr 7, 2019 at 4:03 AM Christopher Dodunski <
ChrisFromTapestry@christopher.net.nz> wrote:

> Hi team,
>

Hello!


> The slight hiccup is that this server endpoint (service) depends on
> another service, which I've tried injecting into a field of the endpoint
> class:
>
>     @InjectService("VesselStateTracker")
>

Why not just @Inject? Do you have more than one service implementing
VesselStateTracker?


>     private VesselStateTracker vesselStateTracker;
>
> The problem is that when Tomcat invokes this endpoint class, the injected
> service is not instantiated (makes sense, as Tapestry is configured to
> ignore calls to the endpoint). So a null pointer exception occurs as soon
> as the endpoint tries to use 'vesselStateTracker' internally.


I'm afraid you're wrong about why the problem is happening. Tapestry-IoC is
the one actually taking care of services and it knows nothing about ignored
paths, which are a Tapestry(-core, the webapp) thing. The problem is that
your endpoint doesn't seem to be a Tapestry-IoC service itself, so it
doesn't get any dependency injection done to it.


> In AppModule I've tried eagerly loading both services after binding them,
> and making the VesselStateTracker field a static field inside the
> endpoint, but this wasn't successful.
>
> Is there a simple solution or work-around to this almost expected problem?
>

I'd try, in your endpoint, to get the Tapestry-IoC registry object from the
ServletContext using the TapestryFilter.REGISTRY_CONTEXT_NAME
(org.apache.tapestry5.application-registry) key, then call
registry.getService(VesselStateTracker.class) on it.


>
> Thanks & regards,
>
> Chris.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

-- 
Thiago