我写了一些Java钱包生成代码,我用它来生成加密货币钱包。提供了代码,
public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) {
final WalletInfo walletInfo = new WalletInfo();
String walletName = generateWallet.getWalletName();
String currencyName = generateWallet.getCurrencyName();
WalletInfo walletInfoDb = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
if (walletInfoDb == null && genWalletMap.get(walletName) == null) {
String currency = currencyName.toUpperCase();
if (currency.equals("BITCOIN")) {
final WalletManager walletManager = WalletManager.setupWallet(walletName);
walletManager.addWalletSetupCompletedListener((wallet) -> {
Address address = wallet.currentReceiveAddress();
WalletInfo newWallet = createWalletInfo(walletName, currencyName, address.toString());
// set the properties of the walletInfo
walletInfo.setId(newWallet.getId());
walletInfo.setName(newWallet.getName());
walletInfo.setAddress(newWallet.getAddress());
walletInfo.setCurrency(newWallet.getCurrency());
walletMangersMap.put(newWallet.getId(), walletManager);
genWalletMap.remove(walletName);
});
genWalletMap.put(walletName, walletManager);
return walletInfo;
} else if (currency.equals("ETHEREUM")) {
return walletInfo;
} else {
return walletInfo;
}
}
return walletInfo;
}
当我使用cURL执行POST请求时,
curl -H "Content-Type: application/json" -X POST -d '{"walletName": "Florence8","currencyName":"Bitcoin"}' http://localhost:8080/rest/wallet/generateAddress
我得到null是返回,
{
"id" : null,
"name" : null,
"address" : null,
"currency" : null
}
实体生成后仍然存在于MySQL中。我一直在调试,这是有线的。调试不遵循从上到下的代码序列。调试的顺序就像,
我想说的是代码是否来到这一行
walletManager.addWalletSetupCompletedListener((wallet)
,那么它应该执行里面的操作。
有任何建议如何在数据库中正确保留后恢复该实体?如果需要,我可以提供更多信息
更新
正如答案中所建议的,我使用CountDownLatch
并解决问题
public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) {
CountDownLatch finshedSetup = new CountDownLatch(1);
// some code
}
终端输出
答案 0 :(得分:1)
看起来你正处于竞争状态。您正在设置回调以填写walletInfo
,但可能会在回调执行之前返回walletInfo
。
某些类型的承诺或锁定在返回之前等待可能会有所帮助。例如:
public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) {
final WalletInfo walletInfo = new WalletInfo();
String walletName = generateWallet.getWalletName();
String currencyName = generateWallet.getCurrencyName();
WalletInfo walletInfoDb = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName);
if (walletInfoDb == null && genWalletMap.get(walletName) == null) {
String currency = currencyName.toUpperCase();
if (currency.equals("BITCOIN")) {
final WalletManager walletManager = WalletManager.setupWallet(walletName);
CountDownLatch finishedSetup = new CountDownLatch(1);
walletManager.addWalletSetupCompletedListener((wallet) -> {
Address address = wallet.currentReceiveAddress();
WalletInfo newWallet = createWalletInfo(walletName, currencyName, address.toString());
// set the properties of the walletInfo
walletInfo.setId(newWallet.getId());
walletInfo.setName(newWallet.getName());
walletInfo.setAddress(newWallet.getAddress());
walletInfo.setCurrency(newWallet.getCurrency());
walletMangersMap.put(newWallet.getId(), walletManager);
genWalletMap.remove(walletName);
finshedSetup.countDown();
});
genWalletMap.put(walletName, walletManager);
finishedSetup.await();
return walletInfo;
} else if (currency.equals("ETHEREUM")) {
return walletInfo;
} else {
return walletInfo;
}
}
return walletInfo;
}