parent
3355d16f28
commit
7e3458ee7a
@ -0,0 +1,52 @@
|
|||||||
|
package com.bsmlab.dfx.agent.task;
|
||||||
|
|
||||||
|
import com.bsmlab.dfx.agent.config.Settings;
|
||||||
|
import com.bsmlab.dfx.agent.config.constant.MessageType;
|
||||||
|
import com.bsmlab.dfx.agent.config.datasource.SqlExecuteService;
|
||||||
|
import com.bsmlab.dfx.agent.listener.dto.ReceiveMessageDto;
|
||||||
|
import com.bsmlab.dfx.agent.task.dropbox.DropBoxDto;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.EnumUtils;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class TaskExecutorService {
|
||||||
|
private final Settings settings;
|
||||||
|
private final SqlExecuteService sqlExecuteService;
|
||||||
|
|
||||||
|
@Async("threadPoolTaskExecutor")
|
||||||
|
public void process(String messageFilePath) {
|
||||||
|
try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(messageFilePath))) {
|
||||||
|
ReceiveMessageDto receiveMessageDto = (ReceiveMessageDto)objectInputStream.readObject();
|
||||||
|
DropBoxDto dropBoxDto = settings.getDropBoxDto(receiveMessageDto.getRecipientDropBoxId());
|
||||||
|
if("SAVE_DATA_TABLE".equals(dropBoxDto.getTaskType())) {
|
||||||
|
//public Map<String, Object> insert(String dataSourceId, String sqlId, Map<String, Object> parameter) {
|
||||||
|
String dataString = receiveMessageDto.getData();
|
||||||
|
List<Map<String, Object>> dataMapList = null; //(List<Map<String, Object>>)receiveMessageDto.getData();
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
dataMapList = (List<Map<String, Object>>) objectMapper.readValue(receiveMessageDto.getData(), List.class);
|
||||||
|
for(Map<String, Object> dataMap : dataMapList) {
|
||||||
|
sqlExecuteService.insert(dropBoxDto.getDataSourceId(), dropBoxDto.getSqlId(), dataMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if("RECEIVE_FILE".equals(dropBoxDto.getTaskType())) {
|
||||||
|
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (ClassNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
package com.bsmlab.dfx.agent.task;
|
||||||
|
|
||||||
|
import com.bsmlab.dfx.agent.task.dropbox.DropBoxService;
|
||||||
|
import io.micrometer.common.util.StringUtils;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class TaskExecutorStarter {
|
||||||
|
private final DropBoxService dropBoxService;
|
||||||
|
private final TaskExecutorService taskExecutorService;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void run() {
|
||||||
|
while(true) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(10);
|
||||||
|
String messageFilePath = dropBoxService.poll();
|
||||||
|
if(StringUtils.isNotBlank(messageFilePath)) {
|
||||||
|
taskExecutorService.process(messageFilePath);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("{}", e, e);
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
package com.bsmlab.dfx.agent.task.dropbox;
|
||||||
|
|
||||||
|
import com.bsmlab.dfx.agent.config.Settings;
|
||||||
|
import com.bsmlab.dfx.agent.listener.dto.ReceiveMessageDto;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
|
public class DropBoxService {
|
||||||
|
private final Settings settings;
|
||||||
|
private ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<>();
|
||||||
|
|
||||||
|
public boolean isExistToday(ReceiveMessageDto receiveMessageDto) {
|
||||||
|
boolean isExist = false;
|
||||||
|
File root = new File(settings.getMessageStorageRootPath());
|
||||||
|
Date today = new Date(System.currentTimeMillis());
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd/");
|
||||||
|
String todayDirectoryString = root.getAbsolutePath() + "/" + dateFormat.format(today);
|
||||||
|
DecimalFormat decimalFormat = new DecimalFormat("00");
|
||||||
|
for(int i = 0; i < 24; i++) {
|
||||||
|
File targetFilePath = new File(todayDirectoryString + decimalFormat.format(i) + "/" + receiveMessageDto.getMessageUuid());
|
||||||
|
isExist = targetFilePath.exists();
|
||||||
|
if(isExist) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return isExist;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(ReceiveMessageDto receiveMessageDto) {
|
||||||
|
File root = new File(settings.getMessageStorageRootPath());
|
||||||
|
Date today = new Date(System.currentTimeMillis());
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd/hh");
|
||||||
|
String targetDirectoryString = root.getAbsolutePath() + "/" + dateFormat.format(today);
|
||||||
|
File targetDirectory = new File(targetDirectoryString);
|
||||||
|
if(!targetDirectory.exists()) {
|
||||||
|
targetDirectory.mkdirs();
|
||||||
|
}
|
||||||
|
String targetFilePath = targetDirectoryString + "/" + receiveMessageDto.getMessageUuid();
|
||||||
|
try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(targetFilePath)))) {
|
||||||
|
objectOutputStream.writeObject(receiveMessageDto);
|
||||||
|
this.queue.add(targetFilePath);
|
||||||
|
}
|
||||||
|
catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String poll() {
|
||||||
|
return queue.poll();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue