文档首页 > > SDK参考> 工作流Java SDK> 工作流触发器示例

工作流触发器示例

分享
更新时间: 2019/07/11 20:20

创建工作流触发器

首先创建工作流client客户端,然后根据触发器定义,调用createTrigger函数创建触发器。CreateTriggerRequest和CreateTriggerResponse属性的详细信息,参考创建触发器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.fgs.sdk.demo;

import com.hwc.fgs.client.FunctionGraphClient;
import com.hwc.fgs.config.FunctionGraphConfig;
import com.hwc.fgs.exception.FgsClientException;
import com.hwc.fgs.exception.FgsServerException;
import com.hwc.fgs.request.trigger.CreateTriggerRequest;
import com.hwc.fgs.response.trigger.CreateTriggerResponse;

public class Main {
    public static void main(String[] args) {
        String ak = "your ak";
        String sk = "your sk";
        String region = "FGS_region";
        String projectId = "your project_id";

        // 创建工作流 client
        FunctionGraphConfig config = new FunctionGraphConfig(region, ak, sk, projectId);
        FunctionGraphClient client = new FunctionGraphClient(config);
        
        // 创建触发器
        try {
            // 创建触发器请求
            CreateTriggerRequest createTriggerRequest =
                    new CreateTriggerRequest.Builder()
                            .triggerName("createTrigger")
                            .eventSource("Worker")
                            .description("smn-test")
                            .triggerDef("{\"type\":\"WorkRequest\"}")
                            .build();
            CreateTriggerResponse createTriggerResponse = client.createTrigger(createTriggerRequest);
        } catch (FgsServerException e) {
            System.out.println(e.getErrorMessage());
        } catch (FgsClientException e) {
            System.out.println(e.getErrorMessage());
        }
    }

}

查询工作流触发器

首先创建工作流client客户端,然后根据已经创建的触发器urn,调用getTrigger函数查询触发器。GetTriggerRequest和GetTriggerResponse属性的详细信息,参考查询触发器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.fgs.sdk.demo;

import com.hwc.fgs.client.FunctionGraphClient;
import com.hwc.fgs.config.FunctionGraphConfig;
import com.hwc.fgs.exception.FgsClientException;
import com.hwc.fgs.exception.FgsServerException;
import com.hwc.fgs.request.trigger.GetTriggerRequest;
import com.hwc.fgs.response.trigger.GetTriggerResponse;

public class Main {
    public static void main(String[] args) {
        String ak = "your ak";
        String sk = "your sk";
        String region = "FGS_region";
        String projectId = "your project_id";

        // 创建工作流 client
        FunctionGraphConfig config = new FunctionGraphConfig(region, ak, sk, projectId);
        FunctionGraphClient client = new FunctionGraphClient(config);

        // 查询触发器
        try {
            GetTriggerRequest getTriggerRequest = new GetTriggerRequest
                    .Builder()
                    .triggerUrn("trigger_urn")
                    .build();
            GetTriggerResponse getTriggerResponse = client.getTrigger(getTriggerRequest);
        } catch (FgsServerException e) {
            System.out.println(e.getErrorMessage());
        } catch (FgsClientException e) {
            System.out.println(e.getErrorMessage());
        }
    }

}

更新工作流触发器

首先创建工作流client客户端,然后定义更新字段,最后根据已经创建的触发器urn,调用updateTrigger函数更新触发器。UpdateTriggerRequest属性的详细信息,参考更新触发器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.fgs.sdk.demo;

import com.hwc.fgs.client.FunctionGraphClient;
import com.hwc.fgs.config.FunctionGraphConfig;
import com.hwc.fgs.exception.FgsClientException;
import com.hwc.fgs.exception.FgsServerException;
import com.hwc.fgs.request.trigger.UpdateTriggerRequest;

public class Main {
    public static void main(String[] args) {
        String ak = "your ak";
        String sk = "your sk";
        String region = "FGS_region";
        String projectId = "your project_id";

        // 创建工作流 client
        FunctionGraphConfig config = new FunctionGraphConfig(region, ak, sk, projectId);
        FunctionGraphClient client = new FunctionGraphClient(config);

        // 更新触发器
        try {
            UpdateTriggerRequest updateTriggerRequest = new UpdateTriggerRequest
                    .Builder()
                    .description("update description")
                    .triggerUrn("trigger_urn").build();
            client.updateTrigger(updateTriggerRequest);
        } catch (FgsServerException e) {
            System.out.println(e.getErrorMessage());
        } catch (FgsClientException e) {
            System.out.println(e.getErrorMessage());
        }
    }

}

