Java源码示例:com.hazelcast.config.XmlConfigBuilder
示例1
public static void main(String[] args) {
try {
// 获取配置文件磁盘路径
final String path = Thread.currentThread().getContextClassLoader().getResource("").toString() + DEF_CONFIG_FILE;
// 构建XML配置
XmlConfigBuilder builder = new XmlConfigBuilder(path);
// 配置对应的properties解析参数
builder.setProperties(getProperties());
// 创建Config
Config config = builder.build();
// 输出Config参数
System.out.println(config.getGroupConfig().getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
示例2
private Config createConfig() {
Config config;
try {
config = new XmlConfigBuilder(Application.class.getClassLoader().getResource(CONFIG_NAME)).build();
}
catch (IOException e) {
logger.error(e.getMessage(), e);
throw new Error(e);
}
config.getSerializationConfig().addDataSerializableFactory(SerializableFactory.ID, new SerializableFactory());
config.getSerializationConfig().getSerializerConfigs().add(new SerializerConfig().setTypeClass(JsonNode.class)
.setImplementation(JsonSerializer.makePlain(JsonNode.class)));
return config;
}
示例3
@Test
public void test_simple_sequencer_initialization_declarative()
throws Exception {
ClassLoader classLoader = BasicTestCase.class.getClassLoader();
InputStream stream = classLoader.getResourceAsStream("hazelcast-node-configuration.xml");
Config config = new XmlConfigBuilder(stream).build();
TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(1);
HazelcastInstance hazelcastInstance = factory.newHazelcastInstance(config);
try {
Snowcast snowcast = SnowcastSystem.snowcast(hazelcastInstance);
SnowcastSequencer sequencer = buildSnowcastSequencer(snowcast);
assertNotNull(sequencer);
} finally {
factory.shutdownAll();
}
}
示例4
public static HazelcastInstance findOrCreateMember(final String xmlConfiguration, String instanceName) throws IOException {
final HazelcastInstance found = Hazelcast.getHazelcastInstanceByName(instanceName);
if (found != null) {
return found;
}
final Config config;
if (xmlConfiguration != null) {
config = new XmlConfigBuilder(IOs.findConfiguration(xmlConfiguration)).build();
} else {
config = new XmlConfigBuilder().build();
}
if (instanceName != null) {
config.setInstanceName(instanceName);
}
return Hazelcast.newHazelcastInstance(config);
}
示例5
private static Config getIMDGConfig(Resource configLocation) throws IOException {
URL configUrl = configLocation.getURL();
String configFileName = configUrl.getPath();
InputStream inputStream = configUrl.openStream();
if (configFileName.endsWith(".yaml") || configFileName.endsWith("yml")) {
return new YamlConfigBuilder(inputStream).build();
}
return new XmlConfigBuilder(inputStream).build();
}
示例6
public static HazelcastInstance getInstance() {
if (hazelcastInstance == null) {
Config config = new XmlConfigBuilder().build();
config.setInstanceName(Thread.currentThread().getName());
hazelcastInstance = Hazelcast.getOrCreateHazelcastInstance(config);
}
return hazelcastInstance;
}
示例7
private Config getHazelcastConfig(NodeConfig nodeConfig, long interval, TimeUnit unit) {
Config hazelcastConfig = new XmlConfigBuilder().build();
hazelcastConfig.setGroupConfig(
new GroupConfig().setName(this.clusterName));
NetworkConfig networkConfig = nodeConfig.getNetworkConfig();
if (unit != null && interval > 0L) {
long seconds = Math.max(unit.toSeconds(interval), 1L);
hazelcastConfig.setProperty("hazelcast.max.join.seconds", Long.toString(seconds));
}
String host = networkConfig.getHost();
if (host != null)
hazelcastConfig.getNetworkConfig().setPublicAddress(host);
hazelcastConfig.getNetworkConfig().setPort(networkConfig.getPort());
String listenInterface = networkConfig.getListeningInterface();
if (listenInterface != null) {
hazelcastConfig.getNetworkConfig().getInterfaces()
.setEnabled(true)
.addInterface(listenInterface);
hazelcastConfig.setProperty("hazelcast.local.localAddress", listenInterface);
hazelcastConfig.setProperty("hazelcast.local.publicAddress", listenInterface);
}
JoinConfig joinConfig = networkConfig.getJoinConfig();
JoinConfig.Member[] members = joinConfig.getMembers();
if (members != null && members.length > 0) {
TcpIpConfig tcpIpConfig = hazelcastConfig.getNetworkConfig().getJoin().getTcpIpConfig();
tcpIpConfig.setConnectionTimeoutSeconds(joinConfig.getTimeout());
tcpIpConfig.setEnabled(true);
for (JoinConfig.Member member : members)
tcpIpConfig.addMember(member.getHost() + ":" + member.getPort());
}
return hazelcastConfig;
}
示例8
@Bean
public Config hazelcastConfig() {
Resource location = hp.getConfigLocation();
if (location != null && location.exists()) {
try {
return new XmlConfigBuilder(hp.getConfigLocation()
.getInputStream()).build();
} catch (IOException e) {
throw new IllegalArgumentException(
"Unable to use config location " + location, e);
}
} else {
return new Config();
}
}
示例9
@Bean
@ConditionalOnProperty(name = "summer.hazelcast.consul.enabled", havingValue = "true", matchIfMissing = false)
public Config hazlecastConsulConfig() throws Exception {
Config config = new XmlConfigBuilder().build();
//
final SpringManagedContext springManagedContext = new SpringManagedContext();
springManagedContext.setApplicationContext(applicationContext);
config.setManagedContext(springManagedContext);
//
// Use Consul for discovery instead of multicast with the help of this:
// https://github.com/bitsofinfo/hazelcast-consul-discovery-spi
//
config.setProperty("hazelcast.discovery.enabled", Boolean.TRUE.toString());
if(healthCheckType==HEALTHCHECKTYPE.HTTP)
{
config.setProperty("hazelcast.http.healthcheck.enabled", Boolean.TRUE.toString());
}
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config.getNetworkConfig().setPublicAddress(InetAddress.getByName(discoveryHostName).getHostAddress());
DiscoveryStrategyConfig discoveryStrategyConfig = new DiscoveryStrategyConfig(
new ConsulDiscoveryStrategyFactory());
discoveryStrategyConfig.addProperty(ConsulDiscoveryConfiguration.CONSUL_HOST.key(), this.consulHost);
discoveryStrategyConfig.addProperty(ConsulDiscoveryConfiguration.CONSUL_PORT.key(), this.consulPort);
discoveryStrategyConfig.addProperty(ConsulDiscoveryConfiguration.CONSUL_SERVICE_TAGS.key(), this.serviceTags);
discoveryStrategyConfig.addProperty(ConsulDiscoveryConfiguration.CONSUL_SERVICE_NAME.key(),
this.servicePrefix + this.appName);
discoveryStrategyConfig.addProperty(ConsulDiscoveryConfiguration.CONSUL_HEALTHY_ONLY.key(), true);
discoveryStrategyConfig.addProperty(ConsulDiscoveryConfiguration.CONSUL_DISCOVERY_DELAY_MS.key(),
this.configurationDelay);
discoveryStrategyConfig.addProperty(ConsulDiscoveryConfiguration.CONSUL_REGISTRATOR.key(),
LocalDiscoveryNodeRegistrator.class.getName());
ObjectNode jsonRegistratorConfig = JsonNodeFactory.instance.objectNode();
jsonRegistratorConfig.put(LocalDiscoveryNodeRegistrator.CONFIG_PROP_PREFER_PUBLIC_ADDRESS, true);
switch(healthCheckType) {
case HTTP:
jsonRegistratorConfig.put(BaseRegistrator.CONFIG_PROP_HEALTH_CHECK_PROVIDER,HttpHealthCheckBuilder.class.getName());
jsonRegistratorConfig.put(HttpHealthCheckBuilder.CONFIG_PROP_HEALTH_CHECK_HTTP, "http://#MYIP:#MYPORT/hazelcast/health");
jsonRegistratorConfig.put(HttpHealthCheckBuilder.CONFIG_PROP_HEALTH_CHECK_HTTP_INTERVAL_SECONDS, healthCheckInterval);
log.debug("Hazelcast HTTP health check set up (run every {} secs)", healthCheckInterval);
break;
case TCP:
jsonRegistratorConfig.put(BaseRegistrator.CONFIG_PROP_HEALTH_CHECK_PROVIDER,TcpHealthCheckBuilder.class.getName());
jsonRegistratorConfig.put(TcpHealthCheckBuilder.CONFIG_PROP_HEALTH_CHECK_TCP, "#MYIP:#MYPORT");
jsonRegistratorConfig.put(TcpHealthCheckBuilder.CONFIG_PROP_HEALTH_CHECK_TCP_INTERVAL_SECONDS, healthCheckInterval);
log.debug("Hazelcast TCP health check set up (run every {} secs)", healthCheckInterval);
break;
default:
log.warn("What are you doing here, my oh my!");
break;
}
// Scripts are executed on the consul server, so they are consul-host
// dependent, meh
// jsonRegistratorConfig.put(BaseRegistrator.CONFIG_PROP_HEALTH_CHECK_PROVIDER,
// ScriptHealthCheckBuilder.class.getName());
// jsonRegistratorConfig.put(ScriptHealthCheckBuilder.CONFIG_PROP_HEALTH_CHECK_SCRIPT,
// "nc -z #MYIP #MYPORT");
// jsonRegistratorConfig.put(ScriptHealthCheckBuilder.CONFIG_PROP_HEALTH_CHECK_SCRIPT_INTERVAL_SECONDS,
// 10);
discoveryStrategyConfig.addProperty(ConsulDiscoveryConfiguration.CONSUL_REGISTRATOR_CONFIG.key(),
jsonRegistratorConfig.toString());
config.getNetworkConfig().getJoin().getDiscoveryConfig().getDiscoveryStrategyConfigs()
.add(discoveryStrategyConfig);
log.info("Hazelcast configured to use Consul for discovery");
// Apply custom configurations, if necessary
if(hazelcastConfigurers!=null)
{
for(HazelcastConfigurer hazelcastConfigurer: hazelcastConfigurers)
{
log.debug("Applying HazelcastConfigurer {}", hazelcastConfigurer.getClass().getName());
hazelcastConfigurer.configure(config);
}
}
return config;
}
示例10
/**
* Loads Hazelcast config XML and transform it into a {@link Config} object.
*
* The content is read from:
* <ol>
* <li>the location denoted by the {@code vertx.hazelcast.config} sysprop, if present, or</li>
* <li>the {@code cluster.xml} file on the classpath, if present, or</li>
* <li>the default config file</li>
* </ol>
*
* @return a config object
*/
public static Config loadConfig() {
Config cfg = null;
try (InputStream is = getConfigStream();
InputStream bis = new BufferedInputStream(is)) {
cfg = new XmlConfigBuilder(bis).build();
} catch (IOException ex) {
log.error("Failed to read config", ex);
}
return cfg;
}