Reputation: 620
I have the following problem: i want to make a list of Cassandra templates starting from a list of sessions. However the problem is that at runtime i get that the session oject is null. The code i use is the following:
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Component;
@Component
public class KeyspaceSession {
@Autowired
@Qualifier("cluster")
CassandraClusterFactoryBean cluster;
@Autowired
@Qualifier("converter")
CassandraConverter converter;
public List<CassandraOperations> getTemplates(){
List<CassandraOperations> listOfTemplates = new ArrayList<>();
for(CassandraKeyspaces keyspace : CassandraKeyspaces.values()){
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster.getObject());
session.setKeyspaceName(keyspace.getKeyspace());
session.setConverter(converter);
session.setSchemaAction(SchemaAction.CREATE_IF_NOT_EXISTS);
CassandraOperations op = new CassandraTemplate(session.getObject());
listOfTemplates.add(op);
}
return listOfTemplates;
}
}
I get the following answer from the server:
{
"timestamp": 1505204641753,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.IllegalArgumentException",
"message": "Session must not be null",
"path": "/getUser/10/pl"
}
Upvotes: 0
Views: 1315
Reputation: 18129
CassandraSessionFactoryBean
requires initialization after you've set all properties. Call CassandraSessionFactoryBean.afterPropertiesSet()
before obtaining the Session
. You also need to clean up sessions on application shutdown.
Ideally, you would register BeanDefinition
s for each Session
and CassandraOperations
bean you want to create.
References:
Upvotes: 1
Reputation: 2134
I haven't worked with Spring before so forgive me if I'm missing something. But I don't see that you set any contact points so how will the driver know what cluster to query?
Try this:
cluster.setContactPoints(<cassandra_server_ip>);
Upvotes: 0