DataWorks Open Platform consists of the OpenAPI, OpenEvent, and Extensions modules. You can use these modules to subscribe to the messages of a type of event in a specified business process and respond to the messages you receive. This topic describes how to configure the settings in DataWorks Open Platform to perform a lockdown based on the node commit and deployment events on the DataStudio page.
Background information
For more information about the features and basic concepts of DataWorks Open Platform that are involved in this topic, see Overview and Overview.
Enable and configure event message subscription (OpenEvent)
This section describes the core configuration steps and precautions during the configuration. For more information about how to enable and configure event message subscription, see Enable event message subscription.
In the EventBridge console, create a custom bus. You do not need to configure the parameters in the Event Source, Event Rule, and Event Target steps.
In the EventBridge console, create an event rule for the custom bus.
This step describes how to configure the core parameters of the demo package for node commit and deployment events on the DataStudio page. In this example, the node is created in a workspace in standard mode.
Configure an event pattern.
{ "source": [ "acs.dataworks" ], "type": [ "dataworks:FileChange:CommitFile" ] }
source: the identifier of the product in which an event occurred. Set this parameter to acs.dataworks.
type: the type of the event that occurs in the product. Set this parameter to dataworks:FileChange:CommitFile.
NoteFor an event that occurs in a workspace in basic mode, set this parameter to dataworks:FileChange:DeployFile.
You can change the values of the source and type parameters in the Event Pattern Debugging section and then click Test. If the test is successful, click Next Step.
In the Configure Targets step, set Service Type to HTTPS and enter a valid URL. Use the default settings for other parameters.
Go to the Open Platform page in the DataWorks console. In the left-side navigation tree, click OpenEvent. On the page that appears, add an event distribution channel.
On the Open Platform page in the DataWorks console, enable the added event distribution channel.
Register and configure an extension (Extensions)
This section describes the core configuration steps and precautions during the configuration. For more information about how to register and configure an extension, see Develop and deploy an extension based on a self-managed service.
Register an extension.
Go to the Open Platform page in the DataWorks console. In the left-side navigation tree, click Extensions. On the page that appears, click Register Extensions to register an extension. Node commit and deployment events are used in the example. In this step, select Pre-event for Node Commit for Processed Extension Points and configure other parameters by following the on-screen instructions when you register the extension.
Develop and deploy the extension.
Configure DataWorks SDK for Java. For more information, see Install SDKs for Java. Sample code:
package com.aliyun.dataworks.demo; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aliyun.dataworks.config.Constants; import com.aliyun.dataworks.config.EventCheckEnum; import com.aliyun.dataworks.config.ExtensionParamProperties; import com.aliyun.dataworks.services.DataWorksOpenApiClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dataworks_public.model.v20200518.UpdateIDEEventResultRequest; import com.aliyuncs.dataworks_public.model.v20200518.UpdateIDEEventResultResponse; import com.aliyuncs.exceptions.ClientException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.List; /** * @author dataworks demo */ @RestController @RequestMapping("/extensions") public class ExtensionsController { @Autowired(required = false) private DataWorksOpenApiClient dataWorksOpenApiClient; @Autowired private ExtensionParamProperties extensionParamProperties; /** * Receive event messages that are sent from EventBridge. * @param jsonParam */ @PostMapping("/consumer") public void consumerEventBridge(@RequestBody String jsonParam){ JSONObject jsonObj = JSON.parseObject(jsonParam); String eventCode = jsonObj.getString(Constants.EVENT_CODE_FILED); if(Constants.COMMIT_FILE_EVENT_CODE.equals(eventCode)){ // Initialize the client. IAcsClient client = dataWorksOpenApiClient.createClient(); // Query the current time. SimpleDateFormat sdf = new SimpleDateFormat(Constants.YYYY_MM_DD); String now = sdf.format(System.currentTimeMillis()); // Query information about the festivals and holidays in 2022 and the lockdown periods. List<String> holidayList = Arrays.asList(extensionParamProperties.getHolidayList().split(",")); // Check whether the current time is within a lockdown period. boolean isExists = holidayList.stream().anyMatch(day -> day.equals(now)); // The callback method. UpdateIDEEventResultRequest updateIDEEventResultRequest = new UpdateIDEEventResultRequest(); updateIDEEventResultRequest.setMessageId(jsonObj.getString("id")); updateIDEEventResultRequest.setExtensionCode(extensionParamProperties.getExtensionCode()); // You failed the check because the current time is within a lockdown period. if(isExists){ updateIDEEventResultRequest.setCheckResult(EventCheckEnum.FAIL.getCode()); updateIDEEventResultRequest.setCheckResultTip("You cannot perform a file commit operation within a lockdown period."); }else{ // You passed the check because the current time is not within a lockdown period. updateIDEEventResultRequest.setCheckResult(EventCheckEnum.OK.getCode()); updateIDEEventResultRequest.setCheckResultTip(EventCheckEnum.OK.getName()); } try { // The extension processes the event messages and calls an API operation of DataWorks to send the processing result to DataWorks. UpdateIDEEventResultResponse acsResponse = client.getAcsResponse(updateIDEEventResultRequest); // The ID of the request. You can troubleshoot errors based on the ID. System.out.println("acsResponse:" + acsResponse.getRequestId()); } catch (ClientException e) { // The ID of the request. You can troubleshoot errors based on the ID. System.out.println("RequestId:" + e.getRequestId()); // The status code of an error. System.out.println("ErrCode:" + e.getErrCode()); // The description of an error. System.out.println("ErrMsg:" + e.getErrMsg()); } }else{ System.out.println("Failed to filter out other types of events. Check the parameter configurations."); } } }
Enable the extension.
After the extension is registered, click Extensions management. In the left-side navigation pane of the SettingCenter page, click Extension. On the Extension page, turn on the switch for the registered extension in the Enable column and complete the authorization by following the on-screen instructions to enable the extension.
Deploy the code for the client that subscribes to event messages and the code for the extension, and run the code for the extension on your on-premises machine.
Download the demo project file:
Environment requirement: Java 8 or later and Maven. Maven is a build automation tool for Java.
Download link for the project file: extension-demo-deploycontroller.zip (30 KB).
After you download the project file, enter the root directory of the project and run the following command:
mvn clean package -Dmaven.test.skip=true spring-boot:repackage
If you obtain the JAR package that can be directly installed, run the following command:
java -jar target/extension-demo-deploycontroller-1.0.jar
The following figure shows the successfully started project.Enter
http://localhost:8080/index
in the address bar of a browser. If"hello world!"
is returned, the extension is successfully deployed. You can subscribe to event messages after network connections are established between DataWorks and your extension and between EventBridge and your extension.
Verify the result
After you complete the preceding configurations, the check for extension point events is triggered when you commit a node on the DataStudio page.