• 0

Place a file in cache or property source and read it in Groovy Script


RVKS
 Share

Question

Hello support team,

                   We are trying to place a JSON file in Cache or Property Source and trying to read the same.Can you please let us know how we can be able to achieve it? We are trying to load the class in Script using scriptCache = this.class.classLoader.loadClass("com.santaba.agent.util.script.ScriptCache").getCache(); but we are getting errors while doing the same.Can you please let us know,how we will be able to know the version of collector we are using ? Where can we find the same ?

                                                                                                                                                                Also,we are unable to read inputs from a JSON File present in the Property Source.Please assist us with the same.

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0
  • Administrators

Yes, you can write JSON content to a cached token since you can write any string object. I'm not sure what the character limit might be. 

def scriptCache
scriptCache = this.class.classLoader.loadClass("com.santaba.agent.util.script.ScriptCache").getCache();
String token = scriptCache.get("myjson");
if(token == null){ //the token is empty because it has not been set previously
  //generate the json and set it into a token
  def json = JsonOutput.toJson([name: 'John Doe', age: 42])
  assert json == '{"name":"John Doe","age":42}'
  scriptCache.set("myjson", json);
} else{ //the json has been stored in the cache previously, so let's use it
  println(token)
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share