[JAVA] Try using JobScheduler's REST-API

Try using JobScheduler's REST-API

1. First-About JobScheduler-

We have summarized the knowledge gained so far about JobScheduler of SOS, which was used for the first time at work from the middle of last month. The appeal of JobScheduler is that it is programmable and multifunctional, and there is plenty of room for Mashup. There are many good points unique to OSS, and when you touch it, it will sprout. It is also wonderful that Chairman Funai of the User Group and everyone are kind and enthusiastic about popularization. I also wanted to disseminate information so that this charming job management tool would become a little more popular in Japan.

2. Purpose of this document

Here is a sample RestClient for REST-API (JASON based) functions added from the latest version. I made a template of RestClient that performs the following processing.


・ Authentication (Make a basic authentication request and get an AccesToken) -Issue a request to REST-API (Issuing a request to acquire order information from RescClient using AccessToken) ・ Parsing the returned value (Parse return value with JSON format hierarchy)


Compared to a script language such as Python, which has a good undercarriage, implementing it in Java seems to be troublesome. By introducing a convenient library, it can be implemented in a fairly short man-hour. I wrote a simple code that can be easily tried by basic engineers who do not usually have much opportunity to program Java.

3. Configuration

【REST-API】 Please refer to the following for the REST-API specifications used this time.

This is a list of REST-API reception URLs that can get a View of all system information of JobScheduler. http://test.sos-berlin.com/JOC/raml-doc/JOC-API/

Click here for detailed I / F information of REST-API to acquire order information. http://test.sos-berlin.com/JOC.1.11.0/raml-doc/JOC-API/resource/orders.html

Regarding the REST-API I / F for authentication, this article was very easy to understand. https://qiita.com/ike_dai/items/8eb4a542aa59937a5f64

[Verification environment]

Built on VirtualBox (CentOS-7-x86_64-Minimal-1708.iso)


・ JobScheduler: (1.11.4) ・ OS: RHEL Ver7.3 -DB: PostgreSQL 9.6.X -Java: JDK1.8.0_152-b16


[Additional library to be introduced]

Introduced "jersey" for RestClient implementation Introduced "json" for parsing JSON format data


・ Jersey-bundle-1.18.jar ・ Jackson-core-2.1.4.jar -Jackson-databind-2.2.0-rc1.jar ・ Jackson-annotations-2.0.0-rc1.jar


[Sample program]

Package information and location of the created program.

tmp
└─kaeru
        ├─ RestClient.java
        ├─ RestClient.class
        ├─ RestTest.java
        └─ RestTest.class

[Verification environment construction procedure]

For the detailed procedure for building the verification environment, refer to the article by Mr. Funai, the chairman of the user group. https://qiita.com/satoruf/items/fd78523c68a7c2a0593e

After building, start JOC (Jetty) and manager.

4. RestClient body source

The source of the main body of RestClient is here.

/tmp/kaeru/RestClient.java https://qiita.com/yatabekaeru/items/b8f03cccfb8b904efe99

① Client for Basic authentication

When generating a Client for Basic authentication, HTTPBasicAuthFilter is applied. No data settings are required for the request body.

② Client for Access Token authentication

The Client that issues the request after issuing the AccessToken sets the AccessToken in the header field. Data is set in the request body as appropriate according to the various REST-API interface specifications.

When issuing a request to "Orders", the following JASON format character string is set in the request body. 『 { jobschedulerId:"scheduler" }

Five. Test class source

The test class is here.

/tmp/kaeru/RestTest.java https://qiita.com/yatabekaeru/items/06e7d40a12935f107458

Two times, when an Access Token is issued by Basic authentication and a request to REST-API "Orders" Make a POST request to the given URL. The data returned as the latter response is parsed by the JASON parser.

6. Easy compilation

Follow the procedure below to compile. It is a super easy execution environment. You can create a batch process with a simple procedure without a deployment tool.

** ★ Below, connect to the verification environment with a terminal and execute as a scheduler user. ** **

① Create a package placement directory

Create a package placement directory.

mkdir /tmp/kaeru

② Source file placement

Place the source file.

/tmp/kaeru/RestClient.java
/tmp/kaeru/RestTest.java

③ Placement of additional jar files

Place the additional jar file in any directory. (Example: Under /usr/java/jdk1.8.0_152/ap_lib/, etc.)

④ CLASSPATH variable setting

Set the CLASSPATH variable to an environment variable.

CLASSPATH=/usr/java/jdk1.8.0_152/ap_lib/jersey-bundle-1.18.jar:/usr/java/jdk1.8.0_152/ap_lib/jackson-core-2.1.4.jar:/usr/java/jdk1.8.0_152/ap_lib/jackson-databind-2.2.0-rc1.jar:/usr/java/jdk1.8.0_152/ap_lib/jackson-annotations-2.0.0-rc1.jar:/usr/java/jdk1.8.0_152/jre/lib:/tmp/
export CLASSPATH

