我能够使代码正常工作,并且调用似乎按我的预期工作。但是,我想知道我是否在实现关于对象同步的最佳实践。问题是IB Api将回调发送到EWrapper接口中定义的方法,并且检索数据存在延迟。因此,我创建了一个singleton组件,以暂停Transaction控制器中方法的执行,以防止在更新帐户数据之前对其进行读取。但是,这感觉有些骇人听闻。因此,我特别想知道处理这种情况的最佳方法是什么,以及我所做的工作是否足够。
package com.vicentex.jdai.controllers;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ib.client.Contract;
import com.ib.client.Order;
import com.vicentex.jdai.models.AccountSummary;
import com.vicentex.jdai.models.ExecutionState;
import com.vicentex.jdai.models.MarketOrderEntry;
import com.vicentex.jdai.services.IBCommander;
// TODO - Implement JWT Token Strategy
@RestController
public class TransactionController {
@Autowired ExecutionState executionState;
@Autowired IBCommander commander;
@RequestMapping(value ="/createMarketOrder", method = RequestMethod.POST)
public Contract testOrder(HttpServletRequest request, @RequestBody MarketOrderEntry payload) {
Contract contract = this.commander.createStockContract(payload.getStock(), "STK", "USD", "SMART", "ISLAND");
Order order = this.commander.createMarketOrder(payload.getQuantity(), payload.getDirection());
this.commander.placeOrder(contract, order);
return contract;
}
@RequestMapping(value ="/getTotalCashValue", method = RequestMethod.GET)
public AccountSummary getTotalCashValue() throws InterruptedException {
this.commander.getTotalCashValue();
synchronized (this.executionState) {
while (this.executionState.isRequestCompleted() == false) {
executionState.wait(100);
}
return this.commander.getAccountSummary();
}
}
@RequestMapping(value ="/getSettledCash", method = RequestMethod.GET)
public AccountSummary getSettledCash() throws InterruptedException {
this.commander.getSettledCash();
synchronized (this.executionState) {
while (this.executionState.isRequestCompleted() == false) {
executionState.wait(100);
}
return this.commander.getAccountSummary();
}
}
@RequestMapping(value ="/getBuyingPower", method = RequestMethod.GET)
public AccountSummary getBuyingPower() throws InterruptedException {
this.commander.getBuyingPower();
synchronized (this.executionState) {
while (this.executionState.isRequestCompleted() == false) {
executionState.wait(100);
}
return this.commander.getAccountSummary();
}
}
}