@PostMapping(value = "/devices/upload", produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<Map<String, String>> handleFileUpload(@RequestParam("file") MultipartFile file)
throws IOException {
LOGGER.info("DeviceController:handleFileUpload start");
String refId = String.valueOf(System.currentTimeMillis());
String fileName = file.getOriginalFilename();
Map<String, String> result = new HashMap<>();
result.put("refId", refId);
if (file.isEmpty()) {
result.put("msg", "Empty File: " + fileName);
return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
}
try (InputStream inStream = file.getInputStream()) {
List<String> lines = IOUtils.readLines(inStream, "UTF-8");
ExecutorService service = DMResourceManager.getExecutorService();
service.submit(() -> {
try {
deviceRegisterService.register(refId, fileName, lines);
} catch (FileNotFoundException | SQLException e) {
LOGGER.error("Error while calling deviceRegisterService's register method ", e);
}
});
result.put("msg", "Submitted File: " + fileName);
LOGGER.info("DeviceController:handleFileUpload end");
return new ResponseEntity<>(result, HttpStatus.OK);
}
}
public void register(String refId, String originalFileName, List<String> lines)
throws SQLException, FileNotFoundException {
LOGGER.info("DeviceRegisterService:register");
deviceRepository.uploadDevice(refId, originalFileName, lines);
}
public void uploadDevice(String refId, String originalFileName, List<String> lines) throws DataAccessException {
String sql = "INSERT INTO device_registration(Device_Id, Device_Name, Serial_Number, Device_Description, Device_Type, Manufacturer_Name, Manufactured_Date, Device_IMEI , Sim_Number , Sim_Type, Battery_Type, Hardware_Version, Base_Firmware_Version, Firmware_Filename, Config_Version, Config_Filename, Device_Mac_Address, Device_RFID_Info, RFID_Version, RFID_Filename, Status)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
for (int i = 1; i < lines.size() - 1; i++) {
String[] arrOfStr = lines.get(i).split(",");
for (int j = 0; j < arrOfStr.length; j = +arrOfStr.length) {
jdbcTemplate.update(sql, UUID.randomUUID().toString(), arrOfStr[0], arrOfStr[1], arrOfStr[2],
arrOfStr[3], arrOfStr[4], arrOfStr[5], arrOfStr[6], arrOfStr[7], arrOfStr[8], arrOfStr[9],
arrOfStr[10], arrOfStr[11], arrOfStr[12], arrOfStr[13], arrOfStr[14], arrOfStr[15],
arrOfStr[16], arrOfStr[17], arrOfStr[18], arrOfStr[19]);
}
}
LOGGER.info("reference Id generated after inserting " + originalFileName + " + file in db:" + refId);
}
@Component
public class DMResourceManager {
private final static Logger LOGGER = LoggerFactory.getLogger(DMResourceManager.class);
private final static ListeningExecutorService executorService =
MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10,
new ThreadFactoryBuilder().setNameFormat("dm-api-pool-%d").build()));
private DMResourceManager() {
}
public static ListeningExecutorService getExecutorService() {
return (executorService);
}
@PreDestroy
public void destroy() {
LOGGER.info("Attempting ExecutorService.shutdown()...");
executorService.shutdown();
try {
if (!executorService.awaitTermination(3, TimeUnit.SECONDS)) {
LOGGER.info("ExecutorService.shutdownNow() issued.");
executorService.shutdownNow();
if (!executorService.awaitTermination(3, TimeUnit.SECONDS)) {
LOGGER.warn("ExecutorService failed to shutdownNow() in 15 seconds.");
}
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
executorService.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
LOGGER.warn("ExecutorService interrupted during shutdown.", ie);
}
LOGGER.info("ExecutorService.shutdown() complete.");
}`enter code here`
}