【问题标题】:Hazelcast's IScheduledExecutorService can't serialize taskHazelcast 的 IScheduledExecutorService 无法序列化任务
【发布时间】:2018-07-06 06:25:48
【问题描述】:

我的项目中有一个Hazelcast,它适用于集群中的两个成员。同时,我使用 Hazelcast 的IScheduledExecutorService 来安排一个任务,该任务从数据库中获取一些数据并以固定速率将其放入缓存中。

问题是,我的任务类(该调度程序的任务)有一些字段无法序列化(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);
    }
}

}

【问题讨论】:

    标签: java spring serialization scheduled-tasks hazelcast


    【解决方案1】:

    @SpringAware 默认禁用。 (请参阅此issue 为什么禁用它)您需要通过声明&lt;hz:spring-aware /&gt; 或以编程方式(如in this example)来启用它。

    【讨论】:

      猜你喜欢
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 2020-01-16
      • 2017-12-30
      • 1970-01-01
      • 2017-04-08
      相关资源
      最近更新 更多