Java Code Examples for com.hazelcast.core.IMap#compute()
The following examples show how to use
com.hazelcast.core.IMap#compute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MatchDetailHandler.java From match-trade with Apache License 2.0 | 6 votes |
/** * @Title: inputMatchDepth 有事务处理 * @Description: TODO(买入队列) * @param input 入单 * @return void 返回类型 * @throws */ public void inputMatchDepth(MatchOrder input) { // TransactionOptions options = new TransactionOptions().setTransactionType(TransactionOptions.TransactionType.ONE_PHASE); // TransactionContext context = hzInstance.newTransactionContext(options); // context.beginTransaction(); try { IMap<BigDecimal, BigDecimal> map = hzInstance.getMap(HazelcastUtil.getMatchKey(input.getCoinTeam(), input.getIsBuy())); IMap<Long, MatchOrder> order_map = hzInstance.getMap(HazelcastUtil.getOrderBookKey(input.getCoinTeam(), input.getIsBuy())); map.compute(input.getPrice(),(k, v) -> HazelcastUtil.numberAdd(v, input.getUnFinishNumber())); input.setList(null);//清空吃单记录,减小内存。 order_map.put(input.getId(), input); //context.commitTransaction();//提交事务 } catch (Exception e) { //context.rollbackTransaction(); log.error("===入单数据处理异常,数据原型:"+input.toJsonString()+" 本次异常:"+e); } }
Example 2
Source File: OrderData.java From match-trade with Apache License 2.0 | 6 votes |
/** * @Title: new_order * @Description: TODO(接收撤销订单数据) * @param 参数 * @return void 返回类型 * @throws */ @KafkaListener(id = "cancel_order", topics = "cancel_order") public void cancel_order(String param) { log.info("===收到cancel_order:"+param); CancelOrderParam cancel = JSON.parseObject(param, CancelOrderParam.class); IMap<Long, MatchOrder> order_map = hzInstance.getMap(HazelcastUtil.getOrderBookKey(cancel.getCoinTeam(), cancel.getIsBuy())); if (order_map.containsKey(cancel.getId())) { TransactionOptions options = new TransactionOptions().setTransactionType(TransactionOptions.TransactionType.ONE_PHASE); TransactionContext context = hzInstance.newTransactionContext(options); context.beginTransaction(); try { IMap<BigDecimal, BigDecimal> map = hzInstance.getMap(HazelcastUtil.getMatchKey(cancel.getCoinTeam(), cancel.getIsBuy())); MatchOrder cmo = order_map.remove(cancel.getId()); map.compute(cmo.getPrice(), (k,v) -> v.subtract(cmo.getUnFinishNumber())); if (map.get(cmo.getPrice()).compareTo(BigDecimal.ZERO) >-1) { context.commitTransaction(); pushData.updateOrder(cmo); //推送撤销成功结果 }else { throw new Exception(); } } catch (Exception e) { context.rollbackTransaction(); } } }
Example 3
Source File: MatchExecutor.java From match-trade with Apache License 2.0 | 4 votes |
public MatchOrder doMatch(MatchOrder input) { try { // 获取对手盘口 IMap<BigDecimal, BigDecimal> outMap = hzInstance.getMap(HazelcastUtil.getMatchKey(input.getCoinTeam(), !input.getIsBuy())); if (null!=outMap&&outMap.size()>0) { BigDecimal outPrice = HazelcastUtil.getOptimalMatch(outMap,input.getIsBuy()); if (HazelcastUtil.canMatch(input, outPrice)) { BigDecimal outNum = outMap.get(outPrice); if (outNum.compareTo(BigDecimal.ZERO)<1) { outMap.remove(outPrice); doMatch(input); // 递归处理 } int contrast = input.getUnFinishNumber().compareTo(outNum); BigDecimal dealNum = contrast > -1?outNum:input.getUnFinishNumber(); input.setFinishNumber(input.getFinishNumber().add(dealNum)); input.setUnFinishNumber(input.getUnFinishNumber().subtract(dealNum)); if (input.getIsBuy()) { input.setSurplusFrozen(input.getSurplusFrozen().subtract(outPrice.multiply(dealNum))); }else { input.setSurplusFrozen(input.getSurplusFrozen().subtract(dealNum)); } //撮合详情记录 >>> 一个价格一个详情 matchDetailHandler.sendTradeRecord(input, outPrice, dealNum, DealWay.TAKER); List<LevelMatch> lms = input.getList(); LevelMatch lm = new LevelMatch(outPrice, dealNum); lm.setEatUp(contrast > -1 ?true:false); lms.add(lm); input.setList(lms); if (contrast == 1) {//水平价格被吃完 outMap.remove(outPrice); input.setState(OrderState.PART.value); doMatch(input); // 递归处理 }else if (contrast == 0) {//都被吃完 outMap.remove(outPrice); input.setState(OrderState.ALL.value); }else {//水平价格有剩余 outMap.compute(outPrice, (k,v) -> v.subtract(dealNum)); input.setState(OrderState.ALL.value); } } } } catch (Exception e) { log.error("执行撮合错误:"+e); input.setState(3);//撤销掉 } return input; }