Search in sources :

Example 1 with KafkaActionPayload

use of org.thingsboard.server.extensions.kafka.action.KafkaActionPayload in project thingsboard by thingsboard.

the class KafkaMsgHandler method process.

@Override
public void process(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) throws RuleException {
    if (!(msg instanceof KafkaActionMsg)) {
        throw new RuleException("Unsupported message type " + msg.getClass().getName() + "!");
    }
    KafkaActionPayload payload = ((KafkaActionMsg) msg).getPayload();
    log.debug("Processing kafka payload: {}", payload);
    try {
        producer.send(new ProducerRecord<>(payload.getTopic(), payload.getMsgBody()), (metadata, e) -> {
            if (payload.isSync()) {
                if (metadata != null) {
                    ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId())));
                } else {
                    ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onError(payload.getMsgType(), payload.getRequestId(), e)));
                }
            }
        });
    } catch (Exception e) {
        throw new RuleException(e.getMessage(), e);
    }
}
Also used : KafkaActionMsg(org.thingsboard.server.extensions.kafka.action.KafkaActionMsg) KafkaActionPayload(org.thingsboard.server.extensions.kafka.action.KafkaActionPayload) ResponsePluginToRuleMsg(org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg) RuleException(org.thingsboard.server.extensions.api.rules.RuleException) RuleException(org.thingsboard.server.extensions.api.rules.RuleException)

Aggregations

ResponsePluginToRuleMsg (org.thingsboard.server.extensions.api.plugins.msg.ResponsePluginToRuleMsg)1 RuleException (org.thingsboard.server.extensions.api.rules.RuleException)1 KafkaActionMsg (org.thingsboard.server.extensions.kafka.action.KafkaActionMsg)1 KafkaActionPayload (org.thingsboard.server.extensions.kafka.action.KafkaActionPayload)1