[ADD] PRUEBAS CON APACHE KAFKA

parent 14feaee5
......@@ -28,33 +28,33 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-logging</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-logging</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>3.2.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-logging</artifactId>-->
<!-- </exclusion>-->
<!-- </exclusions> -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
......@@ -190,6 +190,11 @@
<artifactId>log4j-to-slf4j</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<finalName>trismegisto-services</finalName>
......
......@@ -21,20 +21,24 @@ public class DriveApi {
@PostMapping(path = "/public/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<?> uploadFile(@ModelAttribute DriveRequest request) {
String folder_id = request.getFolder_id();
String file_name = request.getFile_name();
MultipartFile file = request.getFile();
String responseDrive = driveService.uploadFile(folder_id, file_name, file, new JSONObject());
JSONObject response = new JSONObject();
if (responseDrive == null) {
response.put("status", false);
response.put("message", "Error al subir el archivo");
return ResponseEntity.badRequest().body(response.toMap());
} else {
response.put("status", true);
response.put("message", "Archivo subido correctamente");
response.put("file_id", responseDrive);
return ResponseEntity.ok(response.toMap());
try {
String folder_id = request.getFolder_id();
String file_name = request.getFile_name();
MultipartFile file = request.getFile();
String responseDrive = driveService.uploadFile(folder_id, file_name, file, new JSONObject());
JSONObject response = new JSONObject();
if (responseDrive == null) {
response.put("status", false);
response.put("message", "Error al subir el archivo");
return ResponseEntity.badRequest().body(response.toMap());
} else {
response.put("status", true);
response.put("message", "Archivo subido correctamente");
response.put("file_id", responseDrive);
return ResponseEntity.ok(response.toMap());
}
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
}
......
......@@ -26,25 +26,30 @@ public class ExcelApi {
@PostMapping("/public/generate")
public ResponseEntity<?> generateExcel(@RequestBody String json) throws IOException {
JSONObject jsonBody = new JSONObject(json);
int size = json.getBytes().length;
System.out.println("Size: " + size);
byte[] excelByte = excelService.generateExcel(jsonBody,size);
if (jsonBody.optBoolean("base64", false)) {
JSONObject response = new JSONObject();
response.put("file_name", jsonBody.optString("file_name", "no_name.xlsx"));
response.put("base64", commonUtils.byteToBase64(excelByte));
response.put("message", "OK");
response.put("status", true);
return ResponseEntity.ok().body(response.toMap());
} else {
Resource resource = excelService.convertByteToResource(excelByte, jsonBody.optString("file_name", "no_name.xlsx"));
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + jsonBody.optString("file_name", "no_name.xlsx"));
return ResponseEntity.ok()
.headers(headers)
.contentLength(excelByte.length)
.body(resource);
try {
JSONObject jsonBody = new JSONObject(json);
int size = json.getBytes().length;
System.out.println("Size: " + size);
byte[] excelByte = excelService.generateExcel(jsonBody, size);
if (jsonBody.optBoolean("base64", false)) {
JSONObject response = new JSONObject();
response.put("file_name", jsonBody.optString("file_name", "no_name.xlsx"));
response.put("base64", commonUtils.byteToBase64(excelByte));
response.put("message", "OK");
response.put("status", true);
return ResponseEntity.ok().body(response.toMap());
} else {
Resource resource = excelService.convertByteToResource(excelByte, jsonBody.optString("file_name", "no_name.xlsx"));
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + jsonBody.optString("file_name", "no_name.xlsx"));
headers.add(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
return ResponseEntity.ok()
.headers(headers)
.contentLength(excelByte.length)
.body(resource);
}
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
}
}
}
\ No newline at end of file
package web.multitask.trismegistoservices.config;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import java.util.HashMap;
import java.util.Map;
@EnableKafka
@Configuration
public class KafkaConsumerConfig {
@Bean
public ConsumerFactory<String, String> consumerFactory() {
Map<String, Object> config = new HashMap<>();
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
config.put(ConsumerConfig.GROUP_ID_CONFIG, "1");
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
return new DefaultKafkaConsumerFactory<>(config);
}
}
\ No newline at end of file
package web.multitask.trismegistoservices.consumer;
import org.json.JSONObject;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import java.util.logging.Logger;
@Component
public class KafkaConsumer {
Logger logger = Logger.getLogger(KafkaConsumer.class.getName());
@KafkaListener(topics = {"SQLSERVER.EJBPLANILLA","schemahistory.topic","SQLSERVER.EJBPLANILLA.EJBPLANILLA.dbo.CONCODIANE1","SQLSERVER.EJBPLANILLA.EJBPLANILLA.dbo.GEN0001DOC2"}, groupId = "1")
void listener(@Payload String data,
@Header(KafkaHeaders.RECEIVED_PARTITION) int partition,
@Header(KafkaHeaders.OFFSET) int offset) {
JSONObject json = new JSONObject(data);
System.out.println(json);
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package web.multitask.trismegistoservices.services;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -31,13 +32,18 @@ public class ExcelService {
public byte[] generateExcel(JSONObject json,int size) throws IOException {
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
XSSFWorkbook workbook = new XSSFWorkbook();
// org.apache.poi.ss.usermodel.Workbook workbook s= WorkbookFactory.create(new File(System.getProperty("tmpdir") + "/file.xlsx"));
createSheet(workbook, json,size);
try {
workbook.write(outByteStream);
workbook.close();
outByteStream.close();
outByteStream.flush();
return outByteStream.toByteArray();
} catch (Exception e) {
workbook.close();
outByteStream.close();
outByteStream.flush();
System.out.println(e.getMessage());
return null;
}
......@@ -53,7 +59,7 @@ public class ExcelService {
}
} else {
JSONArray sheetArray = json.optJSONArray("sheetArray", new JSONArray());
if (sizeOfBytes < 100000) {
// if (sizeOfBytes < 100000) {
for (int i = 0; i < sheetArray.length(); i++) {
JSONObject jsonObject = sheetArray.getJSONObject(i);
XSSFSheet sheet = workbook.createSheet(jsonObject.optString("sheet_name", "no_name"));
......@@ -63,35 +69,20 @@ public class ExcelService {
}
buildSheet(workbook, sheet, jsonObject);
}
} else {
XSSFSheet[] sheets = new XSSFSheet[sheetArray.length()];
for (int i = 0; i < sheetArray.length(); i++) {
JSONObject jsonObject = sheetArray.getJSONObject(i);
sheets[i] = workbook.createSheet(jsonObject.optString("sheet_name", "no_name"));
}
IntStream.range(0, sheetArray.length()).parallel().forEach(i -> {
JSONObject jsonObject = sheetArray.getJSONObject(i);
JSONArray identifiers = jsonObject.getJSONArray("identifiers");
for (int j = 0; j < identifiers.length(); j++) {
sheets[i].setColumnWidth(j, 16 * 512);
}
CompletableFuture<Boolean> result = buildSheetAsync(workbook, sheets[i], jsonObject);
results.get().add(result);
});
// } else {
// sheetArray.toList().stream().parallel().forEach(sheetObject -> {
// HashMap<String, Object> sheetMap = (HashMap<String, Object>) sheetObject;
// JSONObject jsonObject = new JSONObject(sheetMap);
// XSSFSheet sheet = workbook.createSheet(jsonObject.optString("sheet_name", "no_name"));
// JSONArray identifiers = jsonObject.getJSONArray("identifiers");
// for (int j = 0; j < identifiers.length(); j++) {
// sheets[i].setColumnWidth(j, 16 * 512);
// sheet.setColumnWidth(j, 16 * 512);
// }
// CompletableFuture<Boolean> result = buildSheetAsync(workbook, sheets[i], jsonObject);
// CompletableFuture<Boolean> result = buildSheetAsync(workbook, sheet, jsonObject);
// results.get().add(result);
// });
CompletableFuture.allOf(results.get().toArray(new CompletableFuture[0])).join();
}
// CompletableFuture.allOf(results.get().toArray(new CompletableFuture[0])).join();
// }
}
}
......@@ -136,48 +127,48 @@ public class ExcelService {
}
@Async
public CompletableFuture<Boolean> buildSheetAsync(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) {
final XSSFCellStyle defaultStyle = createCellStyle(workbook, new JSONObject());
List<String> stylesSaved = new ArrayList<>();
List<XSSFCellStyle> styleObjectsSaved = new ArrayList<>();
XSSFRow row;
XSSFCell cell;
XSSFCellStyle cellStyle;
JSONArray identifiers = json.optJSONArray("identifiers", new JSONArray());
JSONArray data = json.optJSONArray("data", new JSONArray());
JSONArray styles = json.optJSONArray("styles", new JSONArray());
fillStylesArray(styles,workbook,stylesSaved,styleObjectsSaved);
for (int i = 0; i < data.length(); i++) {
row = sheet.createRow(i);
JSONObject row_data = data.optJSONObject(i);
for (int j = 0; j < identifiers.length(); j++) {
cell = row.createCell(j);
JSONArray styleArray = styles.optJSONArray(i, new JSONArray());
JSONObject style = styleArray.optJSONObject(j, new JSONObject());
if(style.isEmpty()){
cell.setCellStyle(defaultStyle);
}else{
if(!stylesSaved.contains(style.toString())){
stylesSaved.add(style.toString());
cellStyle = createCellStyle(workbook,style);
cell.setCellStyle(cellStyle);
styleObjectsSaved.add(cellStyle);
}else{
cell.setCellStyle(styleObjectsSaved.get(stylesSaved.indexOf(style.toString())));
}
}
cell.setCellValue(row_data.optString(identifiers.optString(j, "")));
}
}
return CompletableFuture.completedFuture(true);
}
// @Async
// public CompletableFuture<Boolean> buildSheetAsync(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) {
//
// final XSSFCellStyle defaultStyle = createCellStyle(workbook, new JSONObject());
// List<String> stylesSaved = new ArrayList<>();
// List<XSSFCellStyle> styleObjectsSaved = new ArrayList<>();
//
// XSSFRow row;
// XSSFCell cell;
// XSSFCellStyle cellStyle;
//
// JSONArray identifiers = json.optJSONArray("identifiers", new JSONArray());
// JSONArray data = json.optJSONArray("data", new JSONArray());
// JSONArray styles = json.optJSONArray("styles", new JSONArray());
//
// fillStylesArray(styles,workbook,stylesSaved,styleObjectsSaved);
//
// for (int i = 0; i < data.length(); i++) {
// row = sheet.createRow(i);
// JSONObject row_data = data.optJSONObject(i);
// for (int j = 0; j < identifiers.length(); j++) {
// cell = row.createCell(j);
// JSONArray styleArray = styles.optJSONArray(i, new JSONArray());
// JSONObject style = styleArray.optJSONObject(j, new JSONObject());
// if(style.isEmpty()){
// cell.setCellStyle(defaultStyle);
// }else{
// if(!stylesSaved.contains(style.toString())){
// stylesSaved.add(style.toString());
// cellStyle = createCellStyle(workbook,style);
// cell.setCellStyle(cellStyle);
// styleObjectsSaved.add(cellStyle);
// }else{
// cell.setCellStyle(styleObjectsSaved.get(stylesSaved.indexOf(style.toString())));
// }
// }
// cell.setCellValue(row_data.optString(identifiers.optString(j, "")));
// }
// }
//
// return CompletableFuture.completedFuture(true);
// }
private XSSFCellStyle createCellStyle(XSSFWorkbook workbook, JSONObject styleJson) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment