0

我的配置:

<bean parent="cache-template">
    <property name="name" value="yagoLabel" />
    <property name="cacheMode" value="PARTITIONED" />
    <property name="atomicityMode" value="TRANSACTIONAL" />
    <property name="distributionMode" value="PARTITIONED_ONLY" />
    <property name="backups" value="1" />
    <property name="store">
        <bean class="id.ac.itb.ee.lskk.lumen.yago.YagoLabelCacheStore" autowire="byType" init-method="init" />
    </property>
    <property name="writeBehindEnabled" value="true" />
    <property name="writeBehindFlushSize" value="102380" />
    <property name="writeBehindFlushFrequency" value="30000" />
    <property name="writeBehindBatchSize" value="10240" />
    <property name="swapEnabled" value="false" />
    <property name="evictionPolicy">
        <bean class="org.gridgain.grid.cache.eviction.lru.GridCacheLruEvictionPolicy">
            <property name="maxSize" value="102400" />
        </bean>
    </property>
</bean>

我按如下方式启动 GridGain:

我的GridCacheStore实现:

public class YagoLabelCacheStore extends GridCacheStoreAdapter<String, YagoLabel> {

    private static final Logger log = LoggerFactory
        .getLogger(YagoLabelCacheStore.class);
    private DBCollection labelColl;

   @GridSpringResource(resourceName="mongoDb")
   private DB db;
   @Inject
   private GridGainSpring grid;

   @PostConstruct
   public void init() {
   log.info("Grid is {}", grid);
   labelColl = db.getCollection("label");
}

我按如下方式启动 GridGain:

String entityId = "Muhammad";

try (AnnotationConfigApplicationContext appCtx 
          = new AnnotationConfigApplicationContext(LumenConfig.class)) {
    Grid grid = appCtx.getBean(Grid.class);
    GridCache<String, YagoLabel> labelCache = YagoLabel.cache(grid);
    log.info("Label for {}: {}", entityId, labelCache.get(entityId));
}

LumenConfigSpring 配置包含一个DB名为mongoDb.

然而,这会抛出NullPointerException,因为db没有正确注入。我只是为了测试而尝试@Inject GridGainSpring的,甚至GridGainSpring本身也没有注入。

我也尝试<property name="db" ref="mongoDb"/>在 GridGain Config XML 中进行设置,但 Spring 抱怨找不到 bean。

我的解决方法是将它放在一个public static字段中,但这太难了:https ://github.com/ceefour/lumen-kb/blob/b8445fbebd227fb7ac337c758a60badb7ecd3095/cli/src/main/java/id/ac/itb/ee/lskk/流明/yago/YagoLabelCacheStore.java

4

1 回答 1

0

方法是加载GridConfiguration使用 Spring,然后将其传递给GridGainSpring.start()

// "classpath:" is required, otherwise it won't be found in a WAR
@ImportResource("classpath:id/ac/itb/ee/lskk/lumen/core/lumen.gridgain.xml")
@Configuration
public static class GridGainConfig {

    @Inject
    private ApplicationContext appCtx;
    @Inject
    private GridConfiguration gridCfg;

    @Bean(destroyMethod="close")
    public Grid grid() throws GridException {
        return GridGainSpring.start(gridCfg, appCtx);
    }

}

:-)

于 2014-07-07T10:38:49.580 回答