I have a CustomRealm class which handles the custom realm logic to authenticate. I have a static cache so that it's only 1 per jvm, which has a value - setExpireAfterAccess(10). I want this value to come from the yml file to keep it configurable. The config to the CustomRealm is passed on initialization, is there any other way the elasticsearch.yml config can be read in a static way only once so that I can initialize my cache on load and not with every instance? The other way I can think of is to actually create a singleton.
Call to CustomRealm class from CustomRealmFactory @Override
public CustomRealm create(RealmConfig config) {
return new CustomRealm(config);
}
Cache in CustomRealm class private static final Cache<String, String> cache = CacheBuilder.<String, String>builder().setExpireAfterAccess(TimeValue.timeValueMinutes(<value_from_yml>)).build();
I think the only way a custom realm would get access to settings is via RealmConfig. I assume in your use case you want to share this cache with other custom realm instances, if that is the case then I think you are right to have cache inside a class and make it singleton.
Thanks @Yogesh_Gaikwad. When you say other custom realm instances, do you mean multiple instances being created for a CustomRealmType? I have just one CustomRealmType and since many instances are being created on the JVM by the factory class with multiple requests coming in, I want all these to share a common cache by making it static(seems like I cannot do this since I need to fetch config from the yml to initialize the cache which happens via the constructor) to lookup certain keys before making outbound calls.
The strange thing is I tested with a local elastic cluster on my windows machine without declaring it static and it seemed to be working when I was making multiple API requests from a REST client.
Thanks @TimV, that explains why it's working on my local environment.
But a couple questions for my understanding
Though we instantiate a class, it always returns the one Singleton Realm instance? On first look, I always thought this creates a new object every time.
Let's say I initialize my cache by fetching a setting from the config in the constructor shown above(cache=CacheBuilder.build()), does it happen only once since it's a Singleton though we call the constructor every time?
I am going to add loggers to test this out and see how it actually works.
PS - All my assumptions above may be wrong if indeed the framework just creates one instance of this class upon the first request and returns that one Singleton instance until teardown.
Your factory method is called exactly once, when the node is initialised (more or less during startup, but it depends on how your cluster formation works).
The result of that factory is a single Realm instance that is stored and used for all authentication requests.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.