hazelcast 增删改查 代码段

2022-05-23 00:00:00 专区 订阅 付费 交易 评论

------------------------application中要实现以下bean--------------------------
@Bean
public Config metamodelHazelcastConfig(@Value("${metamodel.hazelcast.config}") String url) throws IOException {
return new UrlXmlConfig(url);
}

@Bean(destroyMethod = "shutdown")
public HazelcastInstance metamodelHazelcastInstance(@Qualifier("metamodelHazelcastConfig") Config hazelcastConfig) {
return Hazelcast.newHazelcastInstance(hazelcastConfig);
}

@Bean
public ApplicationRunner refreshData() {
return args -> {
applicationContext.getBeansOfType(SensorService.class).values().forEach(service -> service.refresh(false));
};
}



-----------------以下为service-----------------------------
@Autowired
private SensorMapper mapper;

@Autowired
private ApplicationEventPublisher publisher;

@Autowired
@Qualifier("metamodelHazelcastInstance")
private HazelcastInstance hazelcast;

protected IMap<String, Sensor> getMetamodel() {
return hazelcast.getMap(sensor);
}

public Sensor get(String id) {
return getMetamodel().get(id);
}

public void refresh(boolean force) {
List<Sensor> all = allEntities().stream().collect(Collectors.toList());
all.forEach(m -> {
if (force) {
getMetamodel().set(m.getId(), m);
} else {
getMetamodel().putIfAbsent(m.getId(), m);
}
});
if (force) {
Set<String> allCodes = all.stream().map(Sensor::getId).collect(Collectors.toSet());
getMetamodel().removeAll(entry -> !allCodes.contains(entry.getKey()));
}
}


public List<Sensor> allEntities() {
return mapper.allEntities();
}


public void createSensor(Sensor sensor) {
getMetamodel().set(sensor.getId(), sensor);

}


public void purgeSensor(String id) {
getMetamodel().delete(id);
}

public List<Sensor> findSensorByOrgId(String orgId) {
List<Sensor> valuesToMatch = new ArrayList<Sensor>(
getMetamodel().values(new SqlPredicate("organizationId = " + orgId)));
return valuesToMatch;

}

相关文章