java - Hazelcast 的 IScheduledExecutorService 无法序列化任务
问题描述
我的项目中有一个Hazelcast
,它适用于集群中的两个成员。同时,我使用 HazelcastIScheduledExecutorService
来安排一个任务,该任务从 DB 中获取一些数据并以固定速率将其放入缓存中。
问题是,我的任务类(该调度程序的任务)有一些字段,无法序列化(IScheduledExecutorService
尝试这样做),例如 mybatis Mappers 和 hazelcast 的 CacheManager,它们从数据库获取数据并将数据放入缓存。
我尝试使用 Spring 的功能,例如@Autowire
在该字段和@SpringAware
课堂上,但没有运气。
可能有一些我不知道的 Hazelcast 或 Spring 功能?任何帮助,将不胜感激。
我的任务课:
@Service
@SpringAware
public class MyTask implements Runnable, Serializable {
private static final Logger LOG = LoggerFactory.getLogger(MyTask.class);
private static final String CACHE1 = "cache1";
private static final String CACHE2 = "cache2";
private static final String KEY1 = "key1";
private static final String KEY2 = "key2";
@Autowired
private MapperOne mapperOne;
@Autowired
private MapperTwo mapperTwo;
@Autowired
// if mark it 'transient' it would be null for other threads
private transient CacheManager cacheManager;
@Override
public void run() {
LOG.info("ABOUT TO UPDATE CACHE");
List<SomeStuff> stuff1 = mapperOne.getData();
List<OtherStuff> stuff2 = mapperTwo.getData();
cacheManager.getCache(CACHE1).put(KEY1, stuff1);
cacheManager.getCache(CACHE2).put(KEY2, stuff2);
}
}
我的调度程序:
@Service
public class MySchedulerService {
@Autowired
private MyTask myTask;
@PostConstruct
void init() {
if (hazelcastInstance.getCluster().getMembers().iterator().next().localMember()) {
IScheduledExecutorService notificationCacheService =
hazelcastInstance.getScheduledExecutorService("myService");
notificationCacheService.scheduleOnKeyOwnerAtFixedRate(
myTask, hazelcastInstance.getCluster().getLocalMember(), 0, 15, TimeUnit.SECONDS);
}
}
}
解决方案
推荐阅读
- php - Doctrine ODM 唯一约束不验证属性
- angular - 从 finalize() 运算符发出另一个 Observable 是一种好习惯吗?
- python - 我可以用一个循环来解决这个问题,但为什么没有用这个递归函数显示数组的所有组合?
- spring - SCDF - 在不同的模式中创建元数据表并使用不同的模式处理批处理作业
- javascript - 将数组从平面一维转换为树,如 javascript/lodash
- reactjs - 组件或 mapStateToProps 中的逻辑
- python - Plotly:如何在随机生成的时间序列上实现 X 轴上的日期时间范围
- javascript - 使用 innerwidth 排版由 p5.js 函数组成的字体
- javascript - 在 savePost 运行之前更新 Gutenberg 块内容
- sql - SQL 批量插入 - ROWTERMINATOR 'Ox0A' 与 '\r\n'