阅读和查看 Automation Anywhere 文档

Automation Anywhere Automation 360

关闭内容

内容

打开内容

拉取和推送机制的触发示例

  • 已更新:12/09/2020
    • Automation 360 v.x
    • 探索
    • RPA 工作区

拉取和推送机制的触发示例

本主题中提供的示例演示了使用 SDK 软件包的触发拉取 (link TriggerDemo) 和推送 (SimpleMessageListenerContainer) 机制。

拉取机制示例

此示例是基于计时器触发器的扩展 (TriggerDemo),并演示了触发器拉取机制。

  • 触发器会定期检查数据库 (DB),如果用户提供的 SQL (DB) 值大于 0,则会触发基于计时器的触发器。
  • 在构建 gradle 中中添加有效的 DB 驱动程序以运行此示例。
注: 该类为说明性示例,禁止在生产环境中使用。
@BotCommand(commandType = BotCommand.CommandType.Trigger)
@CommandPkg(label = "JDBC Query Trigger", description = "JDBC Query Trigger", icon = "jdbc.svg", name = "jdbcQueryTrigger")
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;
	@TriggerRunnable
	private Runnable runnable;

	/*
	 * Starts the trigger.
	 */
	@StartListen
	public void startTrigger(
			@Idx(index="1", type = AttributeType.TEXT)
			@Pkg(label = "Provide the database driver class")
			@NotEmpty
			String driverClassName,
			
			@Idx(index="2", type = AttributeType.TEXT)
			@Pkg(label = "Provide the Jdbc connection string")
			@NotEmpty
			String jdbcUrl,
			
			@Idx(index="3", type = AttributeType.TEXT)
			@Pkg(label = "Provide the user Name")
			@NotEmpty
			String userName,
			
			@Idx(index="4", type = AttributeType.CREDENTIAL)
			@Pkg(label = "Provide the password")
			@NotEmpty
			SecureString password,
			
			@Idx(index="5", type = AttributeType.TEXT)
			@Pkg(label = "Provide the SQL statement to check the records")
			@NotEmpty
			String sqlQuery,
			
			@Idx(index = "6", type = AttributeType.NUMBER)
			@Pkg(label = "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)){
						runnable.run();
						return;
					}
				} catch (SQLException e) {
					logger.warn(e.getMessage(),e);
					logger.warn("Trigger is still running.");
				}
				logger.debug("no recorrds found");

			}
		};

		taskMap.put(this.triggerUid, timerTask);
		TIMER.schedule(timerTask, interval.longValue(), interval.longValue());
	}

	/*
	 * Cancel all the tasks and clear the map.
	 */
	@StopAllTriggers
	public void stopAllTriggers() {
		taskMap.forEach((k, v) -> {
			if (v.cancel()) {
				taskMap.remove(k);
			}
		});
	}

	/*
	 * Cancel the task and remove from the 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 BotCommandException("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 Runnable getRunnable() {
		return runnable;
	}

	public void setRunnable(Runnable runnable) {
		this.runnable = runnable;
	}
}

推送机制示例

以下示例展示了如何创建简单的 ActiveMQ 消息侦听器 (SimpleMessageListenerContainer) 来演示触发器推送机制。

@BotCommand(commandType = BotCommand.CommandType.Trigger)
@CommandPkg(label = "JMS Trigger", description = "JMS Trigger", icon = "jms.svg", name = "jmsTrigger")
public class JMSQueue implements SessionAwareMessageListener {

	// Map storing multiple MessageListenerContainer
	private static final Map<String, MessageListenerContainer> taskMap = new ConcurrentHashMap<>();

	@TriggerId
	private String triggerUid;
	@TriggerRunnable
	private Runnable runnable;
	
	//This method is called by MessageListenerContainer when a message arrives.
	// Enable the trigger at this point 
	@Override
	public void onMessage(javax.jms.Message message, Session session) throws JMSException {
		runnable.run();

	}

	/*
	 * Starts the trigger.
	 * 
	 * This method is used to setup the trigger, that is setup the MessageListenerContainer and start it.
	 */
	@StartListen
	public void startTrigger(@Idx(index = "1", type = AttributeType.TEXT)
	@Pkg(label = "Provide the broker URL")
	@NotEmpty
	String brokerURL, @Idx(index = "2", type = AttributeType.TEXT)
	@Pkg(label = "Provide the queue name")
	@NotEmpty
	String queueName) {
		
		if (taskMap.get(triggerUid) == null) {
			synchronized (this) {
				if (taskMap.get(triggerUid) == null) {
					SimpleMessageListenerContainer messageListenerContainer = new SimpleMessageListenerContainer();
					messageListenerContainer.setConnectionFactory(new PooledConnectionFactory(brokerURL));
					messageListenerContainer.setDestinationName(queueName);
					messageListenerContainer.setMessageListener(this);
					messageListenerContainer.start();
					taskMap.put(triggerUid, messageListenerContainer);

				}
			}
		}

	}

	/*
	 * Cancel all the tasks and clear the map.
	 */
	@StopAllTriggers
	public void stopAllTriggers() {
		taskMap.forEach((k, v) -> {
			v.stop();
			taskMap.remove(k);
		});
	}

	/*
	 * Cancel the task and remove from the map
	 *
	 * @param triggerUid
	 */
	@StopListen
	public void stopListen(String triggerUid) {
		taskMap.get(triggerUid).stop();
		taskMap.remove(triggerUid);
	}

	public String getTriggerUid() {
		return triggerUid;
	}

	public void setTriggerUid(String triggerUid) {
		this.triggerUid = triggerUid;
	}

	public Runnable getRunnable() {
		return runnable;
	}

	public void setRunnable(Runnable runnable) {
		this.runnable = runnable;
	}

}
发送反馈