我想为Spring应用程序创建一个同步文件写入机制。我大约有10000万个json,应该保存在单独的文件中,例如:
其他要求:
我创建了FileWriterProxy(单个bean),这是保存文件的主要组件。它加载负责写入文件的懒惰FileWriter组件(原型Bean)(具有同步的写入方法)。每个FileWriter对象代表一个单独的文件。 我怀疑我的解决方案不是线程安全。让我们考虑以下情形:
如果我错了,请纠正我。如何解决我的实施方案?
FileWriterProxy:
@Component
public class FileWriterProxy {
private final BeanFactory beanFactory;
private final Map<String, FileWriter> filePathsMappedToFileWriters = new ConcurrentHashMap<>();
public FileWriterProxy(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public void write(Path path, String data) {
FileWriter fileWriter = getFileWriter(path);
fileWriter.write(data);
removeFileWrite(path);
}
private FileWriter getFileWriter(Path path) {
return filePathsMappedToFileWriters.computeIfAbsent(path.toString(), e -> beanFactory.getBean(FileWriter.class, path));
}
private void removeFileWrite(Path path) {
filePathsMappedToFileWriters.remove(path.toString());
}
}
FileWriterProxyTest:
@RunWith(SpringRunner.class)
@SpringBootTest
public class FileWriterProxyTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private static final String FILE_NAME = "filename.txt";
private File baseDirectory;
private Path path;
@Autowired
private FileWriterProxy fileWriterProxy;
@Before
public void setUp() {
baseDirectory = temporaryFolder.getRoot();
path = Paths.get(baseDirectory.getAbsolutePath(), FILE_NAME);
}
@Test
public void writeToFile() throws IOException {
String data = "test";
fileWriterProxy.write(path, data);
String fileContent = new String(Files.readAllBytes(path));
assertEquals(data, fileContent);
}
@Test
public void concurrentWritesToFile() throws InterruptedException {
Path path = Paths.get(baseDirectory.getAbsolutePath(), FILE_NAME);
List<Task> tasks = Arrays.asList(
new Task(path, "test1"),
new Task(path, "test2"),
new Task(path, "test3"),
new Task(path, "test4"),
new Task(path, "test5"));
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<Boolean>> futures = executorService.invokeAll(tasks);
wait(futures);
}
@Test
public void manyRandomWritesToFiles() throws InterruptedException {
List<Task> tasks = createTasks(1000);
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<Boolean>> futures = executorService.invokeAll(tasks);
wait(futures);
}
private void wait(List<Future<Boolean>> tasksFutures) {
tasksFutures.forEach(e -> {
try {
e.get(10, TimeUnit.SECONDS);
} catch (Exception e1) {
e1.printStackTrace();
}
});
}
private List<Task> createTasks(int number) {
List<Task> tasks = new ArrayList<>();
IntStream.range(0, number).forEach(e -> {
String fileName = generateFileName();
Path path = Paths.get(baseDirectory.getAbsolutePath(), fileName);
tasks.add(new Task(path, "test"));
});
return tasks;
}
private String generateFileName() {
int length = 10;
boolean useLetters = true;
boolean useNumbers = false;
return RandomStringUtils.random(length, useLetters, useNumbers) + ".txt";
}
private class Task implements Callable<Boolean> {
private final Path path;
private final String data;
Task(Path path, String data) {
this.path = path;
this.data = data;
}
@Override
public Boolean call() {
fileWriterProxy.write(path, data);
return true;
}
}
}
配置:
@Configuration
public class Config {
@Bean
@Lazy
@Scope("prototype")
public FileWriter fileWriter(Path path) {
return new FileWriter(path);
}
}
FileWriter:
public class FileWriter {
private static final Logger logger = LoggerFactory.getLogger(FileWriter.class);
private final Path path;
public FileWriter(Path path) {
this.path = path;
}
public synchronized void write(String data) {
String filePath = path.toString();
try {
Files.write(path, data.getBytes());
logger.info("File has been saved: {}", filePath);
} catch (IOException e) {
logger.error("Error occurred while writing to file: {}", filePath);
}
}
}