⑤ Move directory

Change to the directory one level above the package directory. (If / tmp / kaeru is a package directory)

cd /tmp

⑥ Compile execution

Compile with the javac command

javac kaeru/RestClient.java kaeru/RestTest.java

⑦ Grant execution authority

Grant execute permission.

chmod +x /tmp/kaeru/*.class

7. 7. Run

The command execution procedure is described below.

** ★ Execute as the scheduler user in the same terminal as the compiled terminal below. ** ** (If you boot from another terminal, reset your CLASSPATH)

① Move directory

Change to the directory one level above the package directory. (If / tmp / kaeru is a package directory)

cd /tmp

② Sample program execution

Execute the sample program with the following command.

java kaeru.RestTest

③ Execution result

[scheduler@localhost tmp]$ java kaeru.RestTest

<Issued Access Token>

*****1cf7ffa4-ab60-4604-a0e8-36d93b7abe95

<JSON format data>

[orderId]: createDailyPlan [job]: /sos/dailyplan/CreateDailyPlan [path]: /sos/dailyplan/CreateDailyPlan,createDailyPlan [status]: PENDING [nextStart]: 2018-01-08T15:01:00Z
[orderId]: MonitorSystem [job]: /sos/notification/SystemNotifier [path]: /sos/notification/SystemNotifier,MonitorSystem [status]: PENDING [nextStart]:
[orderId]: AcknowledgeMonitorSystem [job]: /sos/notification/ResetNotifications [path]: /sos/notification/ResetNotifications,AcknowledgeMonitorSystem [status]: PENDING [nextStart]:
[orderId]: Reporting [job]: /sos/reporting/ReportingAggregation [path]: /sos/reporting/Reporting,Reporting [status]: PENDING [nextStart]:
[orderId]: Cleanup [job]: /sos/notification/CleanupNotifications [path]: /sos/notification/CleanupNotifications,Cleanup [status]: PENDING [nextStart]:
[orderId]: Unskip [job]: /sos/operations/criticalpath/UncriticalJobNodes [path]: /sos/operations/criticalpath/UncriticalJobNodes,Unskip [status]: PENDING [nextStart]:
[orderId]: Skip [job]: /sos/operations/criticalpath/UncriticalJobNodes [path]: /sos/operations/criticalpath/UncriticalJobNodes,Skip [status]: PENDING [nextStart]:


  1. NextStep

If you want to make it a little more Java-like, prepare a data storage class and mapper and implement object mapping. Creating a set of REST-APIs required for operation and monitoring makes it a nice operation tool.

9. Caution!

The JOC also uses Jersey to start the RESTful API service. Placing additional libraries on the PATH referenced by Jetty may not work properly! --This is because version conflicts occur depending on the environment.

** Be sure to place the additional jar files individually so that unrelated services do not refer to them. ** **

Ten. Finally

So far, I have introduced the REST-API sample program in a hurry. (Please forgive me for the quality of the New Year.)

In addition, I would like to revise the manuscript and upload an article tailored as an actual operation tool.

Recommended Posts

Try using JobScheduler's REST-API
Try using JobScheduler's REST-API --Java RestClient implementation--
Try using JobScheduler's REST-API --Java RestClient Test class-
Try using libGDX
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
Try using Axon Framework
Try using java.lang.Math methods
Try using PowerMock's WhiteBox
Try using Talend Part 2
Try using Talend Part 1
Try using F # list
Try using each_with_index method
Try using Spring JDBC
Try using RocksDB in Java
Try using GloVe with Deeplearning4j
Try using view_component with rails
Try scraping using java [Notes]
Try using Cocoa from Ruby
Try using letter_opener_web for inquiries
[Swift] Try using Collection View
Try using IntelliJ IDEA once
Try using Spring Boot Security
Try using gRPC in Ruby
[Rails] Try using Faraday middleware
[Processing] Try using GT Force.
[Programming Encyclopedia] §2 Try using Ruby
People using docker Try using docker-compose
Try using Redmine on Mac docker
Try using Redis with Java (jar)
[Java] Try to implement using generics
Try using the messaging system Pulsar
Try using IBM Java method tracing
Try using Hyperledger Iroha's Java SDK
[Java] Where did you try using java?
Try using Java framework Nablarch [Web application]
Try using || instead of the ternary operator
Try using the service on Android Oreo
Try using the Stream API in Java
Try using the Rails API (zip code)
Study Java Try using Scanner or Map
Try using JSON format API in Java
Try using Spring Boot with VS Code
Try using Reladomo's MT Loader (Multi-Threaded Matcher Loader)
Create RestAPI using wicketstuff-restannotations of Apache Wicket
Try using Kong + Konga with Docker Compose.
Try using the Emotion API from Android
Try using the Wii remote with Java
Try using simple_form / edit even child models
Try implementing GraphQL server using grahpql-java-tools (+ kotlin)