# Instance of ResourceWatcherService

**URL:** <https://discuss.elastic.co/t/instance-of-resourcewatcherservice/130285>\
**Category:** Elasticsearch\
**Created:** [May 2, 2018, 3:14pm UTC](https://discuss.elastic.co/t/instance-of-resourcewatcherservice/130285 "2018-05-02T15:14:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tobsucht](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tobsucht/32/29265_2.png) [@Tobsucht](https://discuss.elastic.co/u/Tobsucht)\
**Post date:** [May 2, 2018, 3:14pm UTC](https://discuss.elastic.co/t/instance-of-resourcewatcherservice/130285/1 "2018-05-02T15:14:33Z")

</div>

I dont' get it: How do I get an instance of the ResourceWatcherService without using the @Inject annotation? I'm trying to implement updateable search-time synonyms and need an instance for the service. How can I obtain a reference before getAnalyzers() within the Plugin class is called, so that I can pass it to one of my custom analyzers.

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [May 2, 2018, 10:00pm UTC](https://discuss.elastic.co/t/instance-of-resourcewatcherservice/130285/2 "2018-05-02T22:00:24Z")

</div>

You can override the `createComponents` method on Plugin to get this. I recommend creating a service object which will handle the updates, and your synonym token filter and use the currently parsed synonyms from your service (letting the service update these in the background through the resource watcher). So you would have something like this:

```auto

private SetOnce<YourSynonymService> synonymService = new SetOnce<>();

@Override
public Collection<Object> createComponents(..., ResourceWatcherService resourceWatcherService, ...) {
    synonymService.set(new YourSynonymService(resourceWatcherService));
    return Collections.singleton(synonymService.get());
}

@Override  
public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
    // note the SetOnce is passed here because analysis is initialized before createComponents is called
    return Collections.singletonMap("my-synonym-filter", new MyTokenFilterProvider(synonymService));
}

```

---

<div class="post-metadata">

**Author:** ![Tobsucht](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tobsucht/32/29265_2.png) [@Tobsucht](https://discuss.elastic.co/u/Tobsucht)\
**Post date:** [May 3, 2018, 8:08am UTC](https://discuss.elastic.co/t/instance-of-resourcewatcherservice/130285/3 "2018-05-03T08:08:17Z")

</div>

Well, that saved me a lot of time. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 31, 2018, 8:08am UTC](https://discuss.elastic.co/t/instance-of-resourcewatcherservice/130285/4 "2018-05-31T08:08:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
