------------------------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;
}
hazelcast 增删改查 代码段
分享好友
分享这个小栈给你的朋友们,一起进步吧。
订阅须知
• 所有用户可根据关注领域订阅专区或所有专区
• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询
• 专区发布评论属默认订阅所评论专区(除付费小栈外)
技术专家
查看更多- 飘絮絮絮丶专家