Gatilhos personalizados - mecanismo de puxar e empurrar

Os exemplos fornecidos neste tópico demonstram como criar gatilhos baseados no tempo e em eventos, puxam (link TriggerDemo) e empurram (SimpleMessageListenerContainer) mecanismos que utilizam o pacote SDK.

Exemplo: Gatilhos baseados no tempo - Mecanismo de puxar

As condições de acionamento necessárias são testadas no intervalo regular, em segundos.

  • Um gatilho verifica o banco de dados (DB) em intervalos regulares e se o valor SQL (DB) que um usuário fornece é mais do que 0, então o gatilho baseado no tempo é acionado.
  • Quando o evento ocorre, ele executa o método consumer.accept(<RecordValue Instance>) e aciona um bot associado.
  • Ele verifica as condições em cada execução.
  • Adicione o driver de DB válido no arquivo construir gradiente para executar este exemplo.

Este exemplo é uma extensão do gatilho baseado no tempo (TriggerDemo) e demonstra o mecanismo de acionamento do gatilho.

Nota: Este é um exemplo ilustrativo e não o utilize no ambiente de produção.
@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 = "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 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)){
						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 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 Consumer getConsumer() {
		return consumer;
	}
	public void setConsumer(Consumer consumer) {
		this.consumer = consumer;
	}
}

Exemplo: Gatilhos baseados em eventos - Mecanismo de empurrar

O gatilho aguarda a ocorrência de um evento, por exemplo, um ouvinte de mensagens começar a ouvir.

  • Os detalhes de um evento são armazenados na memória e um ouvinte de mensagem espera por uma mensagem.
  • Quando o evento ocorre, ele executa o método consumer.accept(<RecordValue Instance>) e aciona um bot associado.

O exemplo a seguir mostra como criar um simples ouvinte de mensagens ActiveMQ (SimpleMessageListenerContainer) para demonstrar o mecanismo de acionamento do gatilho.

@BotCommand(commandType = BotCommand.CommandType.Trigger)
@CommandPkg(label = "JMS Trigger", description = "JMS Trigger", icon = "jms.svg", name = "jmsTrigger",
		return_type = RECORD, return_name = "TriggerData", return_description = "Available keys: triggerType")
public class JMSQueue implements SessionAwareMessageListener {

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

	@TriggerId
	private String triggerUid;
	@TriggerConsumer
	private Consumer consumer;
	
	//This method is called by MessageListenerContainer when a message arrives.
	// At this point, the trigger get enabled 
	@Override
	public void onMessage(javax.jms.Message message, Session session) throws JMSException {
		consumer.accept(getRecordValue());

	}

	private RecordValue getRecordValue() {
		List<Schema> schemas = new LinkedList<>();
		List<Value> values = new LinkedList<>();
		schemas.add(new Schema("triggerType"));
		values.add(new StringValue("JMSQueue"));

		RecordValue recordValue = new RecordValue();
		recordValue.set(new Record(schemas,values));
		return recordValue;
	}
	/*
	 * Starts the trigger.
	 * 
	 * Use this method to setup the trigger, such as, 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 tasks 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 Consumer getConsumer() {
		return consumer;
	}

	public void setConsumer(Consumer consumer) {
		this.consumer = consumer;
	}

}