Skip to content

Commit

Permalink
FMWK-283 Add DriverAction implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
reugn committed Dec 11, 2023
1 parent c3325ab commit 17967ae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/aerospike/jdbc/AerospikeDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class AerospikeDriver implements Driver {

static {
try {
java.sql.DriverManager.registerDriver(new AerospikeDriver());
java.sql.DriverManager.registerDriver(new AerospikeDriver(), new AerospikeDriverAction());
} catch (SQLException e) {
throw new ExceptionInInitializerError("Can not register AerospikeDriver");
}
Expand All @@ -48,8 +48,9 @@ public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
allInfo.add(new DriverPropertyInfo(kv[0], kv.length > 1 ? kv[1] : null));
});
}
allInfo.addAll(info.entrySet().stream().map(e -> new DriverPropertyInfo((String) e.getKey(),
(String) e.getValue())).collect(toList()));
allInfo.addAll(info.entrySet().stream()
.map(e -> new DriverPropertyInfo((String) e.getKey(), (String) e.getValue()))
.collect(toList()));
return allInfo.toArray(new DriverPropertyInfo[0]);
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/aerospike/jdbc/AerospikeDriverAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.aerospike.jdbc;

import com.aerospike.jdbc.async.EventLoopProvider;

import java.sql.DriverAction;
import java.util.logging.Logger;

public class AerospikeDriverAction implements DriverAction {

private static final Logger logger = Logger.getLogger(AerospikeDriverAction.class.getName());

@Override
public void deregister() {
logger.info("Deregister AerospikeDriver");
EventLoopProvider.close();
}
}
28 changes: 24 additions & 4 deletions src/main/java/com/aerospike/jdbc/async/EventLoopProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import com.aerospike.client.async.NettyEventLoops;
import io.netty.channel.nio.NioEventLoopGroup;

import java.util.logging.Logger;

import static java.util.Objects.requireNonNull;

public final class EventLoopProvider {

private static EventLoops eventLoops;
private static final Logger logger = Logger.getLogger(EventLoopProvider.class.getName());
private static volatile EventLoops eventLoops;

private EventLoopProvider() {
}
Expand All @@ -25,11 +28,28 @@ public static EventLoops getEventLoops() {
return eventLoops;
}

public static synchronized void close() {
if (null != eventLoops) {
logger.info(() -> "Close eventLoops");
eventLoops.close();
eventLoops = null;
}
}

private static void initEventLoops() {
if (null == eventLoops) {
int nThreads = Math.max(2, Runtime.getRuntime().availableProcessors());
eventLoops = new NettyEventLoops(new EventPolicy(), new NioEventLoopGroup(nThreads));
requireNonNull(eventLoops.get(0));
synchronized (EventLoopProvider.class) {
if (null == eventLoops) {
logger.info(() -> "Init eventLoops");
int nThreads = Math.max(2, Runtime.getRuntime().availableProcessors());
EventLoops nettyEventLoops = new NettyEventLoops(
new EventPolicy(),
new NioEventLoopGroup(nThreads)
);
requireNonNull(nettyEventLoops.get(0));
eventLoops = nettyEventLoops;
}
}
}
}
}

0 comments on commit 17967ae

Please sign in to comment.