我不知道如何正确地标题我的问题,因为我不确定这里有什么问题,所以我会发布我的代码并解释我在尝试什么实现..
我有一个正在运行并每秒发送一次事件的服务..我需要在该过程中进行少量检查,每次检查都会在一段时间后运行,例如..每一秒我都获得设备属性值,我每2秒钟取一次" currentBox",每5秒我检查一次网络连接,每小时我都试图登录..
这是代码:
private void startAutoUpdate() {
long current = System.currentTimeMillis();
if (!connectivityHelper.isConnectedMobile() && !connectivityHelper.isConnectedEthernet() && !connectivityHelper.isConnectedWifi()) {
eventBus.post(new Event(new ObjectConnectivity(connectivityHelper.isOnline(), null), Event.EVENT_TYPE_CONNECTIVITY_EVENT));
}
boolean localReset = false; // maybe force was set after this started!!
if (clearETags)
localReset = true;
try {
try {
if (localReset) {
attributeValueRepository.clearETag();
attributeValueRepository.clear();
attributeValueRepository.fetchMeteoAttr();
} else if (canUpdate(getResources().getInteger(R.integer.weather_update_delay)))
attributeValueRepository.fetchMeteoAttr();
} catch (Exception e) {
Log.d(TAG, "fail to load meteo attributes");
}
attributeValueRepository.fetchAll();
Log.d("AVTEST", "Fetch all");
try {
if (localReset) {
deviceStateRepository.clearETag();
deviceStateRepository.clear();
deviceStateRepository.fetchAll();
} else if (canUpdate(getResources().getInteger(R.integer.state_update_delay)))
deviceStateRepository.fetchAll();
} catch (Exception e) {
Log.d(TAG, "fail to load deviceStateRepository");
}
try {
if ((current - lastBoxCall) > 2000) {
currentBox = restTemplate.getRemoteOnlyCopy().getForObject("v2/box", Box.class);
if (currentBox != null) {
newSyncDate = df.format(currentBox.getSyncDate());
if (syncDate == null) {
syncDate = df.format(currentBox.getSyncDate());
} else {
if (!newSyncDate.equals(syncDate)) {
eventBus.post(new Event(null, Event.EVENT_TYPE_REFRESH_REQUEST));
syncDate = newSyncDate;
}
}
}
lastBoxCall = current;
Log.d("AVTEST", "Fetch current box");
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if ((current - lastNetworkCall) > 5000) {
if (connectivityHelper.isConnectedEthernet() || connectivityHelper.isConnectedWifi()) {
try {
int timeoutMs = 1500;
Socket sock = new Socket();
SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);
sock.connect(sockaddr, timeoutMs);
sock.close();
isOnline = true;
showNotification(true);
} catch (IOException e) {
isOnline = false;
showNotification(false);
}
connectivityHelper.setIsOnline(isOnline);
eventBus.post(new Event(isOnline, Event.EVENT_CONNECTION));
} else {
connectivityHelper.setIsOnline(false);
showNotification(false);
}
lastNetworkCall = current;
Log.d("AVTEST", "Check network connection");
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if ((current - lastLoginCall) > 3600000) {
restTemplate.login(restTemplate.getUsername(), restTemplate.getPassword(), restTemplate.getSerial());
}
lastLoginCall = current;
Log.d("AVTEST", "Relogin");
} catch (Exception e) {
Log.e(TAG, e.getMessage() != null ? e.getMessage() : "relogin error");
}
if (localReset) {
clearETags = false;
}
errorCount = 0;
previousCall = System.currentTimeMillis();
eventBus.post(new Event(null, Event.EVENT_TYPE_AUTO_UPDATER_SERVICE));
eventBus.post(new Event(new ObjectConnectivity(connectivityHelper.isOnline(), null), Event.EVENT_TYPE_CONNECTIVITY_EVENT));
} catch (Exception e) {
Log.d(TAG, "", e);
}
}
所以重点是,我的屏幕上的小部件应该在每个事件(每秒)之后更新,但由于某种原因它们有时会被延迟,所以我调试了它。我发现这部分导致了延迟:
try {
if ((current - lastLoginCall) > 3600000) {
restTemplate.login(restTemplate.getUsername(), restTemplate.getPassword(), restTemplate.getSerial());
}
lastLoginCall = current;
Log.d("AVTEST", "Relogin");
} catch (Exception e) {
Log.e(TAG, e.getMessage() != null ? e.getMessage() : "relogin error");
}
我在Android Monitor中查找了这个流程:
D/AVTEST: Fetch all
D/AVTEST: Fetch current box
D/AVTEST: Check network connection
D/AVTEST: Relogin
D/AVTEST: Home screen types update
D/AVTEST: Push logic: key: 2130968946, value: ViewHolder{3cec115 position=0 id=-1, oldPos=-1, pLpos:-1}
最后两个不是来自这项服务,但它现在并不重要,事情是,日志停止之前" Relogin"为15秒然后继续..是否计算这个东西太重或什么?
((current - lastLoginCall) > 3600000)
答案 0 :(得分:1)
你说日志在Relogin之前停止了15秒左右。我最好的猜测是它是由restTemplate.login调用引起的 - 记住你的" Relogin" log是在restTemplate调用之后。这是有道理的,因为它取决于网络连接性以及服务器对您的登录请求的响应速度。
(current - lastLogin)总是可以忽略不计。
答案 1 :(得分:0)
好像你在服务线程中执行网络调用,所以这段代码
restTemplate.login(restTemplate.getUsername(), restTemplate.getPassword(), restTemplate.getSerial());
执行需要花费很多时间。
要修复它,请在另一个线程中进行网络调用。
此外,也许您应该将日志调用移至时间条件:
if ((current - lastLoginCall) > 3600000) {
Log.d("AVTEST", "Relogin");
restTemplate.login(restTemplate.getUsername(), restTemplate.getPassword(), restTemplate.getSerial());
}