package com.megatim.fdxconsultation.service.impl.camel.routes.in;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
import com.megatim.fdxcommons.model.dtos.dataproduction.ProductionMetaDataDto;
|
import com.megatim.fdxcommons.tools.context.AppContext;
|
import com.megatim.fdxcommons.tools.database.connection.DBConnection;
|
import com.megatim.fdxcommons.tools.database.queries.EchecIntegrationQueries;
|
import com.megatim.fdxcommons.tools.database.tables.FdxConsultationTable;
|
import com.megatim.fdxconsultation.core.ifaces.integration.EchecIntegrationManager;
|
import com.megatim.fdxconsultation.service.impl.camel.consumer.ProductionMetaDaConsumer;
|
import com.megatim.fdxconsultation.tools.context.AppCommonContext;
|
import java.sql.Connection;
|
import java.sql.SQLException;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
import javax.annotation.Resource;
|
import javax.inject.Inject;
|
import javax.sql.DataSource;
|
import org.apache.camel.Exchange;
|
import org.apache.camel.builder.RouteBuilder;
|
import org.apache.camel.component.jackson.JacksonDataFormat;
|
|
/**
|
*
|
* @author lenovo
|
*/
|
public class ProductionMetaDataRoute extends RouteBuilder {
|
|
@Inject
|
private EchecIntegrationManager echecIntegrationManager;
|
|
@Resource(lookup = AppCommonContext.JNDI_NAME)
|
private DataSource dataSource;
|
|
@Override
|
public void configure() throws Exception {
|
|
ObjectMapper mapper = new ObjectMapper();
|
mapper.registerModule(new JavaTimeModule());
|
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
|
JacksonDataFormat jacksonDataFormat = new JacksonDataFormat();
|
jacksonDataFormat.setObjectMapper(mapper);
|
jacksonDataFormat.setUnmarshalType(ProductionMetaDataDto.class);
|
|
onException(Exception.class)
|
.handled(false)
|
.log("Exception occured: ${exception.message}")
|
.logStackTrace(true)
|
.process(ex -> {
|
System.out.println("---------------------In Exception process----------------------------");
|
ProductionMetaDataDto dto = ex.getIn().getBody(ProductionMetaDataDto.class);
|
|
if (echecIntegrationManager.getById(dto.getDataProduction().getId()) == null) {
|
updateDb(dto);
|
}
|
})
|
.end();
|
|
from("rabbitmq:"
|
+ AppContext.DATA_INTEGRATION_EXCHANGE
|
+ "?queue=" + AppContext.DATA_INTEGRATION_QUEUE
|
+ "&routingKey=" + AppContext.DATA_INTEGRATION_ROUTING_KEY
|
+ "&autoDelete=false"
|
+ "&autoAck=false")//Désactiver l'auto acquittement du message
|
// .log("************************In FdxConsultation*****************************")
|
.errorHandler(defaultErrorHandler()
|
.maximumRedeliveries(3)
|
.onExceptionOccurred(ex -> {
|
Throwable caused = ex.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
|
caused.printStackTrace();
|
|
}))
|
.unmarshal(jacksonDataFormat)
|
.log("------------------------------In ProductionMetaDataRoute-----------------------------------")
|
.bean(ProductionMetaDaConsumer.class);
|
}
|
|
private void updateDb(ProductionMetaDataDto dto) {
|
FdxConsultationTable consTable = new FdxConsultationTable(dto.getDataProduction().getCodeTypeFichier(), dto.getDataProduction().getReferentielVersion());
|
|
try ( Connection connection = new DBConnection(dataSource).connection()) {
|
connection.setAutoCommit(false);
|
|
consTable.removeUnCompleteData(connection, dto.getDataProduction().getId());
|
new EchecIntegrationQueries(connection, dto.getDataProduction().getId()).save();
|
|
connection.commit();
|
|
} catch (SQLException ex) {
|
Logger.getLogger(ProductionMetaDataRoute.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
|
} catch (Exception ex) {
|
Logger.getLogger(ProductionMetaDataRoute.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
|
}
|
}
|
}
|