It doesn't work. The token will be fetched locally. Even I log out.
Sessions expire after a certain amount of time - 60 minutes by default.
The only way to determine whether the session is still valid is to make a request and see whether it succeeds.
The Service.getToken() method that was mentioned by others will give you the current session token that is being used but it won't tell you whether it is valid or not. The Service object doesn't actually know whether it is still valid until a new request is made. Even after a timeout occurs the existing token isn't changed (or blanked out) until Server.login() is called again.
If you need to make an explicit check, something like the following should work:
service.getServiceInfo().getServerName(); // throws SplunkException if session invalid
However note that the session may time out by the time you send the next request. Therefore such an explicit check cannot reliably tell you whether a subsequent request will succeed.
A more robust solution is to harden any code that talks to Splunk to handle session timeouts, relogin if necessary, and reperform the request.
Could you please give me an example of this?
The only way to determine whether the session is still valid is to make a request and see whether it succeeds.
Nothing much on documentation, but by description of the method, worth a try (gut feeling it might work)
I didn't try it. But I will try it now. Do they have documentation for the token?
Could you try using service.getToken method. I believe if session is expired, it should return null (or throw exception). Haven't verified it.