Announcement Announcement Module
Collapse
No announcement yet.
Configure complex object in xml to inject. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configure complex object in xml to inject.

    --------------------------------------------------------------------
    AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
    .forCluster("ClusterName")
    .forKeyspace("KeyspaceName")
    .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl("MyConnectionPool" )
    .setPort(9160)
    .setSeeds("127.0.0.1:9160")
    )
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
    .buildKeyspace(ThriftFamilyFactory.getInstance());

    context.start();
    Keyspace keyspace = context.getEntity();
    --------------------------------------------------------------------
    I need to inject Keyspace instance in spring and need to configure it in applicationContext.xml.
    Can anyone tell me how to do it? it is really complex. thanks.

  • #2
    This is straight forward. Read the spring documentation on FactoryBean for details.

    You need to create a FactoryBean for this. Roughly the class should look something like this

    Code:
    public class KeyspaceFactoryBean<Keyspace> implements FactoryBean<Keyspace> {
    private String clusterName;
    private String keyspaceName;
    private String connectionPoolName;
    private int portNo;
    private String seeds;
    public String getSeeds() {
    return seeds;
    }
    public void setSeeds(String seeds) {
    this.seeds = seeds;
    }
    public int getPortNo() {
    return portNo;
    }
    public void setPortNo(int portNo) {
    this.portNo = portNo;
    }
    public void setConnectionPoolName(String name) {
    this.connectionPoolName = name;
    }
    public String getConnectionPoolName() {
    return this.connectionPoolName;
    }
    public String getKeyspaceName() {
    return this.keyspaceName;
    }
    public void setKeyspaceName(String name) {
    this.keyspaceName = name;
    }
    public void setClusterName(String name) {
    this.clusterName = name;
    }
    public String getClusterName() {
    return this.clusterName;
    }
    public Keyspace getObject() throws Exception {
    AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
    .forCluster(clusterName)
    .forKeyspace(keyspaceName)
    .withConnectionPoolConfiguration(new ConnectionPoolConfigurationImpl(connectionPoolName )
    .setPort(portNo)
    .setSeeds(seeds)
    )
    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
    .buildKeyspace(ThriftFamilyFactory.getInstance());
    
    context.start();
    Keyspace keyspace = context.getEntity();
    return keyspace;
    }
    }
    now this can be declared inside your XML Configuration like this

    Code:
    <bean id="keyspace" class="KeyspaceFactoryBean">
    <property name="clusterName" value="clusterName" />
    <property name="keyspaceName" value="keyspaceName" />
    <property name="connectionPoolName" value="connectionPoolName" />
    <property name="clusterName" value="clusterName" />
    <property name="portNo" value="9110" />
    <property name="seeds" value="someAppropriateSeedsValue" />
    </bean>
    As suggested

    Comment


    • #3
      Hi,

      Is there a way to dynamically set KeyspaceFactoryBean properties from the client when it asks the context for a new instance?

      Thanks a lot,

      Max

      Comment

      Working...
      X