8

我在 Java 中有一个问题,我用 JMX 接口设置了一个动态代理,将它传递给另一个组件,然后调用代理对象。当我这样做时,应用程序会为每个调用泄漏两个线程,这些线程似乎永远不会超时,并且会不断累积,直到应用程序内存不足。

线程成对出现,请参阅底部的堆栈跟踪。

我尝试在 JMX 中使用一些稍微模糊的系统属性来关闭超时,但这并没有什么不同。关键操作似乎是动态代理调用。通过代理调用的对象实现了 Serializable ,所以这应该不是问题。

当我手动创建一个带有 MBean 路径和对象接口字符串的 Bean 并从中调用方法时,问题就消失了。

当涉及到动态代理时,我主要在这里寻找经典的陷阱,因为我对它们没有太多经验。

这就是代理实例的创建方式

public <T> T create(final Class<T> type,
        final Object... nameParameters) throws JmxConnectionException {
    return type.cast(Proxy.newProxyInstance(
            type.getClassLoader(),
            new Class< ? >[] {type},
            new MyInvocationHandler(this,
                    fill(nameOf(type), nameParameters))));
}

以及 MyInvocationHandler 的实现:

final class MyInvocationHandler implements InvocationHandler, Serializable {
private static final long serialVersionUID = 0L; //actually a proper random long
private final transient ProxyFactory proxyFactory;
private String mBeanName;
private RemoteObject remoteObject;

MyInvocationHandler(final ProxyFactory proxyFactory,
        final String mBeanName) {
    this.proxyFactory = proxyFactory;
    this.mBeanName = mBeanName;
}

private void writeObject(final ObjectOutputStream out)
throws IOException {
    try {
        checkConnected();
    } catch (final JmxConnectionException e) {
        throw new IOException(e);
    }
    out.writeObject(mBeanName);
    out.writeObject(remoteObject);
}

private void readObject(final ObjectInputStream in)
throws IOException, ClassNotFoundException {
    mBeanName = (String) in.readObject();
    remoteObject = (RemoteObject) in.readObject();
}

public Object invoke(final Object proxy, final Method method,
        final Object[] args) throws Throwable {
    checkConnected(); //Just checks that the RemoteObject isn't null.
    try {
        return invokeMethod(method, args); // Calls the method on the remoteObject with the arguments, code cut.
    } catch (final InvocationTargetException e) {
        throw e.getCause();
    }
}

}

两个线程的线程堆栈跟踪(总是成对出现):

Name: JMX server connection timeout 53
State: TIMED_WAITING on [I@18bbe70
Total blocked: 3  Total waited: 4

Stack trace: 
java.lang.Object.wait(Native Method)
com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(ServerCommunicatorAdmin.java:150)
java.lang.Thread.run(Thread.java:619)

Name: Thread-21
State: TIMED_WAITING
Total blocked: 0  Total waited: 1

Stack trace: 
java.lang.Thread.sleep(Native Method)
com.sun.jmx.remote.internal.ClientCommunicatorAdmin$Checker.run(ClientCommunicatorAdmin.java:154)
java.lang.Thread.run(Thread.java:619)
4

1 回答 1

13

问题已解决。在序列化 RemoteObject 下的对象期间出现此问题。

创建JMXConnector时,请确保在使用完后将其关闭,而不是将其留给垃圾收集,否则它们似乎会继续堆积...

JMXConnector connector = JMXConnectorFactory.connect(url);
//...
connector.close();
于 2009-02-19T16:19:51.467 回答