删除工作流触发器

首先创建工作流client客户端,然后根据已经创建的触发器urn,调用deleteTrigger函数删除触发器。DeleteTriggerRequest属性的详细信息,参考删除触发器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.fgs.sdk.demo;

import com.hwc.fgs.client.FunctionGraphClient;
import com.hwc.fgs.config.FunctionGraphConfig;
import com.hwc.fgs.exception.FgsClientException;
import com.hwc.fgs.exception.FgsServerException;
import com.hwc.fgs.request.trigger.DeleteTriggerRequest;

public class Main {
    public static void main(String[] args) {
        String ak = "your ak";
        String sk = "your sk";
        String region = "FGS_region";
        String projectId = "your project_id";

        // 创建工作流 client
        FunctionGraphConfig config = new FunctionGraphConfig(region, ak, sk, projectId);
        FunctionGraphClient client = new FunctionGraphClient(config);

        // 删除触发器
        try {
            DeleteTriggerRequest deleteTriggerRequest = new DeleteTriggerRequest
                    .Builder()
                    .triggerUrn("trigger_urn").build();
            client.deleteTrigger(deleteTriggerRequest);
        } catch (FgsServerException e) {
            System.out.println(e.getErrorMessage());
        } catch (FgsClientException e) {
            System.out.println(e.getErrorMessage());
        }
    }

}

查询工作流触发器列表

首先创建工作流client客户端,然后创建查询条件,调用listTrigger函数查询触发器列表。ListTriggersRequest和ListTriggersResponse属性的详细信息,参考查询触发器列表

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.fgs.sdk.demo;

import com.hwc.fgs.client.FunctionGraphClient;
import com.hwc.fgs.config.FunctionGraphConfig;
import com.hwc.fgs.exception.FgsClientException;
import com.hwc.fgs.exception.FgsServerException;
import com.hwc.fgs.request.trigger.ListTriggersRequest;
import com.hwc.fgs.response.trigger.ListTriggersResponse;


public class Main {
    public static void main(String[] args) {
        String ak = "your ak";
        String sk = "your sk";
        String region = "FGS_region";
        String projectId = "your project_id";

        // 创建工作流 client
        FunctionGraphConfig config = new FunctionGraphConfig(region, ak, sk, projectId);
        FunctionGraphClient client = new FunctionGraphClient(config);

        try {
            // 创建查询触发器列表请求
            ListTriggersRequest listTriggerRequest = new ListTriggersRequest
                    .Builder()
                    .start(1)
                    .limit(1000)
                    .eventSource("SMN")
                    .triggerUrn("SMN")
                    .triggerName("trigger-43bc").build();

            // 发送该请求
            ListTriggersResponse listTriggerResponse = client.listTriggers(listTriggerRequest);
        } catch (FgsServerException e) {
            System.out.println(e.getErrorMessage());
        } catch (FgsClientException e) {
            System.out.println(e.getErrorMessage());
        }
    }
}

批量删除工作流触发器

首先创建工作流client客户端,然后创建需要批量删除的触发器列表,最后调用batchDoTrigger批量删除触发器。BatchDoTriggerRequest属性的详细信息,参考触发器批量操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.fgs.sdk.demo;

import com.hwc.fgs.client.FunctionGraphClient;
import com.hwc.fgs.config.FunctionGraphConfig;
import com.hwc.fgs.exception.FgsClientException;
import com.hwc.fgs.exception.FgsServerException;
import com.hwc.fgs.request.trigger.BatchDoTriggerRequest;

import java.util.ArrayList;
import java.util.List;


public class Main {
    public static void main(String[] args) {
        String ak = "your ak";
        String sk = "your sk";
        String region = "FGS_region";
        String projectId = "your project_id";

        // 创建工作流 client
        FunctionGraphConfig config = new FunctionGraphConfig(region, ak, sk, projectId);
        FunctionGraphClient client = new FunctionGraphClient(config);

        try {
            List<String> urnList = new ArrayList<>();
            urnList.add("trigger_urn1");
            urnList.add("trigger_urn2");

            // 创建批量删除触发器请求
            BatchDoTriggerRequest batchDoTriggerRequest = new BatchDoTriggerRequest
                    .Builder()
                    .actionType("delete")
                    .urnList(urnList).build();

            //批量删除触发器
            client.batchDoTrigger(batchDoTriggerRequest);
        } catch (FgsServerException e) {
            System.out.println(e.getErrorMessage());
        } catch (FgsClientException e) {
            System.out.println(e.getErrorMessage());
        }
    }
}
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

跳转到云社区