Avoid throws Exception where possible - Phase III
This commit is contained in:
committed by
Artem Bilan
parent
b138ab80f8
commit
78199dca9b
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2018 the original author or authors.
|
||||
* Copyright 2014-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -247,7 +247,7 @@ public class LeaderInitiator implements SmartLifecycle {
|
||||
protected class LeaderListener extends LeaderSelectorListenerAdapter {
|
||||
|
||||
@Override
|
||||
public void takeLeadership(CuratorFramework framework) throws Exception {
|
||||
public void takeLeadership(CuratorFramework framework) {
|
||||
try {
|
||||
LeaderInitiator.this.candidate.onGranted(LeaderInitiator.this.context);
|
||||
if (LeaderInitiator.this.leaderEventPublisher != null) {
|
||||
@@ -265,7 +265,7 @@ public class LeaderInitiator implements SmartLifecycle {
|
||||
// candidate is no longer leader
|
||||
Thread.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
catch (@SuppressWarnings("unused") InterruptedException e) {
|
||||
// InterruptedException, like any other runtime exception,
|
||||
// is handled by the finally block below. No need to
|
||||
// reset the interrupt flag as the interrupt is handled.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2017 the original author or authors.
|
||||
* Copyright 2015-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,7 +19,6 @@ package org.springframework.integration.zookeeper.lock;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -157,7 +156,7 @@ public class ZookeeperLockRegistry implements ExpirableLockRegistry, DisposableB
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
public void destroy() {
|
||||
if (!this.mutexTaskExecutorExplicitlySet) {
|
||||
((ExecutorConfigurationSupport) this.mutexTaskExecutor).shutdown();
|
||||
}
|
||||
@@ -266,7 +265,7 @@ public class ZookeeperLockRegistry implements ExpirableLockRegistry, DisposableB
|
||||
try {
|
||||
return tryLock(1, TimeUnit.SECONDS);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
catch (@SuppressWarnings("unused") InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return false;
|
||||
}
|
||||
@@ -278,13 +277,13 @@ public class ZookeeperLockRegistry implements ExpirableLockRegistry, DisposableB
|
||||
try {
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
future = this.mutexTaskExecutor.submit(new Callable<Boolean>() {
|
||||
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
future = this.mutexTaskExecutor.submit(() -> {
|
||||
try {
|
||||
return ZkLock.this.client.checkExists().forPath("/") != null;
|
||||
}
|
||||
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
});
|
||||
|
||||
long waitTime = unit.toMillis(time);
|
||||
@@ -300,10 +299,16 @@ public class ZookeeperLockRegistry implements ExpirableLockRegistry, DisposableB
|
||||
return this.mutex.acquire(waitTime, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
catch (TimeoutException e) {
|
||||
future.cancel(true);
|
||||
catch (@SuppressWarnings("unused") TimeoutException e) {
|
||||
if (future != null) {
|
||||
future.cancel(true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw e;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessagingException("Failed to acquire mutex at " + this.path, e);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015-2018 the original author or authors.
|
||||
* Copyright 2015-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -48,6 +48,8 @@ import org.springframework.util.Assert;
|
||||
*/
|
||||
public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLifecycle {
|
||||
|
||||
private static final String UNUSED = "unused";
|
||||
|
||||
private final Object lifecycleMonitor = new Object();
|
||||
|
||||
private final CuratorFramework client;
|
||||
@@ -120,14 +122,15 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
|
||||
createNode(key, value);
|
||||
return null;
|
||||
}
|
||||
catch (KeeperException.NodeExistsException e) {
|
||||
catch (@SuppressWarnings(UNUSED) KeeperException.NodeExistsException e) {
|
||||
// so the data actually exists, we can read it
|
||||
try {
|
||||
byte[] bytes = this.client.getData().forPath(getPath(key));
|
||||
return IntegrationUtils.bytesToString(bytes, this.encoding);
|
||||
}
|
||||
catch (Exception exceptionDuringGet) {
|
||||
throw new ZookeeperMetadataStoreException("Exception while reading node with key '" + key + "':", e);
|
||||
throw new ZookeeperMetadataStoreException("Exception while reading node with key '" + key + "':",
|
||||
exceptionDuringGet);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
@@ -150,16 +153,16 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (KeeperException.NoNodeException e) {
|
||||
catch (@SuppressWarnings(UNUSED) KeeperException.NoNodeException e) {
|
||||
// ignore, the node doesn't exist there's nothing to replace
|
||||
return false;
|
||||
}
|
||||
catch (KeeperException.BadVersionException e) {
|
||||
catch (@SuppressWarnings(UNUSED) KeeperException.BadVersionException e) {
|
||||
// ignore
|
||||
return false;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new ZookeeperMetadataStoreException("Cannot replace value");
|
||||
throw new ZookeeperMetadataStoreException("Cannot replace value", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -186,7 +189,7 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
|
||||
try {
|
||||
createNode(key, value);
|
||||
}
|
||||
catch (KeeperException.NodeExistsException e) {
|
||||
catch (@SuppressWarnings(UNUSED) KeeperException.NodeExistsException e) {
|
||||
updateNode(key, value, -1);
|
||||
}
|
||||
}
|
||||
@@ -240,7 +243,7 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
|
||||
this.updateMap.put(key, new LocalChildData(null, Integer.MAX_VALUE));
|
||||
return IntegrationUtils.bytesToString(bytes, this.encoding);
|
||||
}
|
||||
catch (KeeperException.NoNodeException e) {
|
||||
catch (@SuppressWarnings(UNUSED) KeeperException.NoNodeException e) {
|
||||
// ignore - the node doesn't exist
|
||||
return null;
|
||||
}
|
||||
@@ -250,13 +253,13 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
|
||||
}
|
||||
}
|
||||
|
||||
private void updateNode(String key, String value, int version) throws Exception {
|
||||
private void updateNode(String key, String value, int version) throws Exception { // NOSONAR external lib throws
|
||||
Stat stat = this.client.setData().withVersion(version).forPath(getPath(key),
|
||||
IntegrationUtils.stringToBytes(value, this.encoding));
|
||||
this.updateMap.put(key, new LocalChildData(value, stat.getVersion()));
|
||||
}
|
||||
|
||||
private void createNode(String key, String value) throws Exception {
|
||||
private void createNode(String key, String value) throws Exception { // NOSONAR external lib throws
|
||||
this.client.create().forPath(getPath(key), IntegrationUtils.stringToBytes(value, this.encoding));
|
||||
this.updateMap.put(key, new LocalChildData(value, 0));
|
||||
}
|
||||
@@ -359,7 +362,7 @@ public class ZookeeperMetadataStore implements ListenableMetadataStore, SmartLif
|
||||
}
|
||||
|
||||
@Override
|
||||
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
|
||||
public void childEvent(CuratorFramework framework, PathChildrenCacheEvent event) {
|
||||
synchronized (ZookeeperMetadataStore.this.updateMap) {
|
||||
String eventPath = event.getData().getPath();
|
||||
String eventKey = getKey(eventPath);
|
||||
|
||||
Reference in New Issue
Block a user