2017-03-02 61 views
0

在我的應用程序我有這樣的代碼: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); 
    } 
} 
+2

沒有什麼不對您的演員。你的對象工廠只是不返回'SocketChannel'對象,而是正常的'java.lang.Object's。 – Kayaman

+0

@Kayaman我廠是這個樣子'公共類SamplePoolableObjectFactory實現PoolableObjectFactory' – Mercer

+0

那麼爲什麼它返回'Object's? – Kayaman

回答

2

下面是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所提供的實例,因此你應該相應地實現此方法。

現在你只返回一個Object的實例,當然這個實例不能被鑄造成SocketChannel

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

public Object makeObject()創建一個Object。您正試圖將其轉換爲SocketChannel

就像貓不是狗,Objects不是SocketChannels無論你試圖投它們多少。

+0

我該怎麼做才能創造一個'GenericObjectPool <的SocketChannel>' – Mercer

+0

您可以通過實現這樣做'makeObject()'在你的工廠。看看下面的答案。 – mike

相關問題