-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from tst-labs/ISSUE-248
Implementa integração com Apache Kafka
- Loading branch information
Showing
7 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/esocial-jt-service/src/main/java/br/jus/tst/esocialjt/ocorrencia/OcorrenciaResp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package br.jus.tst.esocialjt.ocorrencia; | ||
|
||
public class OcorrenciaResp { | ||
public long statusCode; | ||
public boolean isError; | ||
public Object payload; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/esocial-jt-service/src/main/java/br/jus/tst/esocialjt/ocorrencia/OcorrenciaRespPub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package br.jus.tst.esocialjt.ocorrencia; | ||
|
||
import java.util.UUID; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class OcorrenciaRespPub { | ||
@Value("${esocial-jt-ocorrencia-resp-topic:}") | ||
private String topic; | ||
|
||
@Autowired | ||
private KafkaTemplate<String, OcorrenciaResp> kafkaTemplate; | ||
|
||
public void send(long statusCode, boolean isError, Object payload) { | ||
OcorrenciaResp ocorrenciaResp = new OcorrenciaResp(); | ||
ocorrenciaResp.statusCode = statusCode; | ||
ocorrenciaResp.isError = isError; | ||
ocorrenciaResp.payload = payload; | ||
kafkaTemplate.send(topic, UUID.randomUUID().toString(), ocorrenciaResp); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/esocial-jt-service/src/main/java/br/jus/tst/esocialjt/ocorrencia/OcorrenciaSub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package br.jus.tst.esocialjt.ocorrencia; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.Date; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.kafka.annotation.KafkaListener; | ||
import org.springframework.messaging.handler.annotation.Payload; | ||
import org.springframework.stereotype.Component; | ||
|
||
import br.jus.tst.esocial.ocorrencia.OcorrenciaDTO; | ||
import br.jus.tst.esocialjt.dominio.Ocorrencia; | ||
import br.jus.tst.esocialjt.negocio.OcorrenciaServico; | ||
|
||
@Component | ||
public class OcorrenciaSub { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(OcorrenciaSub.class); | ||
|
||
@Value("${esocial-jt-ocorrencia-topic:}") | ||
private String topic; | ||
|
||
@Autowired | ||
OcorrenciaServico servico; | ||
|
||
@Autowired | ||
OcorrenciaDTODeserializer deserializer; | ||
|
||
@Autowired | ||
OcorrenciaRespPub ocorrenciaRespPub; | ||
|
||
@KafkaListener(topics = "${esocial-jt-ocorrencia-topic:}", autoStartup = "${kafka.autostart:false}") | ||
public void consume(@Payload String mensagem) { | ||
|
||
LOGGER.info("Lendo tópico: \n" | ||
+ "\ttpc: " + topic + "\n" | ||
+ "\tmsg: " + mensagem+"\n"); | ||
|
||
try { | ||
OcorrenciaDTO ocorrenciaDTO = deserializer.converter(mensagem); | ||
Ocorrencia ocorrencia = OcorrenciaMapper.INSTANCE.comoOcorrencia(ocorrenciaDTO); | ||
ocorrencia.setDataRecebimento(new Date(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli())); | ||
Ocorrencia ocorrenciaSalva = servico.salvar(ocorrencia); | ||
ocorrenciaRespPub.send(200, false, ocorrenciaSalva); | ||
} catch (Exception e) { | ||
LOGGER.error("Erro ao ler tópico: \n" | ||
+ "\ttpc: " + topic + "\n" | ||
+ "\tmsg: " + mensagem+"\n" | ||
+ "\t"+e.getMessage()); | ||
LOGGER.debug(e.getMessage(), e); | ||
ocorrenciaRespPub.send(400, true, e.getMessage()); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/esocial-jt-service/src/main/java/br/jus/tst/esocialjt/util/KafkaSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package br.jus.tst.esocialjt.util; | ||
|
||
import org.springframework.kafka.support.serializer.JsonSerializer; | ||
|
||
public class KafkaSerializer<T> extends JsonSerializer<T> { | ||
|
||
public KafkaSerializer() { | ||
super(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters