implementation of the try-with-resource added in Java 7 to close the connection automatically
This commit is contained in:
parent
adc5f55b60
commit
018c3a9380
|
|
@ -5,12 +5,14 @@ import me.legrange.mikrotik.ApiConnection;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author gideon
|
* @author gideon
|
||||||
|
* @author clairtonluz
|
||||||
*/
|
*/
|
||||||
abstract class Example {
|
abstract class Example {
|
||||||
|
|
||||||
protected void connect() throws Exception {
|
protected ApiConnection connect() throws Exception {
|
||||||
con = ApiConnection.connect(Config.HOST, ApiConnection.DEFAULT_PORT, 2000);
|
con = ApiConnection.connect(Config.HOST, ApiConnection.DEFAULT_PORT, 2000);
|
||||||
con.login(Config.USERNAME, Config.PASSWORD);
|
con.login(Config.USERNAME, Config.PASSWORD);
|
||||||
|
return con;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void disconnect() throws Exception {
|
protected void disconnect() throws Exception {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package examples;
|
||||||
|
|
||||||
|
import me.legrange.mikrotik.ApiConnection;
|
||||||
|
import me.legrange.mikrotik.MikrotikApiException;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Example 10: The try-with-resources Statement
|
||||||
|
*
|
||||||
|
* @author clairtonluz
|
||||||
|
*/
|
||||||
|
public class Example10 extends Example {
|
||||||
|
|
||||||
|
public static void main(String... args) throws Exception {
|
||||||
|
Example10 ex = new Example10();
|
||||||
|
try (ApiConnection c = ex.connect()) {
|
||||||
|
ex.test();
|
||||||
|
}
|
||||||
|
System.out.printf("\nisConnected = %B",ex.con.isConnected());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void test() throws MikrotikApiException, InterruptedException {
|
||||||
|
List<Map<String, String>> res = con.execute("/interface/ethernet/print");
|
||||||
|
for (Map<String, String> r : res) {
|
||||||
|
System.out.println(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package me.legrange.mikrotik;
|
package me.legrange.mikrotik;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import me.legrange.mikrotik.impl.ApiConnectionImpl;
|
import me.legrange.mikrotik.impl.ApiConnectionImpl;
|
||||||
|
|
@ -9,8 +10,9 @@ import me.legrange.mikrotik.impl.ApiConnectionImpl;
|
||||||
* Mikrotik and send commands to it.
|
* Mikrotik and send commands to it.
|
||||||
*
|
*
|
||||||
* @author GideonLeGrange
|
* @author GideonLeGrange
|
||||||
|
* @author clairtonluz
|
||||||
*/
|
*/
|
||||||
public abstract class ApiConnection {
|
public abstract class ApiConnection implements Closeable {
|
||||||
|
|
||||||
/** default TCP port used by Mikrotik API */
|
/** default TCP port used by Mikrotik API */
|
||||||
public static final int DEFAULT_PORT = 8728;
|
public static final int DEFAULT_PORT = 8728;
|
||||||
|
|
|
||||||
|
|
@ -200,6 +200,15 @@ public final class ApiConnectionImpl extends ApiConnection {
|
||||||
private Integer _tag = 0;
|
private Integer _tag = 0;
|
||||||
private int timeout = ApiConnection.DEFAULT_COMMAND_TIMEOUT;
|
private int timeout = ApiConnection.DEFAULT_COMMAND_TIMEOUT;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
try {
|
||||||
|
disconnect();
|
||||||
|
} catch (ApiConnectionException e) {
|
||||||
|
throw new IOException(e.getMessage(), e.getCause());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* thread to read data from the socket and process it into Strings
|
* thread to read data from the socket and process it into Strings
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue