繁体   English   中英

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转换为SocketChannel

就像猫不是狗一样,无论您尝试投射多少ObjectsObjects都不是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             }

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

现在,您仅返回Object的实例,该实例当然不能转换为SocketChannel

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM