All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zalando.boot.cassandra.CassandraSessionFactoryBean Maven / Gradle / Ivy

There is a newer version: 2.2
Show newest version
package org.zalando.boot.cassandra;

import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;

import lombok.Getter;
import lombok.Setter;

/**
 * A factory bean that creates a single cassandra session.
 */
@Getter
@Setter
public class CassandraSessionFactoryBean extends AbstractFactoryBean {

    /**
     * cluster
     */
    private Cluster cluster;

    /**
     * keyspace
     */
    private String keyspace;
    
    /**
     * Creates a new CassandraSessionFactoryBean.
     */
    public CassandraSessionFactoryBean() {
        super();
    }

    /**
     * Creates a new CassandraSessionFactoryBean with the given cluster.
     * 
     * @param cluster
     *            the cassandra cluster
     */
    public CassandraSessionFactoryBean(Cluster cluster) {
        this.cluster = cluster;
    }

    /**
     * {@inheritDoc}
     * 
     * @see AbstractFactoryBean#createInstance()
     */
    @Override
    protected Session createInstance() throws Exception {
        Assert.notNull(cluster);

        Session session = null;
        if (StringUtils.hasText(keyspace)) {
            session = cluster.connect(keyspace);
        } else {
            session = cluster.connect();
        }
        return session;
    }

    /**
     * {@inheritDoc}
     * 
     * @see AbstractFactoryBean#destroyInstance(Object)
     */
    @Override
    protected void destroyInstance(Session instance) throws Exception {
        instance.close();
    }

    /**
     * {@inheritDoc}
     * 
     * @see AbstractFactoryBean#getObjectType()
     */
    @Override
    public Class getObjectType() {
        return Session.class;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy