Hazelcast 的 IScheduledExecutorService 无法序列化任务
Hazelcast's IScheduledExecutorService can't serialize task
我的项目中有一个 Hazelcast
,它在集群中的两个成员上工作。同时,我使用 Hazelcast 的 IScheduledExecutorService
来安排一个任务,它从数据库中获取一些数据并将其以固定速率放入缓存中。
问题是,我的任务 class(该调度程序的任务)有一些字段无法序列化(IScheduledExecutorService
试图这样做),例如 mybatis Mappers 和hazelcast 的 CacheManager,它从 DB 获取数据并将其放入缓存。
我尝试使用 Spring 的功能,例如在该字段上使用 @Autowire
,在 class 上使用 @SpringAware
,但没有成功。
可能有一些我不知道的 Hazelcast 或 Spring 功能?任何帮助将不胜感激。
我的任务class:
@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);
}
}
}
@SpringAware 默认是禁用的。 (见此 issue why it was disabled) You need to enable it by declaring <hz:spring-aware />
or programmatically like in this example。
我的项目中有一个 Hazelcast
,它在集群中的两个成员上工作。同时,我使用 Hazelcast 的 IScheduledExecutorService
来安排一个任务,它从数据库中获取一些数据并将其以固定速率放入缓存中。
问题是,我的任务 class(该调度程序的任务)有一些字段无法序列化(IScheduledExecutorService
试图这样做),例如 mybatis Mappers 和hazelcast 的 CacheManager,它从 DB 获取数据并将其放入缓存。
我尝试使用 Spring 的功能,例如在该字段上使用 @Autowire
,在 class 上使用 @SpringAware
,但没有成功。
可能有一些我不知道的 Hazelcast 或 Spring 功能?任何帮助将不胜感激。
我的任务class:
@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);
}
}
}
@SpringAware 默认是禁用的。 (见此 issue why it was disabled) You need to enable it by declaring <hz:spring-aware />
or programmatically like in this example。