在我的應用程序我有這樣的代碼:java.lang.Object中不能被強制轉換爲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);
}
}
沒有什麼不對您的演員。你的對象工廠只是不返回'SocketChannel'對象,而是正常的'java.lang.Object's。 – Kayaman
@Kayaman我廠是這個樣子'公共類SamplePoolableObjectFactory實現PoolableObjectFactory' – Mercer
那麼爲什麼它返回'Object's? – Kayaman