我有以下代码,用于观察目录和创建一个线程启动的文件,它应该读取一个CSV文件并将其保存在一个对象中,之后我使用保存的数据在对象中根据config.proprties文件中定义的某些策略从新目录中的CSV文件中对数据进行排序,当我将一些文件粘贴到正在观看的目录中时,它会一个接一个地处理,但我想要的是什么是让它们同时运行,除非我有多个文件使用相同的策略可以有人建议我做错了什么?
这是我的Watcher课程:
Oracle Plug-in: Executing SQL statement: SELECT * FROM (SELECT ROW_NUMBER()
OVER ( ORDER BY "Example"."Example"
DESC,"DETAILS"."DETAILS" DESC) AS
Ranking,"Example"."FIRST_NAME" AS "FIRST_NAME",
"DETAILS"."LAST_NAME" AS "LAST_NAME",
"DETAILS"."CONTACT_ID" AS "RepID",
"DETAILS"."DETAIL_NOTES_NUM" AS "DETAIL_NOTES_NUM",
"DETAILS"."TYPE" AS "IMG",
"DETAILS"."EMAIL" AS "img",
"DETAILS"."EMAIL" AS "EMAIL",
"DETAILS"."NEWS_DT" AS "NEWS_DT",
"DETAILS"."FIRM_NAME" AS "FIRM_NAME",
"DETAILS"."DETAIL_NOTES" AS "DETAIL_NOTES",
"DETAILS"."CITY" AS "CITY",
"DETAILS"."STATE" AS "STATE" FROM
"DETAILS" "DETAILS" LEFT JOIN
"DETAILS"."Example" "YXISMOBILE.MV_VMW_USERREPRELTN" ON
"DETAILS"."CONTACT_ID" =
"DETAILS_USERREPRELTN"."REPID" WHERE (
"DETAILS_USERREPRELTN"."USERID" = 'clooby' ) AND (
"DETAILS"."TYPE" = 'LapsedPrdr' ) ) LimitingTable WHERE
Ranking <= 200
以下是用于读取属性文件并将其与已创建文件匹配的类:
Select substring(LogMsg, CHARINDEX('LapsedPrdr', logmsg) , CHARINDEX('))' ,logmsg)) as TYPE
这是我的策略实施类:
public class MyWatchService extends Thread {
private final Path path;
private PropertiesValue propertiesValue;
public MyWatchService(Path file) {
propertiesValue = new PropertiesValue(file.toString());
this.path = file;
}
public void doOnChange(String path) {
String fullPath = MyWatchService.this.path + "/" + path;
Thread thread = new Thread(() -> {
try {
propertiesValue.validatePatternAndCheckCase(path, fullPath);
} catch (Exception e) {
System.out.println("Do on changes in the dir: ");
e.printStackTrace();
}
});
thread.start();
}
@Override
public void run() {
try {
WatchService watchService = FileSystems.getDefault().newWatchService();
path.register(watchService, ENTRY_CREATE);
while (true)
checkForEvents(watchService);
} catch (Exception e) {
System.out.println("Main Thread: " + e);
}
}
private void checkForEvents(java.nio.file.WatchService watchService) {
try {
System.out.println("Waiting for events");
WatchKey watchKey = watchService.take();
List<WatchEvent<?>> events = watchKey.pollEvents();
events.forEach((e) -> getUpdates(e));
watchKey.reset();
} catch (InterruptedException e) {
System.out.println("Checking For Events: " + e);
}
}
private void getUpdates(WatchEvent<?> e) {
Path updatedPath = (Path) e.context();
doOnChange(updatedPath.toString());
}
}
当我运行代码时,我得到以下结果:
public class PropertiesValue {
private InputStream inputStream;
private Config config = new Config();
private String path;
ReadCSVFile a = new ReadCSVFile();
public PropertiesValue(String path) {
this.path = path;
}
private void checkInputStream(Properties properties, String fileName) throws IOException {
if (inputStream != null)
properties.load(inputStream);
else
throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
}
public void caseCheck(List<Transactions> listOfTrans) throws Exception {
StratgyImpl strategy = new StratgyImpl();
config = validatePattern(path);
String keys = config.getStrategy();
String path = config.getPath();
checkStrategy(listOfTrans, strategy, keys, path);
}
private void checkStrategy(List<Transactions> listOfTrans, StratgyImpl strategy, String keys, String path) throws Exception {
for (Transactions transaction : listOfTrans) {
if (keys.equals("1")) {
path = "/home/user/ATM/" + transaction.getIban() + "/";
strategy.imp(1).getDestPathForTransaction(path, transaction,
config);// .getDestPathForTransaction(path, t, config);
} else if (keys.equals("2")) {
path = "/home/user/OP/" + transaction.getCurrency() + "/";
strategy.imp(2).getDestPathForTransaction(path, transaction,
config);//byCurrency.getDestPathForTransaction(path, t, config);
} else if (keys.equals("3")) {
path = "/home/user/PA/" + transaction.getTransactionTime() + "/";
strategy.imp(3).getDestPathForTransaction(path, transaction, config);
} else if (keys.equals("4")) {
path = "/home/user/Transactions/" + transaction.getTransactionType() + "/";
strategy.imp(4).getDestPathForTransaction(path, transaction,
config);//byType.getDestPathForTransaction(path, t, config);
}
}
}
public Config validatePattern(String path) throws IOException {
try {
return getConfig(path);
} catch (Exception e) {
System.out.println("Validating Pattern Failed: " + e);
} finally {
inputStream.close();
}
System.out.println("This file does not match any pattern: " + path);
return null;
}
public synchronized void validatePatternAndCheckCase(String path, String fullPath) throws Exception {
validatePattern(path);
caseCheck(a.readFile(fullPath));
}
private Config getConfig(String path) throws IOException {
Properties properties;
String fileName;
Set<String> value;
properties = new Properties();
fileName = "config.properties";
inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
checkInputStream(properties, fileName);
value = properties.stringPropertyNames();
getStrategyAndPath(path, properties, value);
return config;
}
private void getStrategyAndPath(String path, Properties properties, Set<String> value) {
String key;
String[] strings;
for (String val : value) {
key = (String) properties.get(val);
strings = key.split(",");
if (path.matches(val)) {
config.setStrategy(strings[0]);
config.setPath(strings[1]);
}
}
}
}
虽然所有文件都有不同的策略来实现! 如果有人有兴趣帮助,请告诉我是否要查看更多代码:)
答案 0 :(得分:0)
由于validatePatternAndCheckCase()
已同步,因此一次只能有一个线程执行此方法。试图在另一个线程执行它时执行此方法的所有其他线程将被强制等待。由于这是所有线程都在调用的方法,因此一次只能执行一个线程,否定了使用线程进行并发执行的目的。