1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| @Data @Component @ConfigurationProperties(prefix = "ice") public class Client { private String adapterName; private String host; private int port;
private Logger _logger = LoggerFactory.getLogger(Client.class);
public Result execute(CommandBody command) { Ice.Communicator ic = null; try { ic = Ice.Util.initialize(); Ice.ObjectPrx base = ic.stringToProxy(getStringProxy()); ZKRoadRangeAdminPrx interfacePrx = ZKRoadRangeAdminPrxHelper.checkedCast(base); if (interfacePrx == null) { return new Result(false, "Invalid proxy"); } Result result = executeCommand(command, interfacePrx); if (result == null) { return new Result(false, "暂无此操作命令"); } return result; } catch (Exception e) { _logger.info(e.getMessage(), e); return new Result(false, "连接错误!" + e); } finally { if (ic != null) { ic.destroy(); } } }
private Result executeCommand(CommandBody command, ZKRoadRangeAdminPrx interfacePrx) { CommandType type = command.getCommandType(); if (type.equals(CommandType.addRange)) { return returnMessage(interfacePrx.AddRange(command.getZkRoadRange())); } else if (type.equals(CommandType.updateRange)) { return returnMessage(interfacePrx.UpdateRange(command.getZkRoadRange())); } else if (type.equals(CommandType.removeRange)) { return returnMessage(interfacePrx.RemoveRange(command.getZkRoadRange().code)); } else if (type.equals(CommandType.getRange)) { return new Result(true, JSONObject.toJSONString(interfacePrx.GetRange(command.getZkRoadRange().code))); } else if (type.equals(CommandType.listRanges)) { return new Result(true, JSONObject.toJSONString(interfacePrx.ListRanges())); } return null; }
private String getStringProxy() { return adapterName + ":tcp -h " + host + " -p " + port; }
private Result returnMessage(boolean result) { return result ? new Result(true, "success") : new Result(false, "failure"); }
}
|