在我的应用中,我有这段代码:
valueChanges
我有这个错误:
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;
}
}
答案 0 :(得分:2)
您的response = emr.run_job_flow(
Name="start-my-cluster",
ReleaseLabel="emr-5.3.1",
LogUri='s3://logs',
Instances={
'InstanceGroups': [
{'Name': 'EmrMaster',
'InstanceRole': 'MASTER',
'InstanceType': 'm3.xlarge',
'InstanceCount': 1},
{'Name': 'EmrCore',
'InstanceRole': 'CORE',
'InstanceType': 'm3.xlarge',
'InstanceCount': 2}
],
'Ec2KeyName': 'my-key-name',
'KeepJobFlowAliveWhenNoSteps' : True,
},
Applications=[{'Name': 'Hadoop'}, {'Name': 'Spark'}, {'Name': 'Hive'}],
JobFlowRole='EMR_EC2_DefaultRole',
ServiceRole='EMR_DefaultRole',
VisibleToAllUsers=True,
Steps=[
# steps go here...
]
)
创建了public Object makeObject()
。您正试图将其转换为Object
。
就像猫不是狗一样,SocketChannel
不是Objects
,无论你多少尝试投射它们。
答案 1 :(得分: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;
}