Search code examples
javasocketscastingsocketchannel

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


In my app i have this code:

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

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

i have this error:

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

what is wrong with my cast ?

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;
    }
}

AND

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);
    }
}

Solution

  • Below is an excerpt from 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             }
    

    As you can see makeObject() is called on the provided instance of PoolableObjectFactory, so you should implement this method accordingly.

    Right now you only return an instance of Object, which - of course - cannot be casted to SocketChannel.

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