Two main updates:

Astyanax dependency version updated
Keyspace reference is now unique

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/portal/social-networking-library@121704 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2015-12-09 10:51:23 +00:00
parent 567713d064
commit eda8fce8f6
2 changed files with 36 additions and 18 deletions

View File

@ -45,6 +45,7 @@
<dependency> <dependency>
<groupId>com.netflix.astyanax</groupId> <groupId>com.netflix.astyanax</groupId>
<artifactId>astyanax</artifactId> <artifactId>astyanax</artifactId>
<version>1.56.26</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.gcube.resources.discovery</groupId> <groupId>org.gcube.resources.discovery</groupId>

View File

@ -14,6 +14,7 @@ import com.netflix.astyanax.connectionpool.NodeDiscoveryType;
import com.netflix.astyanax.connectionpool.OperationResult; import com.netflix.astyanax.connectionpool.OperationResult;
import com.netflix.astyanax.connectionpool.exceptions.ConnectionException; import com.netflix.astyanax.connectionpool.exceptions.ConnectionException;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl; import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType;
import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor; import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor;
import com.netflix.astyanax.ddl.ColumnDefinition; import com.netflix.astyanax.ddl.ColumnDefinition;
import com.netflix.astyanax.ddl.ColumnFamilyDefinition; import com.netflix.astyanax.ddl.ColumnFamilyDefinition;
@ -74,27 +75,43 @@ public class CassandraClusterConnection {
} }
/** /**
* * Get the reference to the current keyspace
* @return * @return keyspace reference
*/ */
public Keyspace getKeyspace() { public Keyspace getKeyspace() {
// The Keyspace instance can be shared among different requests
if(myKeyspace == null){
synchronized(this){
if(myKeyspace == null){ // double check lock
AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder() AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
.forCluster(clusterName) .forCluster(clusterName)
.forKeyspace(keyspaceName) .forKeyspace(keyspaceName)
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl() .withAstyanaxConfiguration(
.setDiscoveryType(NodeDiscoveryType.NONE) new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.NONE) // use only the host given as seeds (do not discover)
.setConnectionPoolType(ConnectionPoolType.ROUND_ROBIN) // how handle connections of the the connection pool
) )
.withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool") .withConnectionPoolConfiguration(
.setPort(9160) new ConnectionPoolConfigurationImpl("MyConnectionPool")
.setMaxConnsPerHost(1) .setMaxConnsPerHost(3) // for each seed(host)
//.setSocketTimeout(1000) -> default: 11 seconds
//.setConnectTimeout(1000) -> default: 2 seconds
.setSeeds(host) .setSeeds(host)
) )
.withConnectionPoolMonitor(new CountingConnectionPoolMonitor()) .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
.buildKeyspace(ThriftFamilyFactory.getInstance()); .buildKeyspace(ThriftFamilyFactory.getInstance());
context.start(); context.start();
// save keyspace reference
myKeyspace = context.getEntity(); myKeyspace = context.getEntity();
}
}
}
return myKeyspace; return myKeyspace;
} }