创建自定义触发器(示例 1)
- Updated: 2025/04/23
软件开发工具包(SDK)提供工具用于构建自定义触发器,触发器将根据预定义条件自动执行操作。
本节将讨论如何使用 Automation Anywhere Package SDK 创建间隔触发器。 使用名为 TriggerDemo
的示例类逐步阐述该工具包的功能。 最后,您将充分理解如何使用 Package SDK 构建自己的自定义触发器:
过程
package com.automationanywhere.botcommand.samples.trigger; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; import javax.sql.DataSource; 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.botcommand.samples.exceptions.DemoException; import com.automationanywhere.commandsdk.annotations.*; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.automationanywhere.botcommand.exception.BotCommandException; 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 com.automationanywhere.core.security.SecureString; import static com.automationanywhere.commandsdk.model.DataType.RECORD; /** * 此示例是基于计时器的触发器 {@link TriggerDemo} 的扩展,演示了 * 拉取机制。* 触发器将定期检查数据库,如果用户提供的 SQL 返回的记录数 * 大于 0,则会触发。* 注 1: 此类仅用于说明目的,不适合在实际正式中使用。* 注 2: 请在 build gradle 中添加有效的 DB 驱动程序以运行此示例。 * */@BotCommand(commandType = BotCommand.CommandType.Trigger) @CommandPkg(label = "JDBC Query Trigger", description = "JDBC Query Trigger", icon = "jdbc.svg", name = "jdbcQueryTrigger", return_type = RECORD, return_name = "TriggerData", return_description = "Available keys: triggerType") public class DBStatus { private static Logger logger = LogManager.getLogger(DBStatus.class); // 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.TEXT) @Pkg(label = "Please provide the database driver class") @NotEmpty String driverClassName, @Idx(index="2", type = AttributeType.TEXT) @Pkg(label = "Please provide the Jdbc connection string") @NotEmpty String jdbcUrl, @Idx(index="3", type = AttributeType.TEXT) @Pkg(label = "Please provide the user Name") @NotEmpty String userName, @Idx(index="4", type = AttributeType.CREDENTIAL) @Pkg(label = "Please provide the password") @NotEmpty SecureString password, @Idx(index="5", type = AttributeType.TEXT) @Pkg(label = "Please provide the SQL to check the records") @NotEmpty String sqlQuery, @Idx(index = "6", type = AttributeType.NUMBER) @Pkg(label = "Please provide the interval to query in seconds", default_value = "300", default_value_type = DataType.NUMBER) @GreaterThan("0") @NumberInteger @NotEmpty Double interval) { DataSource dataSource = getDataSource(driverClassName, jdbcUrl, userName, password); TimerTask timerTask = new TimerTask() { @Override public void run() { logger.debug("checking DB"); try { if(checkRecordsExist(dataSource.getConnection(), sqlQuery)){ consumer.accept(getRecordValue()); return; } } catch (SQLException e) { logger.warn(e.getMessage(),e); logger.warn("Trigger is still running."); } logger.debug("no records found"); } }; taskMap.put(this.triggerUid, timerTask); TIMER.schedule(timerTask, interval.longValue(), interval.longValue()); } private RecordValue getRecordValue() { List<Schema> schemas = new LinkedList<>(); List<Value> values = new LinkedList<>(); schemas.add(new Schema("triggerType")); values.add(new StringValue("DBStatus")); 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 static DataSource getDataSource(String driverClassName, String url, String userName,SecureString password) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driverClassName); ds.setUrl(url); ds.setUsername(userName); ds.setPassword(password.getInsecureString()); return ds; } public static boolean checkRecordsExist(Connection con, String query) throws SQLException { Statement stmt = null; try { stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(query); rs.last(); if(rs.getRow() > 0) return true; } catch (SQLException e ) { throw new DemoException("Problem running statemnt", e); } finally { if (stmt != null) { stmt.close(); } } return false; } public String getTriggerUid() { return triggerUid; } public void setTriggerUid(String triggerUid) { this.triggerUid = triggerUid; } public Consumer getConsumer() { return consumer; } public void setConsumer(Consumer consumer) { this.consumer = consumer; } }
后续步骤
在上述代码示例中,描述了用于创建和管理触发器的各种方法和注释。 通过理解代码及其功能,您现在可以使用 Package SDK 创建自己的间隔触发器。 尝试不同的时间间隔和触发操作,以有效自动化您的业务流程。
以上只是一个示例,您需要根据您的具体要求和目标受众进一步完善和定制。