作为参考,我只想分享我为此实现的以下类。它采用给定的库和接口,制作n
副本并将 JNA 代理接口映射到每个副本,然后返回另一个实现线程安全锁定的代理接口,创建一个可重入的版本,并且可以运行最多处理器数量有。
public class LibraryReplicator<C> {
final BlockingQueue<C> libQueue;
final Class<C> interfaceClass;
final C proxiedInterface;
@SuppressWarnings("unchecked")
public LibraryReplicator(URL libraryResource, Class<C> interfaceClass, int copies) throws IOException {
if (!interfaceClass.isInterface())
throw new RuntimeException(interfaceClass + "is not a valid interface to map to the library.");
libQueue = new LinkedBlockingQueue<C>(copies);
this.interfaceClass = interfaceClass;
// Create copies of the file and map them to interfaces
String orig = libraryResource.getFile();
File origFile = new File(orig);
for( int i = 0; i < copies; i++ ) {
File copy = new File(orig + "." + i);
Files.copy(origFile, copy);
C libCopy = (C) Native.loadLibrary(copy.getPath(), interfaceClass);
libQueue.offer(libCopy); // This should never fail
}
proxiedInterface = (C) Proxy.newProxyInstance(
interfaceClass.getClassLoader(),
new Class[] { interfaceClass },
new BlockingInvocationHandler());
}
public LibraryReplicator(URL libraryResource, Class<C> interfaceClass) throws IOException {
this(libraryResource, interfaceClass, Runtime.getRuntime().availableProcessors());
}
public C getProxiedInterface() {
return proxiedInterface;
}
/*
* Invocation handler that uses the queue to grab locks and maintain thread safety.
*/
private class BlockingInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Exception {
C instance = null;
// Grab a copy of the library out of the queue
do {
try { instance = libQueue.take(); }
catch(InterruptedException e) {}
} while(instance == null);
// Invoke the method
Object result = method.invoke(instance, args);
// Return the library to the queue
while(true) {
try { libQueue.put(instance); break; }
catch( InterruptedException e ) {}
}
return result;
}
}
}
作为静态初始化程序的一部分,示例用法如下所示:
MvnPackGenz lib = new LibraryReplicator<MvnPackGenz>(
MvnPackGenz.class.getClassLoader().getResource("mvnpack.so"),
MvnPackGenz.class).getProxiedInterface();
这会创建一堆库的副本(在我的例子中是 12 个)创建lib
上面的变量“看起来”是可重入的,并且可以由多个线程安全地运行:
-rw-r--r-- 1 mao mao 50525 Sep 26 13:55 mvnpack.so
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.0
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.1
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.10
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.11
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.2
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.3
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.4
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.5
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.6
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.7
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.8
-rw-r--r-- 1 mao mao 50525 Sep 26 18:21 mvnpack.so.9
您可以在以下位置查看更新版本
https://github.com/mizzao/libmao/blob/master/src/main/java/net/andrewmao/misc/LibraryReplicator.java