새로운 장난감 인 JCC 2.21을 가지고 놀고 있으며 파이썬 스크립트에서 콜백을 구현하는 데 문제가 있습니다. 다음 간단한 자바 스레드 API 래핑 된 및 파이썬 2.7 (CPython)에서 호출하고 있지만 JccTest.addJccTestListener(JccTestListener)
메서드를 호출 할 때 JVM null 인수를보고합니다.PyLucene JCC : Python으로 Java 인터페이스를 구현하고이를 통해 자바 스레드 콜백을 수신합니다.
import jcc_test
import time, sys
jcc_test.initVM(jcc_test.CLASSPATH)
test = jcc_test.JccTest()
class MyListener(jcc_test.JccTestListener):
def __init__(self):
pass
def message(self, msg):
print msg
test.addJccTestListener(MyListener())
test.start()
time.sleep(10)
test.stop()
sys.exit(0)
결과 :
"python.exe" jcc_test_test.py
Traceback (most recent call last):
File "jcc_test_test.py", line 16, in <module>
test.addJccTestListener(MyListener())
jcc_test.JavaError: java.lang.IllegalArgumentException: argument must be non-null
Java stacktrace:
java.lang.IllegalArgumentException: argument must be non-null
at com.example.jcc.JccTest.addJccTestListener(JccTest.java:32)
python -m jcc --jar jcc-test.jar --python jcc_test --build --install
그리고는 (JccTest
의 주요 방법에 해당)이 스크립트를 실행 :로
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class JccTest implements Runnable {
private final Object listenersLock = new Object();
private final List<JccTestListener> listeners = new ArrayList<JccTestListener>();
private final AtomicBoolean running = new AtomicBoolean(false);
private final AtomicBoolean finished = new AtomicBoolean(false);
public void start() {
if (running.compareAndSet(false, true)) {
new Thread(this).start();
}
}
public void stop() {
finished.set(true);
}
public void addJccTestListener(JccTestListener l) {
if (l == null) {
throw new IllegalArgumentException("argument must be non-null");
}
synchronized (listenersLock) {
listeners.add(l);
}
}
public void removeJccTestListener(JccTestListener l) {
synchronized (listenersLock) {
listeners.remove(l);
}
}
@Override
public void run() {
System.out.println("Start");
while (!finished.get()) {
System.out.println("Notifiying listeners");
synchronized (listenersLock) {
for (JccTestListener l : listeners) {
System.out.println("Notifiying " + String.valueOf(l));
l.message("I'm giving you a message!");
}
}
System.out.println("Sleeping");
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
continue;
}
}
running.set(false);
System.out.println("Stop");
}
public static void main(String[] args) throws InterruptedException {
JccTest test = new JccTest();
test.addJccTestListener(new JccTestListener() {
@Override
public void message(String msg) {
// called from another thread
System.out.println(msg);
}
});
test.start();
Thread.sleep(10000);
test.stop();
}
}
public interface JccTestListener {
public void message(String msg);
}
생성 랩퍼
null 리스너 인스턴스 외에도 CPython에서도 이와 같은 작업을 수행하고 있습니까? 그 구현에서 단 한 스레드가 파이썬 스크립트를 한 번에 실행할 수 있다는 것을 읽었습니다.이 스크립트는 (?) 나를 위해 문제가 될 수 있습니다. 자이 썬에서 이런 일을하는 것은 사소한 일이었다.
저는 다소 파이썬에 익숙하지 않으므로 친절하십시오.