首页 > 解决方案 > 使用 Jet.bootstrappedInstance() 时无法设置 hazelcast ManagedContext

问题描述

我想hazelcast-jet在我的 spring 应用程序中嵌入运行(即集群在进程中/不是远程的)。我还希望我的 jet 工作能够从ApplicationContext(请参阅jet/spring docs)访问 spring beans。我不想在每个 jet 作业上配置类路径,我想使用 JVM 的引导类路径。

我想我需要用来Jet.bootstrappedInstance()获取 aJetInstance但这不允许我ManagedContext在基础上设置HazelcastInstance. 文档Jet.bootstrappedInstance()详细介绍了我想与 Jet 交互的方式

当您将作业提交到在 JVM 中本地运行的 Jet 实例时,它将具有所有可用的依赖类

我试过这个:

@Bean
public ManagedContext managedContext() {
   return new SpringManagedContext();
}
@Bean
public JetInstance jet(ManagetContext managedContext, List<Pipeline> pipelines) {   ​
   JetInstance jet = Jet.bootstrappedInstance();
   jet.getHazelcastInstance().getConfig().setManagedContext(managedContext);
   ...
   return jet;
}

但我得到以下异常

Caused by: java.lang.UnsupportedOperationException: Unsupported operation
    at com.hazelcast.internal.dynamicconfig.DynamicConfigurationAwareConfig.setManagedContext(DynamicConfigurationAwareConfig.java:981)

请注意,调用 setManagedContext(...) 的正常方式是这样的

   JetConfig config = new JetConfig();
   config.configureHazelcast(hz -> hz.setManagedContext(managedContext));
   JetInstance jet = Jet.newJetInstance(config);

我找不到Jet.bootstrappedInstance()Config.setManagedContext(ManagedContext)

作为一种解决方法,我从JetBootstrap.createStandaloneInstance()(私有方法)复制了代码并添加了hzConfig.setManagedContext(managedContext)

  JetConfig config = ConfigProvider.locateAndGetJetConfig();
  Config hzConfig = config.getHazelcastConfig();
  
  // turn off all discovery to make sure node doesn't join any existing cluster
  hzConfig.setProperty("hazelcast.wait.seconds.before.join", "0");
  hzConfig.getAdvancedNetworkConfig().setEnabled(false);
  hzConfig.setManagedContext(managedContext);
  
  JoinConfig join = hzConfig.getNetworkConfig().getJoin();
  join.getAutoDetectionConfig().setEnabled(false);
  join.getMulticastConfig().setEnabled(false);
  join.getTcpIpConfig().setEnabled(false);
  join.getAwsConfig().setEnabled(false);
  join.getGcpConfig().setEnabled(false);
  join.getAzureConfig().setEnabled(false);
  join.getKubernetesConfig().setEnabled(false);
  join.getEurekaConfig().setEnabled(false);
  join.setDiscoveryConfig(new DiscoveryConfig());
  JetInstance jet = Jet.newJetInstance(config);  

标签: springhazelcasthazelcast-jet

解决方案


I want to run hazelcast-jet embedded in my spring application (I don't want to submit jobs to a remote jet cluster)

In this case using bootstrapInstance() doesn't make sense. It was designed to work with the bin/jet submit command where it creates a client instance according to the config/hazelcast-client.yaml or standalone instance for testing from your IDE.

For using Jet embedded you can use the annotation based configuration or alternatively you can use Hazelcast Jet Spring Boot Starter.


推荐阅读