Introduction:
The XSJob file enables you to run a service (for example, an XS JavaScript or a SQLScript) at a scheduled interval.
In this article, we will explain how to schedule a job that triggers an XS JavaScript application.
Prerequisites:
In this example we will create a job that triggers an XS JavaScript application that
Create Table:
Copy paste the below SQL script to create table.
--REPLACE <YOUR_SCHEMA> WITH YOUR SCHEMA NAME
CREATE COLUMN TABLE <YOUR_SCHEMA>.STOCK (
CREATED_AT TIMESTAMP PRIMARY KEY,
STOCK_VALUE FLOAT
)
Create XSJob to get SAP stock value and update in HANA table:
1. Create HANA XS Project as mentioned in Create Your First HANA XS Application using HANA Studio
Note: We highly recommend you that you create a package with the name “xsjob-tutorial” in Content and create the project inside it. Otherwise you will have to change the package reference in XSJS and XSJob files.
2. Create a package inside the project called “jobs” and create 3 files
The XSJob file enables you to run a service (for example, an XS JavaScript or a SQLScript) at a scheduled interval.
In this article, we will explain how to schedule a job that triggers an XS JavaScript application.
Prerequisites:
- You have access to a HANA system
- You have all the role and privileges required
- You have role sap.hana.xs.admin.roles::JobAdministrator and sap.hana.xs.admin.roles::HTTPDestAdministrator assigned to your user
- You know how to Create Your First HANA XS Application using HANA Studio
In this example we will create a job that triggers an XS JavaScript application that
- Reads the latest value of a share price from a public financial service available on the Internet.
- Update the stock value for SAP DE in HANA table with current timestamps.
Create Table:
Copy paste the below SQL script to create table.
--REPLACE <YOUR_SCHEMA> WITH YOUR SCHEMA NAME
CREATE COLUMN TABLE <YOUR_SCHEMA>.STOCK (
CREATED_AT TIMESTAMP PRIMARY KEY,
STOCK_VALUE FLOAT
)
Create XSJob to get SAP stock value and update in HANA table:
1. Create HANA XS Project as mentioned in Create Your First HANA XS Application using HANA Studio
Note: We highly recommend you that you create a package with the name “xsjob-tutorial” in Content and create the project inside it. Otherwise you will have to change the package reference in XSJS and XSJob files.
2. Create a package inside the project called “jobs” and create 3 files
- yahoo.xsjob // job schedule definition
- yahoo.xshttpdest // HTTP destination details
- yahoo.xsjs // Script to run on schedule
3. Open yahoo.xsjs file and paste below code.
function readStock(input) {
var stock = input.stock;
var dest = $.net.http.readDestination("xsjob-tutorial.jobs", "yahoo");
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.GET, "/d/quotes.csv?f=a&s=" + stock);
client.request(req, dest);
var response = client.getResponse();
var stockValue;
if (response.body){
stockValue = parseInt(response.body.asString(), 10);
}
var sql = "INSERT INTO SAP_HANA_TUTORIAL.STOCK VALUES (NOW(), ?)";
var conn = $.db.getConnection();
var pstmt = conn.prepareStatement(sql);
pstmt.setDouble(1, stockValue);
pstmt.execute();
conn.commit();
conn.close();
}
Note: You may have to change the package name in the line
$.net.http.readDestination("xsjob-tutorial.jobs", "yahoo");
4. Open the HTTP destination file yahoo.xshttpdest and paste below content.
host = "download.finance.yahoo.com";
port = 80;
description = "my stock-price checker";
useSSL = false;
pathPrefix = "/d/quotes.csv?f=a";
authType = none;
useProxy = false;
proxyHost = "";
proxyPort = 0;timeout = 0;
Note: In case you are behind a proxy, then change the proxy settings. For example:
useProxy = true;
proxyHost = "proxy";
proxyPort = 8080;
5. Open XS job file and paste below content
{
"description": "Read stock value",
"action": "xsjob-tutorial.jobs:yahoo.xsjs::readStock",
"schedules": [{
"description": "Read current stock value every 10 second",
"xscron": "* * * * * * 0:59/10",
"parameter": {
"stock": "SAP.DE"
}
}]
}
Note: You may have to change the package name in the line
"action": "xsjob-tutorial.jobs:yahoo.xsjs::readStock",
6.Save and activate all the files.
7.Open HANA Admin tool. The URL of Admin tool is:
http://<WebServerHost>:80<SAPHANAinstance>/sap/hana/xs/admin/
8. Activate the job. Specify User name and other details and save the job.
9. Check the job logs to ensure the XS job is active and running according to the defined schedule.
You can open the table STOCK and see the data inserted into it.
Download Full Source Code:
Click here to download the full source code.
Continue reading:
- SAP HANA Core Data Service
- SAP HANA OData Service
- SAP HANA XSJS Service
- SAP HANA XSJS Examples
- HTTP Destination
- XSJS Libraries
- User Defined Structured Type in CDS
I'm so happy to read this. I learned more information in this article. Thank you.
ReplyDeletePlacement Training in Chennai
placement classes
Placement Training institutes
Placement Training in Adyar
soft skills training in chennai
soft skill training courses in chennai
clinical sas training in chennai
SAS Training in Chennai
Thanks for sharing this information to our vision.
ReplyDeleteSpoken English Classes in Adyar
Spoken English Classes in T-Nagar
Spoken English Classes in Porur
Spoken English Class in Anna Nagar
IELTS Chennai
IELTS Classes in Mumbai