0

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"
}
Charles
  • 570
  • 11
  • 29

2 Answers2

1

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 BeanDefinitions for each Session and CassandraOperations bean you want to create.

References:

mp911de
  • 17,546
  • 2
  • 55
  • 95
0

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>);
Simon Fontana Oscarsson
  • 2,114
  • 1
  • 17
  • 20
  • I have a configuration class that does that, this is why i am injecting the cluster and the converter: – Charles Sep 12 '17 at 08:57
  • The idea is that i want to generate the templates and the sessions dinamically and cycle through them and use the appropriate one depending on the requiest. So far i was only able to have more sessions by making multiple templates in the Configuration class however now i want to generate the session and the corresponding template dinamically by reading the keyspaces from a properties file. – Charles Sep 12 '17 at 09:10
  • Okay I understand better now. However this goes beyond my knowledge, hope someone else can help you. – Simon Fontana Oscarsson Sep 12 '17 at 09:28