Commit 2db79aa5569fa1ea8530cb74b7726a37ad95347d

Authored by Mike Lohmann
Committed by Jan Christoph Bernack
1 parent 6b47046b

Issue #1686 Introduced ScheduledFuture in SessionMap to be able to cancel

deregisterSession call after successful responses in CoAP to avoid
5.03 responses after ACK.
... ... @@ -178,15 +178,24 @@ public abstract class AbstractTransportService implements TransportService {
178 178
179 179 @Override
180 180 public void registerSyncSession(TransportProtos.SessionInfoProto sessionInfo, SessionMsgListener listener, long timeout) {
181   - sessions.putIfAbsent(toId(sessionInfo), new SessionMetaData(sessionInfo, TransportProtos.SessionType.SYNC, listener));
182   - schedulerExecutor.schedule(() -> {
  181 + SessionMetaData currentSession = new SessionMetaData(sessionInfo, TransportProtos.SessionType.SYNC, listener);
  182 + sessions.putIfAbsent(toId(sessionInfo), currentSession);
  183 +
  184 + ScheduledFuture executorFuture = schedulerExecutor.schedule(() -> {
183 185 listener.onRemoteSessionCloseCommand(TransportProtos.SessionCloseNotificationProto.getDefaultInstance());
184 186 deregisterSession(sessionInfo);
185 187 }, timeout, TimeUnit.MILLISECONDS);
  188 +
  189 + currentSession.setScheduledFuture(executorFuture);
186 190 }
187 191
188 192 @Override
189 193 public void deregisterSession(TransportProtos.SessionInfoProto sessionInfo) {
  194 + SessionMetaData currentSession = sessions.get(toId(sessionInfo));
  195 + if (currentSession.hasScheduledFuture()) {
  196 + log.debug("Stopping scheduler to avoid resending response if request has been ack.");
  197 + currentSession.getScheduledFuture().cancel(false);
  198 + }
190 199 sessions.remove(toId(sessionInfo));
191 200 }
192 201
... ...
1 1 /**
2 2 * Copyright © 2016-2019 The Thingsboard Authors
3   - *
  3 + * <p>
4 4 * Licensed under the Apache License, Version 2.0 (the "License");
5 5 * you may not use this file except in compliance with the License.
6 6 * You may obtain a copy of the License at
7   - *
8   - * http://www.apache.org/licenses/LICENSE-2.0
9   - *
  7 + * <p>
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + * <p>
10 10 * Unless required by applicable law or agreed to in writing, software
11 11 * distributed under the License is distributed on an "AS IS" BASIS,
12 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
... ... @@ -19,6 +19,8 @@ import lombok.Data;
19 19 import org.thingsboard.server.common.transport.SessionMsgListener;
20 20 import org.thingsboard.server.gen.transport.TransportProtos;
21 21
  22 +import java.util.concurrent.ScheduledFuture;
  23 +
22 24 /**
23 25 * Created by ashvayka on 15.10.18.
24 26 */
... ... @@ -29,19 +31,33 @@ class SessionMetaData {
29 31 private final TransportProtos.SessionType sessionType;
30 32 private final SessionMsgListener listener;
31 33
  34 + private ScheduledFuture scheduledFuture;
  35 +
32 36 private volatile long lastActivityTime;
33 37 private volatile boolean subscribedToAttributes;
34 38 private volatile boolean subscribedToRPC;
35 39
36   - SessionMetaData(TransportProtos.SessionInfoProto sessionInfo, TransportProtos.SessionType sessionType, SessionMsgListener listener) {
  40 + SessionMetaData(
  41 + TransportProtos.SessionInfoProto sessionInfo,
  42 + TransportProtos.SessionType sessionType,
  43 + SessionMsgListener listener
  44 + ) {
37 45 this.sessionInfo = sessionInfo;
38 46 this.sessionType = sessionType;
39 47 this.listener = listener;
40 48 this.lastActivityTime = System.currentTimeMillis();
  49 + this.scheduledFuture = null;
41 50 }
42 51
43 52 void updateLastActivityTime() {
44 53 this.lastActivityTime = System.currentTimeMillis();
45 54 }
46 55
  56 + void setScheduledFuture(ScheduledFuture scheduledFuture) { this.scheduledFuture = scheduledFuture; }
  57 +
  58 + public ScheduledFuture getScheduledFuture() {
  59 + return scheduledFuture;
  60 + }
  61 +
  62 + public boolean hasScheduledFuture() { return null != this.scheduledFuture; }
47 63 }
... ...