Skip to content

Commit 19c1af8

Browse files
kun-songralf0131
authored andcommitted
[Enhancement] Replace explicit resource management with try-with-resource (#3281)
1 parent fd16a2b commit 19c1af8

File tree

13 files changed

+61
-160
lines changed

13 files changed

+61
-160
lines changed

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -593,21 +593,12 @@ private void resolveFile() {
593593
}
594594
if (resolveFile != null && resolveFile.length() > 0) {
595595
Properties properties = new Properties();
596-
FileInputStream fis = null;
597-
try {
598-
fis = new FileInputStream(new File(resolveFile));
596+
try (FileInputStream fis = new FileInputStream(new File(resolveFile))) {
599597
properties.load(fis);
600598
} catch (IOException e) {
601599
throw new IllegalStateException("Failed to load " + resolveFile + ", cause: " + e.getMessage(), e);
602-
} finally {
603-
try {
604-
if (null != fis) {
605-
fis.close();
606-
}
607-
} catch (IOException e) {
608-
logger.warn(e.getMessage(), e);
609-
}
610600
}
601+
611602
resolve = properties.getProperty(interfaceName);
612603
}
613604
}

dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,11 @@ private String findConfigedHosts(ProtocolConfig protocolConfig, List<URL> regist
626626
// skip multicast registry since we cannot connect to it via Socket
627627
continue;
628628
}
629-
try {
630-
Socket socket = new Socket();
631-
try {
632-
SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort());
633-
socket.connect(addr, 1000);
634-
hostToBind = socket.getLocalAddress().getHostAddress();
635-
break;
636-
} finally {
637-
try {
638-
socket.close();
639-
} catch (Throwable e) {
640-
}
641-
}
629+
try (Socket socket = new Socket()) {
630+
SocketAddress addr = new InetSocketAddress(registryURL.getHost(), registryURL.getPort());
631+
socket.connect(addr, 1000);
632+
hostToBind = socket.getLocalAddress().getHostAddress();
633+
break;
642634
} catch (Exception e) {
643635
logger.warn(e.getMessage(), e);
644636
}

dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/DataSourceStatusChecker.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,20 @@ public Status check() {
6767
buf.append(", ");
6868
}
6969
buf.append(entry.getKey());
70-
try {
71-
Connection connection = dataSource.getConnection();
72-
try {
73-
DatabaseMetaData metaData = connection.getMetaData();
74-
ResultSet resultSet = metaData.getTypeInfo();
75-
try {
76-
if (!resultSet.next()) {
77-
level = Status.Level.ERROR;
78-
}
79-
} finally {
80-
resultSet.close();
70+
71+
try (Connection connection = dataSource.getConnection()) {
72+
DatabaseMetaData metaData = connection.getMetaData();
73+
try (ResultSet resultSet = metaData.getTypeInfo()) {
74+
if (!resultSet.next()) {
75+
level = Status.Level.ERROR;
8176
}
82-
buf.append(metaData.getURL());
83-
buf.append("(");
84-
buf.append(metaData.getDatabaseProductName());
85-
buf.append("-");
86-
buf.append(metaData.getDatabaseProductVersion());
87-
buf.append(")");
88-
} finally {
89-
connection.close();
9077
}
78+
buf.append(metaData.getURL());
79+
buf.append("(");
80+
buf.append(metaData.getDatabaseProductName());
81+
buf.append("-");
82+
buf.append(metaData.getDatabaseProductVersion());
83+
buf.append(")");
9184
} catch (Throwable e) {
9285
logger.warn(e.getMessage(), e);
9386
return new Status(level, e.getMessage());

dubbo-metadata-report/dubbo-metadata-report-api/src/main/java/org/apache/dubbo/metadata/support/AbstractMetadataReport.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,23 @@ private void doSaveProperties(long version) {
134134
if (!lockfile.exists()) {
135135
lockfile.createNewFile();
136136
}
137-
RandomAccessFile raf = new RandomAccessFile(lockfile, "rw");
138-
try {
139-
try (FileChannel channel = raf.getChannel()) {
140-
FileLock lock = channel.tryLock();
141-
if (lock == null) {
142-
throw new IOException("Can not lock the metadataReport cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.metadata.file=xxx.properties");
137+
try (RandomAccessFile raf = new RandomAccessFile(lockfile, "rw");
138+
FileChannel channel = raf.getChannel()) {
139+
FileLock lock = channel.tryLock();
140+
if (lock == null) {
141+
throw new IOException("Can not lock the metadataReport cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.metadata.file=xxx.properties");
142+
}
143+
// Save
144+
try {
145+
if (!file.exists()) {
146+
file.createNewFile();
143147
}
144-
// Save
145-
try {
146-
if (!file.exists()) {
147-
file.createNewFile();
148-
}
149-
try (FileOutputStream outputFile = new FileOutputStream(file)) {
150-
properties.store(outputFile, "Dubbo metadataReport Cache");
151-
}
152-
} finally {
153-
lock.release();
148+
try (FileOutputStream outputFile = new FileOutputStream(file)) {
149+
properties.store(outputFile, "Dubbo metadataReport Cache");
154150
}
151+
} finally {
152+
lock.release();
155153
}
156-
} finally {
157-
raf.close();
158154
}
159155
} catch (Throwable e) {
160156
if (version < lastCacheChanged.get()) {
@@ -168,23 +164,13 @@ private void doSaveProperties(long version) {
168164

169165
void loadProperties() {
170166
if (file != null && file.exists()) {
171-
InputStream in = null;
172-
try {
173-
in = new FileInputStream(file);
167+
try (InputStream in = new FileInputStream(file)) {
174168
properties.load(in);
175169
if (logger.isInfoEnabled()) {
176170
logger.info("Load service store file " + file + ", data: " + properties);
177171
}
178172
} catch (Throwable e) {
179173
logger.warn("Failed to load service store file " + file, e);
180-
} finally {
181-
if (in != null) {
182-
try {
183-
in.close();
184-
} catch (IOException e) {
185-
logger.warn(e.getMessage(), e);
186-
}
187-
}
188174
}
189175
}
190176
}

dubbo-metadata-report/dubbo-metadata-report-api/src/test/java/org/apache/dubbo/metadata/integration/MetadataReportServiceTest.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,7 @@ public class MetadataReportServiceTest {
4040

4141
@BeforeEach
4242
public void before() {
43-
44-
metadataReportService1 = MetadataReportService.instance(new Supplier<URL>() {
45-
@Override
46-
public URL get() {
47-
return url;
48-
}
49-
});
43+
metadataReportService1 = MetadataReportService.instance(() -> url);
5044
}
5145

5246
@Test

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TKv.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ public String rendering() {
5959

6060
private String filterEmptyLine(String content) {
6161
final StringBuilder sb = new StringBuilder();
62-
Scanner scanner = null;
63-
try {
64-
scanner = new Scanner(content);
62+
try (Scanner scanner = new Scanner(content)) {
6563
while (scanner.hasNextLine()) {
6664
String line = scanner.nextLine();
6765
if (line != null) {
@@ -73,10 +71,6 @@ private String filterEmptyLine(String content) {
7371
}
7472
sb.append(line).append(System.lineSeparator());
7573
}
76-
} finally {
77-
if (null != scanner) {
78-
scanner.close();
79-
}
8074
}
8175

8276
return sb.toString();

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTable.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,10 @@ private static String replaceTab(String string) {
419419
*/
420420
private static int width(String string) {
421421
int maxWidth = 0;
422-
final Scanner scanner = new Scanner(new StringReader(string));
423-
try {
422+
try (Scanner scanner = new Scanner(new StringReader(string))) {
424423
while (scanner.hasNextLine()) {
425424
maxWidth = max(length(scanner.nextLine()), maxWidth);
426425
}
427-
} finally {
428-
scanner.close();
429426
}
430427
return maxWidth;
431428
}

dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/textui/TTree.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,21 @@ public void callback(int deep, boolean isLast, String prefix, Node node) {
7878
treeSB.append(costPrefix);
7979
}
8080

81-
final Scanner scanner = new Scanner(new StringReader(node.data.toString()));
82-
try {
81+
try (Scanner scanner = new Scanner(new StringReader(node.data.toString()))) {
8382
boolean isFirst = true;
8483
while (scanner.hasNextLine()) {
8584
if (isFirst) {
8685
treeSB.append(scanner.nextLine()).append("\n");
8786
isFirst = false;
8887
} else {
89-
treeSB
90-
.append(prefix)
88+
treeSB.append(prefix)
9189
.append(repeat(' ', stepStringLength))
9290
.append(hasChild ? "|" : EMPTY)
9391
.append(repeat(' ', costPrefixLength))
9492
.append(scanner.nextLine())
9593
.append(System.lineSeparator());
9694
}
9795
}
98-
} finally {
99-
scanner.close();
10096
}
10197

10298
}

dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/support/AbstractRegistry.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -155,33 +155,23 @@ public void doSaveProperties(long version) {
155155
if (!lockfile.exists()) {
156156
lockfile.createNewFile();
157157
}
158-
RandomAccessFile raf = new RandomAccessFile(lockfile, "rw");
159-
try {
160-
FileChannel channel = raf.getChannel();
158+
try (RandomAccessFile raf = new RandomAccessFile(lockfile, "rw");
159+
FileChannel channel = raf.getChannel()) {
160+
FileLock lock = channel.tryLock();
161+
if (lock == null) {
162+
throw new IOException("Can not lock the registry cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties");
163+
}
164+
// Save
161165
try {
162-
FileLock lock = channel.tryLock();
163-
if (lock == null) {
164-
throw new IOException("Can not lock the registry cache file " + file.getAbsolutePath() + ", ignore and retry later, maybe multi java process use the file, please config: dubbo.registry.file=xxx.properties");
166+
if (!file.exists()) {
167+
file.createNewFile();
165168
}
166-
// Save
167-
try {
168-
if (!file.exists()) {
169-
file.createNewFile();
170-
}
171-
FileOutputStream outputFile = new FileOutputStream(file);
172-
try {
173-
properties.store(outputFile, "Dubbo Registry Cache");
174-
} finally {
175-
outputFile.close();
176-
}
177-
} finally {
178-
lock.release();
169+
try (FileOutputStream outputFile = new FileOutputStream(file)) {
170+
properties.store(outputFile, "Dubbo Registry Cache");
179171
}
180172
} finally {
181-
channel.close();
173+
lock.release();
182174
}
183-
} finally {
184-
raf.close();
185175
}
186176
} catch (Throwable e) {
187177
if (version < lastCacheChanged.get()) {

dubbo-registry/dubbo-registry-multicast/src/main/java/org/apache/dubbo/registry/multicast/MulticastRegistry.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,33 +167,15 @@ private boolean isExpired(URL url) {
167167
if (!url.getParameter(Constants.DYNAMIC_KEY, true) || url.getPort() <= 0 || Constants.CONSUMER_PROTOCOL.equals(url.getProtocol()) || Constants.ROUTE_PROTOCOL.equals(url.getProtocol()) || Constants.OVERRIDE_PROTOCOL.equals(url.getProtocol())) {
168168
return false;
169169
}
170-
Socket socket = null;
171-
try {
172-
socket = new Socket(url.getHost(), url.getPort());
170+
try (Socket socket = new Socket(url.getHost(), url.getPort())) {
173171
} catch (Throwable e) {
174172
try {
175173
Thread.sleep(100);
176174
} catch (Throwable e2) {
177175
}
178-
Socket socket2 = null;
179-
try {
180-
socket2 = new Socket(url.getHost(), url.getPort());
176+
try (Socket socket2 = new Socket(url.getHost(), url.getPort())) {
181177
} catch (Throwable e2) {
182178
return true;
183-
} finally {
184-
if (socket2 != null) {
185-
try {
186-
socket2.close();
187-
} catch (Throwable e2) {
188-
}
189-
}
190-
}
191-
} finally {
192-
if (socket != null) {
193-
try {
194-
socket.close();
195-
} catch (Throwable e) {
196-
}
197179
}
198180
}
199181
return false;

0 commit comments

Comments
 (0)