Code review fixes - moved out .lock from try block

This commit is contained in:
Volodymyr Babak 2021-07-08 11:53:17 +03:00
parent 11a7fc68e3
commit 3319154513
6 changed files with 11 additions and 11 deletions

View File

@ -280,8 +280,8 @@ public final class EdgeGrpcSession implements Closeable {
private void sendDownlinkMsg(ResponseMsg downlinkMsg) { private void sendDownlinkMsg(ResponseMsg downlinkMsg) {
log.trace("[{}] Sending downlink msg [{}]", this.sessionId, downlinkMsg); log.trace("[{}] Sending downlink msg [{}]", this.sessionId, downlinkMsg);
if (isConnected()) { if (isConnected()) {
downlinkMsgLock.lock();
try { try {
downlinkMsgLock.lock();
outputStream.onNext(downlinkMsg); outputStream.onNext(downlinkMsg);
} catch (Exception e) { } catch (Exception e) {
log.error("[{}] Failed to send downlink message [{}]", this.sessionId, downlinkMsg, e); log.error("[{}] Failed to send downlink message [{}]", this.sessionId, downlinkMsg, e);
@ -345,8 +345,8 @@ public final class EdgeGrpcSession implements Closeable {
} }
private boolean sendDownlinkMsgsPack(List<DownlinkMsg> downlinkMsgsPack) throws InterruptedException { private boolean sendDownlinkMsgsPack(List<DownlinkMsg> downlinkMsgsPack) throws InterruptedException {
downlinkMsgsPackLock.lock();
try { try {
downlinkMsgsPackLock.lock();
boolean success; boolean success;
pendingMsgsMap.clear(); pendingMsgsMap.clear();
downlinkMsgsPack.forEach(msg -> pendingMsgsMap.put(msg.getDownlinkMsgId(), msg)); downlinkMsgsPack.forEach(msg -> pendingMsgsMap.put(msg.getDownlinkMsgId(), msg));

View File

@ -189,8 +189,8 @@ public class DeviceEdgeProcessor extends BaseEdgeProcessor {
private Device createDevice(TenantId tenantId, Edge edge, DeviceUpdateMsg deviceUpdateMsg, String deviceName) { private Device createDevice(TenantId tenantId, Edge edge, DeviceUpdateMsg deviceUpdateMsg, String deviceName) {
Device device; Device device;
deviceCreationLock.lock();
try { try {
deviceCreationLock.lock();
log.debug("[{}] Creating device entity [{}] from edge [{}]", tenantId, deviceUpdateMsg, edge.getName()); log.debug("[{}] Creating device entity [{}] from edge [{}]", tenantId, deviceUpdateMsg, edge.getName());
DeviceId deviceId = new DeviceId(new UUID(deviceUpdateMsg.getIdMSB(), deviceUpdateMsg.getIdLSB())); DeviceId deviceId = new DeviceId(new UUID(deviceUpdateMsg.getIdMSB(), deviceUpdateMsg.getIdLSB()));
device = deviceService.findDeviceById(tenantId, deviceId); device = deviceService.findDeviceById(tenantId, deviceId);

View File

@ -124,8 +124,8 @@ public abstract class AbstractNashornJsInvokeService extends AbstractJsInvokeSer
protected ListenableFuture<UUID> doEval(UUID scriptId, String functionName, String jsScript) { protected ListenableFuture<UUID> doEval(UUID scriptId, String functionName, String jsScript) {
jsPushedMsgs.incrementAndGet(); jsPushedMsgs.incrementAndGet();
ListenableFuture<UUID> result = jsExecutor.executeAsync(() -> { ListenableFuture<UUID> result = jsExecutor.executeAsync(() -> {
evalLock.lock();
try { try {
evalLock.lock();
try { try {
if (useJsSandbox()) { if (useJsSandbox()) {
sandbox.eval(jsScript); sandbox.eval(jsScript);

View File

@ -275,8 +275,8 @@ public class EdgeImitator {
private ListenableFuture<Void> saveDownlinkMsg(AbstractMessage message) { private ListenableFuture<Void> saveDownlinkMsg(AbstractMessage message) {
if (!ignoredTypes.contains(message.getClass())) { if (!ignoredTypes.contains(message.getClass())) {
lock.lock();
try { try {
lock.lock();
downlinkMsgs.add(message); downlinkMsgs.add(message);
} finally { } finally {
lock.unlock(); lock.unlock();
@ -312,8 +312,8 @@ public class EdgeImitator {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends AbstractMessage> Optional<T> findMessageByType(Class<T> tClass) { public <T extends AbstractMessage> Optional<T> findMessageByType(Class<T> tClass) {
Optional<T> result; Optional<T> result;
lock.lock();
try { try {
lock.lock();
result = (Optional<T>) downlinkMsgs.stream().filter(downlinkMsg -> downlinkMsg.getClass().isAssignableFrom(tClass)).findAny(); result = (Optional<T>) downlinkMsgs.stream().filter(downlinkMsg -> downlinkMsg.getClass().isAssignableFrom(tClass)).findAny();
} finally { } finally {
lock.unlock(); lock.unlock();
@ -324,8 +324,8 @@ public class EdgeImitator {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends AbstractMessage> List<T> findAllMessagesByType(Class<T> tClass) { public <T extends AbstractMessage> List<T> findAllMessagesByType(Class<T> tClass) {
List<T> result; List<T> result;
lock.lock();
try { try {
lock.lock();
result = (List<T>) downlinkMsgs.stream().filter(downlinkMsg -> downlinkMsg.getClass().isAssignableFrom(tClass)).collect(Collectors.toList()); result = (List<T>) downlinkMsgs.stream().filter(downlinkMsg -> downlinkMsg.getClass().isAssignableFrom(tClass)).collect(Collectors.toList());
} finally { } finally {
lock.unlock(); lock.unlock();

View File

@ -185,8 +185,8 @@ public class EdgeGrpcClient implements EdgeRpcClient {
@Override @Override
public void sendUplinkMsg(UplinkMsg msg) { public void sendUplinkMsg(UplinkMsg msg) {
uplinkMsgLock.lock();
try { try {
uplinkMsgLock.lock();
this.inputStream.onNext(RequestMsg.newBuilder() this.inputStream.onNext(RequestMsg.newBuilder()
.setMsgType(RequestMsgType.UPLINK_RPC_MESSAGE) .setMsgType(RequestMsgType.UPLINK_RPC_MESSAGE)
.setUplinkMsg(msg) .setUplinkMsg(msg)
@ -198,8 +198,8 @@ public class EdgeGrpcClient implements EdgeRpcClient {
@Override @Override
public void sendSyncRequestMsg(boolean syncRequired) { public void sendSyncRequestMsg(boolean syncRequired) {
uplinkMsgLock.lock();
try { try {
uplinkMsgLock.lock();
SyncRequestMsg syncRequestMsg = SyncRequestMsg.newBuilder().setSyncRequired(syncRequired).build(); SyncRequestMsg syncRequestMsg = SyncRequestMsg.newBuilder().setSyncRequired(syncRequired).build();
this.inputStream.onNext(RequestMsg.newBuilder() this.inputStream.onNext(RequestMsg.newBuilder()
.setMsgType(RequestMsgType.SYNC_REQUEST_RPC_MESSAGE) .setMsgType(RequestMsgType.SYNC_REQUEST_RPC_MESSAGE)
@ -212,8 +212,8 @@ public class EdgeGrpcClient implements EdgeRpcClient {
@Override @Override
public void sendDownlinkResponseMsg(DownlinkResponseMsg downlinkResponseMsg) { public void sendDownlinkResponseMsg(DownlinkResponseMsg downlinkResponseMsg) {
uplinkMsgLock.lock();
try { try {
uplinkMsgLock.lock();
this.inputStream.onNext(RequestMsg.newBuilder() this.inputStream.onNext(RequestMsg.newBuilder()
.setMsgType(RequestMsgType.UPLINK_RPC_MESSAGE) .setMsgType(RequestMsgType.UPLINK_RPC_MESSAGE)
.setDownlinkResponseMsg(downlinkResponseMsg) .setDownlinkResponseMsg(downlinkResponseMsg)

View File

@ -248,8 +248,8 @@ public class DeviceProfileServiceImpl extends AbstractEntityService implements D
log.trace("Executing findOrCreateDefaultDeviceProfile"); log.trace("Executing findOrCreateDefaultDeviceProfile");
DeviceProfile deviceProfile = findDeviceProfileByName(tenantId, name); DeviceProfile deviceProfile = findDeviceProfileByName(tenantId, name);
if (deviceProfile == null) { if (deviceProfile == null) {
findOrCreateLock.lock();
try { try {
findOrCreateLock.lock();
deviceProfile = findDeviceProfileByName(tenantId, name); deviceProfile = findDeviceProfileByName(tenantId, name);
if (deviceProfile == null) { if (deviceProfile == null) {
deviceProfile = this.doCreateDefaultDeviceProfile(tenantId, name, name.equals("default")); deviceProfile = this.doCreateDefaultDeviceProfile(tenantId, name, name.equals("default"));