- 08 Nov 2024
- 2 Minutes to read
- Print
- DarkLight
Server Startup
- Updated on 08 Nov 2024
- 2 Minutes to read
- Print
- DarkLight
Purpose
Allows you to execute specific tasks automatically after the server restart. For example, you can define tasks such as automatically open a quote with several line items to retrieve and cache data from externally integrated systems. This reduces manual work after server restart.
Customers can tailor the Groovy script to suit their business needs, such as initializing data caches, preloading data, or running pre-defined queries to ensure that necessary information is readily available upon server restart.
Setup
Upload the script for Server Startup Script.
For details about uploading and configuring Groovy scripts, read Upload a Groovy script.
Binding variables
This extension point has access to the common binding variables only.
Sample script
Use the following sample Groovy script to upload for this extension point:
import java.nio.charset.*
import org.apache.http.*
import com.imc.iss.groovy.*
import org.apache.http.entity.*
import org.apache.http.util.*
import org.apache.http.entity.mime.*
import org.apache.http.entity.mime.content.*
import org.apache.http.client.methods.*
import org.apache.http.impl.client.*
import org.apache.http.client.entity.*
import org.apache.http.params.*
import javax.net.ssl.*
import org.apache.http.message.*
import org.json.simple.*
import javax.json.*
import com.imc.vocabulary.Schema
import static Constants.*
class Constants
{
// URLs
static final String auth_url_pf = '/oauth/token'
static final String quotes = '/quotes/'
static final String externalPricing = '/externalPricing'
static final String annotations = '/annotations/'
}
class Globals
{
String base_url, token, error
}
Globals globals = new Globals()
groovyLogger.logError("Groovy Execution Started.................");
CloseableHttpClient httpclient = new DefaultHttpClient()
try
{
for(int i =0;i<3;i++){
groovyLogger.logError("Thread....................... " + i);
Thread.sleep(3 * 1000)
}
globals.token = getToken(httpclient, globals)
String result = openQuote(httpclient, globals)
//Access Lookup table
def records = dbLookupTable.table('TABLE_TEST').addSelectFields('ID','NAME').search()
for(rec in records)
{
groovyLogger.logError("RECORDS ----------------------- " + rec)
}
//Access Lookup table that does not exisit.
//This particular execution fail but does not affect the application availability
def dummy = dbLookupTable.table('DUMMY_TABLE').addSelectFields('abc').search()
}
catch (Exception e)
{
throw e
}
finally
{
httpclient.close() // close Client
}
String openQuote(CloseableHttpClient httpclient, Globals globals)
{
//String response
String quoteUri = "<Quote_URI>"
String quoteID = quoteUri.bytes.encodeBase64().toString()
String url = globals.base_url + quotes + quoteID + "/salesItemInstanceTree"
HttpPost request = new HttpPost(url)
setRequestHeader(request, globals.token)
//String userUri = rec.USER_TO_NOTIFY
payload = "{ \"type\": \"Quote\", \"quoteElements\" : {\"bda\": [\"objectName\", \"objectId\", \"objectExternalId\"], \"bra\": [] } ,\"salesItemElements\": {\"bda\": [\"objectName\",\"salesItemPosition\"],\"bra\": [\"hasUnitofMeasure\",\"hasProductStatus\"] } } "
entity = new StringEntity(payload, ContentType.APPLICATION_FORM_URLENCODED)
request.setEntity(entity)
HttpResponse response
try
{
response = httpclient.execute(request)
String responseCode = response.getStatusLine().getStatusCode().toString()
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader (response.getEntity().getContent()))
groovyLogger.notifyWarning('Response Code: ' + responseCode)
groovyLogger.logError('Quote : ' + json.toString())
if(responseCode != '200')
{
// Error
globals.error = responseCode + ' message: ' + json
ok = false
}
} catch (Exception e)
{
throw e
}finally
{
response.close()
}
return "ok"
}
String getToken(CloseableHttpClient httpclient, Globals globals)
{
String token
globals.base_url = "http://<base_url>/api/"
String url = globals.base_url + auth_url_pf
HttpPost request = new HttpPost(url)
request.setHeader('Content-Type', 'application/x-www-form-urlencoded')
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2)
params.add(new BasicNameValuePair('base_url', 'http://<base_url>/api/'))
params.add(new BasicNameValuePair('grant_type', 'password'))
params.add(new BasicNameValuePair('username', '<username>'))
params.add(new BasicNameValuePair('password', '<password>'))
params.add(new BasicNameValuePair('client_id', 'mx_client'))
request.setEntity(new UrlEncodedFormEntity(params, 'UTF-8'))
// execute
HttpResponse response = httpclient.execute(request)
try
{
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader (response.getEntity().getContent()))
token = json.values().iterator().next()
} catch (Exception e)
{
throw e
} finally
{
response.close()
}
return token
}
void setRequestHeader(def request, String token)
{
request.setHeader('Authorization', 'Bearer ' + token)
request.setHeader('Accept','application/json; charset=utf-8')
request.setHeader('Accept-Encoding', 'gzip;q=0,deflate;q=0') // !!! q=0 - othrwise response comes gzip encoded and cannot be pased to jsonobject
request.setHeader('Content-Type', 'application/json')
}
Expected output
No output expected.
Script execution
A script that you upload for this extension point is run automatically at the server restart. The script execution does not impact the server restart. The server is up and running irrespective of whether the script executes the defined tasks successfully or fails to execute one or more tasks. You can check the status of executed tasks in the log.