java.lang.Object 无法转换为 java.nio.channels.SocketChannel

java.lang.Object cannot be cast to java.nio.channels.SocketChannel

在我的应用程序中我有这个代码:

        SamplePoolableObjectFactory factory = new SamplePoolableObjectFactory();
        this.pool = new SampleObjectPool(factory);
.....

    SocketChannel channel = null;
            try {
    channel = (SocketChannel) this.pool.borrowObject();
....
}

我有这个错误:

Unable to borrow socket from pool java.lang.ClassCastException: java.lang.Object cannot be cast to java.nio.channels.SocketChannel

我的演员有什么问题吗?

public class SamplePoolableObjectFactory implements PoolableObjectFactory{
    private final static Log log = LogFactory.getLog(SamplePoolableObjectFactory.class);

    /* (non-Javadoc)
     * @see org.apache.commons.pool.PoolableObjectFactory#activateObject(java.lang.Object)
     */
    public void activateObject(Object obj) throws Exception {
        log.debug("Activate object ..." + obj.hashCode());
    }
    /* (non-Javadoc)
     * @see org.apache.commons.pool.PoolableObjectFactory#destroyObject(java.lang.Object)
     */
    public void destroyObject(Object obj) throws Exception {
        log.debug("Destroy object ..." + obj.hashCode());
        obj = null;
    }
    /* (non-Javadoc)
     * @see org.apache.commons.pool.PoolableObjectFactory#makeObject()
     */
    public Object makeObject() throws Exception {
        Object obj = new Object();
        log.debug("Make object ..." + obj.hashCode());
        return obj;
    }
    /* (non-Javadoc)
     * @see org.apache.commons.pool.PoolableObjectFactory#passivateObject(java.lang.Object)
     */
    public void passivateObject(Object obj) throws Exception {
        log.debug("Passivate object ..." + obj.hashCode());

    }
    /* (non-Javadoc)
     * @see org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang.Object)
     */
    public boolean validateObject(Object obj) {
        log.debug("Validate object ..." + obj.hashCode());
        return false;
    }
}

public class SampleObjectPool extends GenericObjectPool {
    private final static Log log = LogFactory.getLog(SampleObjectPool.class);


    /**
     * Constructor
     * @param factory
     */
    public SampleObjectPool(PoolableObjectFactory factory) {
        super(factory);
        log.debug("New pool created with factory ...");
    }

    /**
     * Constructor
     * @param factory
     * @param config
     */
    public SampleObjectPool(PoolableObjectFactory factory, Config config) {
        super(factory, config);
        log.debug("New pool created with factory and config ...");
    }

    /* (non-Javadoc)
     * @see org.apache.commons.pool.ObjectPool#addObject()
     */
    public void addObject() throws Exception {
        log.debug("Add an object ...");
        super.addObject();
    }
    /* (non-Javadoc)
     * @see org.apache.commons.pool.ObjectPool#borrowObject()
     */
    public Object borrowObject() throws Exception {
        log.debug("Borrow an object ...");
        return super.borrowObject();
    }
    /* (non-Javadoc)
     * @see org.apache.commons.pool.ObjectPool#invalidateObject(java.lang.Object)
     */
    public void invalidateObject(Object obj) throws Exception {
        log.debug("Invalidate an object ...");
        super.invalidateObject(obj);
    }
    /* (non-Javadoc)
     * @see org.apache.commons.pool.ObjectPool#returnObject(java.lang.Object)
     */
    public void returnObject(Object obj) throws Exception {
        log.debug("Return an object ...");
        super.returnObject(obj);
    }
}

您的 public Object makeObject() 创建了一个 Object。您正在尝试将其转换为 SocketChannel.

就像猫不是狗一样,Objects 也不是 SocketChannels,无论您如何施放它们。

以下摘自 GenericObjectPool.borrowObject()

836             // create new object when needed
837             boolean newlyCreated = false;
838             if(null == pair) {
839                 try {
840                     Object obj = _factory.makeObject();
841                     pair = new ObjectTimestampPair(obj);
842                     newlyCreated = true;
843                 } finally {
844                     if (!newlyCreated) {
845                         // object cannot be created
846                         _numActive--;
847                         notifyAll();
848                     }
849                 }
850             }

如您所见,makeObject() 是在提供的 PoolableObjectFactory 实例上调用的,因此您应该相应地实现此方法。

现在你只有 return Object 的实例,当然 - 不能转换为 SocketChannel.

public Object makeObject() throws Exception {
    SocketChannel  obj = ... // create channel here
    log.debug("Make object ..." + obj.hashCode());
    return obj;
}