hazelcast not getting MapStore from spring beans (autowired dependency is null)
Asked Answered
K

1

2

I am implementing the hazelcast map store for persistence. But could not autowired spring beans(DataSource below) into the hazelcast mapstore object (meaning hazelcast not getting the map store object from spring beans). I read that hazelcast supports spring DI. What am i missing ? Below is my partial map store code

If I get the bean from context using get bean like below

MySQLStore store = (MySQLStore)context.getBean(MySQLStore.class);

I get mysql store with the datasource dependency injected. So this should be an issue with Hazelcast not getting beans from spring. Am I missing any configuration in hazelcast map store.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;

import com.hazelcast.core.MapStore;

@Component
public class MySQLStore implements MapStore<String, ScripDetails> {

    @Autowired
    private DataSource dataSource;

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public ScripDetails load(String arg0) {
        System.out.println("loading data from store");

        String sql = "SELECT * FROM DATA";

        Connection conn = null;

Below is the spring-beans xml with has the datasource and component scan which includes the MySQLStore package.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <context:component-scan base-package="com.tlab"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://host:port/DB" />
        <property name="username" value="username" />
        <property name="password" value="password" />
    </bean>

</beans>

Hazelcast xml below

<?xml version="1.0" encoding="UTF-8"?>

<hazelcast
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.0.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <network>
        <join><multicast enabled="true"/></join>
    </network>

    <map name="scrips">
        <backup-count>1</backup-count>
        <time-to-live-seconds>0</time-to-live-seconds>
        <max-idle-seconds>0</max-idle-seconds>
        <eviction-policy>LRU</eviction-policy>
        <eviction-percentage>25</eviction-percentage>
        <merge-policy>hz.ADD_NEW_ENTRY</merge-policy>
        <map-store enabled="true">
              <class-name>com.tlab.MySQLStore</class-name>
        </map-store>
    </map>
</hazelcast>
Kimberli answered 12/5, 2015 at 20:13 Comment(0)
D
3

In your example, Hazelcast instance is not a Spring bean. You need to configure Hazelcast instance using Spring. You can find documentation here and examples

I hope that it helps.

Thank you

Dyslalia answered 13/5, 2015 at 1:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.