创建自定义触发器(示例 2)
- Updated: 2023/07/21
示例展示了如何在 Automation 360 中创建一个触发器,该触发器允许您创建将在指定时间间隔执行的任务。
触发器在满足特定条件时启动 机器人 并等待条件改变或停止。 当触发器的条件匹配时,将调用运行方法向触发器发出信号。
所需注释
要创建触发器,需要以下注释:
注释 | 使用情况 |
---|---|
BotCommand
|
使用 BotCommand 注释,trigger 作为 commandType 。 这确保了普通 Java 对象 (POJO)适合于使用 Automation 360 创建触发器。 |
CommandPkg
|
这些值在创建软件包时使用。 提供注释的名称、标签和描述。 |
TriggerId
|
触发器 ID 是停止触发器必需的。 |
|
指定在满足触发条件时要调用的消费者函数。 |
StartListen
|
启动触发器侦听器。 |
停止所有触发器
|
标识用于取消所有触发器的方法。 |
停止监听
|
标识负责取消特定触发器的方法。 |
用例 - 定时器触发器
以下场景展示了如何创建一个定时器触发器,以定期触发 机器人。 这里解释的演示触发器
命令示例是一个触发器命令,它允许您根据用户指定的间隔创建和管理触发器。 此触发器可用于定期执行任务。
@BotCommand(commandType = BotCommand.CommandType.Trigger) @CommandPkg(label = "Demo Trigger", description = "Demo Trigger", icon = "email.svg", name = "demoTrigger", return_type = RECORD, return_name = "TriggerData", return_description = "Available keys: triggerType")
- 命令类型: 触发器
- 标签: 演示触发器
- 描述: 演示触发器
- 图标:email.svg
- 名称:demoTrigger
- 返回类型: RECORD
- 返回名称: TriggerData
- 返回描述: 可用键:triggerType
TriggerDemo
类是一个管理触发任务的单例类。 它使用 triggerId
作为键在一个映射中存储任务。 一旦满足触发条件,关联的可运行
的允许
方法将被调用以发出触发信号。
-
taskMap
: 一个静态ConcurrentHashMap
,用于存储多个触发任务。 -
TIMER
: 一个静态的Timer
,用于调度触发任务。 -
triggerUid
: 注释为@TriggerId
,它代表了触发器的唯一标识符。 -
consumer
: 注释为@TriggerConsumer
,它是将接受触发数据的消费者。
-
startTrigger
: 注释为@StartListen
,该方法可启动触发器。 它需要一个间隔参数(以秒为单位)来指定触发器激活的间隔。 它创建一个新的TimerTask>
,在执行时调用关联的可运行
的运行
方法。 将TimerTask
添加到taskMap
中,以 triggerUid 作为键,计划在指定的间隔时间内运行。 -
getRecordValue
: 一个私有方法,可创建并返回一个RecordValue
,包含触发器数据。 触发器数据包含一个架构,其中有一个键“triggerType”及其对应的值“Interval Trigger”。 -
stopAllTriggers
: 注释为@StopAllTriggers
,此方法取消所有触发任务并清除taskMap
。 -
stopListen
: 注释为@StopListen
,此方法取消由triggerUid
参数标识的特定触发任务,并将其从taskMap
中移除。 - 设置方法:
setTriggerUid
和setConsumer
分别是triggerUid
和consumer
变量的 setter 方法。
/* * 版权所有 (c) 2023 Automation Anywhere。 * 保留所有权利。 * * 本软件是 Automation Anywhere 的专有信息。 * 您只能根据与 Automation Anywhere 签订的许可协议的条款使用它。 */ /** * */ package com.automationanywhere.botcommand.samples.trigger; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; import com.automationanywhere.botcommand.data.Value; import com.automationanywhere.botcommand.data.impl.RecordValue; import com.automationanywhere.botcommand.data.impl.StringValue; import com.automationanywhere.botcommand.data.model.Schema; import com.automationanywhere.botcommand.data.model.record.Record; import com.automationanywhere.commandsdk.annotations.*; import com.automationanywhere.commandsdk.annotations.rules.GreaterThan; import com.automationanywhere.commandsdk.annotations.rules.NotEmpty; import com.automationanywhere.commandsdk.annotations.rules.NumberInteger; import com.automationanywhere.commandsdk.model.AttributeType; import com.automationanywhere.commandsdk.model.DataType; import static com.automationanywhere.commandsdk.model.DataType.RECORD; /** * 此类将会有一个单例实例。 每当创建一个新触发器*时,它都会带有一个 triggerId。 在此示例中,我们创建多个任务*,并将它们存储在一个映射中,以这些 triggerId 作为键。 一旦触发器的条件匹配,我们就会调用可运行对象的运行方法来发出触发器信号。*在下面的例子中,我们将创建一个触发器,该触发器是用户指定的间隔。 * */ @BotCommand(commandType = BotCommand.CommandType.Trigger) @CommandPkg(label = "Demo Trigger", description = "Demo Trigger", icon = "email.svg", name = "demoTrigger", return_type = RECORD, return_name = "TriggerData", return_description = "Available keys: triggerType") public class TriggerDemo { // Map storing multiple tasks private static final Map<String, TimerTask> taskMap = new ConcurrentHashMap<>(); private static final Timer TIMER = new Timer(true); @TriggerId private String triggerUid; @TriggerConsumer private Consumer consumer; /* * Starts the trigger. */ @StartListen public void startTrigger(@Idx(index = "1", type = AttributeType.NUMBER) @Pkg(label = "Please provide the interval to trigger in seconds", default_value = "120", default_value_type = DataType.NUMBER) @GreaterThan("0") @NumberInteger @NotEmpty Double interval) { TimerTask timerTask = new TimerTask() { @Override public void run() { consumer.accept(getRecordValue()); } }; taskMap.put(this.triggerUid, timerTask); TIMER.schedule(timerTask, interval.longValue()); } private RecordValue getRecordValue() { List<Schema> schemas = new LinkedList<>(); List<Value> values = new LinkedList<>(); schemas.add(new Schema("triggerType")); values.add(new StringValue("Interval Trigger")); RecordValue recordValue = new RecordValue(); recordValue.set(new Record(schemas,values)); return recordValue; } /* * Cancel all the task and clear the map. */ @StopAllTriggers public void stopAllTriggers() { taskMap.forEach((k, v) -> { if (v.cancel()) { taskMap.remove(k); } }); } /* * Cancel the task and remove from map * * @param triggerUid */ @StopListen public void stopListen(String triggerUid) { if (taskMap.get(triggerUid).cancel()) { taskMap.remove(triggerUid); } } public void setTriggerUid(String triggerUid) { this.triggerUid = triggerUid; } public void setConsumer(Consumer consumer) { this.consumer = consumer; } }
示例
以下示例使用
演示触发器
demoTrigger.startTrigger(60.0);
这将启动一个触发器,每 60 秒运行一次,并使用触发器数据调用相关的消费者。
要停止触发器,您可以使用以下方法:
-
demoTrigger.stopAllTriggers()
: 此方法将停止所有活动触发器。 -
demoTrigger.stopListen(triggerUid)
: 该方法将停止由triggerUid
标识的特定触发器。注:triggerUid
是每个触发器的唯一标识符,在创建触发器时自动分配。