com.hazelcast.config.NetworkConfig Java Examples

The following examples show how to use com.hazelcast.config.NetworkConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HazelcastConfigRuntimeModify.java    From hazelcast-demo with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	// 创建默认config对象
	Config config = new Config();
	
	// 获取network元素<network></network>
	NetworkConfig netConfig = config.getNetworkConfig();
	System.out.println("Default port:" + netConfig.getPort());
	
	// 设置组网起始监听端口
	netConfig.setPort(9701);
	System.out.println("Customer port:" + netConfig.getPort());
	// 获取join元素<join></join>
	JoinConfig joinConfig = netConfig.getJoin();
	// 获取multicast元素<multicast></multicast>
	MulticastConfig multicastConfig = joinConfig.getMulticastConfig();
	// 输出组播协议端口
	System.out.println(multicastConfig.getMulticastPort());
	// 禁用multicast协议
	multicastConfig.setEnabled(false);
	
	// 初始化Hazelcast
	Hazelcast.newHazelcastInstance(config);
}
 
Example #2
Source File: HazelcastSymmetric.java    From Android_Code_Arbiter with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void init() {

        //Specific map time to live
        MapConfig myMapConfig = new MapConfig();
        myMapConfig.setName("cachetest");
        myMapConfig.setTimeToLiveSeconds(10);

        //Package config
        Config myConfig = new Config();
        myConfig.addMapConfig(myMapConfig);

        //Symmetric Encryption
        SymmetricEncryptionConfig symmetricEncryptionConfig = new SymmetricEncryptionConfig();
        symmetricEncryptionConfig.setAlgorithm("DESede");
        symmetricEncryptionConfig.setSalt("saltysalt");
        symmetricEncryptionConfig.setPassword("lamepassword");
        symmetricEncryptionConfig.setIterationCount(1337);

        //Weak Network config..
        NetworkConfig networkConfig = new NetworkConfig();
        networkConfig.setSymmetricEncryptionConfig(symmetricEncryptionConfig);

        myConfig.setNetworkConfig(networkConfig);

        Hazelcast.init(myConfig);

        cacheMap = Hazelcast.getMap("cachetest");
    }
 
Example #3
Source File: HazelcastTestFactory.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
static HazelcastInstance newStandaloneHazelcastInstance() {
    Config config = new Config();
    config.setProperty("hazelcast.logging.type", "slf4j");
    config.setProperty("hazelcast.shutdownhook.enabled", "false");
    NetworkConfig network = config.getNetworkConfig();
    network.getJoin().getTcpIpConfig().setEnabled(false);
    network.getJoin().getMulticastConfig().setEnabled(false);
    return Hazelcast.newHazelcastInstance(config);
}
 
Example #4
Source File: HazelcastConfigSimple.java    From hazelcast-demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
	// 从classpath加载配置文件
	Config config = new ClasspathXmlConfig("xmlconfig/simple-config.xml");
	// 获取网络配置
	NetworkConfig netConfig = config.getNetworkConfig();
	// 获取用户定义的map配置
	MapConfig mapConfigXml = config.getMapConfig("demo.config");
	// 获取系统默认的map配置
	MapConfig mapConfigDefault = config.getMapConfig("default");
	// 输出集群监听的起始端口号
	System.out.println("Current port:" + netConfig.getPort());
	// 输出监听端口的累加号
	System.out.println("Current port count:" + netConfig.getPortCount());
	// 输出自定义map的备份副本个数
	System.out.println("Config map backup count:" + mapConfigXml.getBackupCount());
	// 输出默认map的备份副本个数
	System.out.println("Default map backup count:" + mapConfigDefault.getBackupCount());

	// 测试创建Hazelcast实例并读写测试数据
	HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
	HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);

	Map<Integer, String> defaultMap1 = instance1.getMap("defaultMap");
	defaultMap1.put(1, "testMap");
	Map<Integer, String> configMap1 = instance1.getMap("configMap");
	configMap1.put(1, "configMap");

	Map<Integer, String> testMap2 = instance2.getMap("defaultMap");
	System.out.println("Default map value:" + testMap2.get(1));
	Map<Integer, String> configMap2 = instance2.getMap("configMap");
	System.out.println("Config map value:" + configMap2.get(1));
}
 
Example #5
Source File: MainDeploy.java    From okapi with Apache License 2.0 5 votes vote down vote up
private void deployClustered(final Logger logger, Handler<AsyncResult<Vertx>> fut) {
  if (hazelcastConfig == null) {
    hazelcastConfig = ConfigUtil.loadConfig();
    if (clusterHost != null) {
      NetworkConfig network = hazelcastConfig.getNetworkConfig();
      InterfacesConfig interfacesConfig = network.getInterfaces();
      interfacesConfig.setEnabled(true).addInterface(clusterHost);
    }
  }
  hazelcastConfig.setProperty("hazelcast.logging.type", "log4j");

  HazelcastClusterManager mgr = new HazelcastClusterManager(hazelcastConfig);
  vopt.setClusterManager(mgr);
  EventBusOptions eventBusOptions = vopt.getEventBusOptions();
  if (clusterHost != null) {
    logger.info("clusterHost={}", clusterHost);
    eventBusOptions.setHost(clusterHost);
  } else {
    logger.warn("clusterHost not set");
  }
  if (clusterPort != -1) {
    logger.info("clusterPort={}", clusterPort);
    eventBusOptions.setPort(clusterPort);
  } else {
    logger.warn("clusterPort not set");
  }
  eventBusOptions.setClustered(true);

  Vertx.clusteredVertx(vopt, res -> {
    if (res.succeeded()) {
      MainVerticle v = new MainVerticle();
      v.setClusterManager(mgr);
      deploy(v, res.result(), fut);
    } else {
      fut.handle(Future.failedFuture(res.cause()));
    }
  });
}
 
Example #6
Source File: HazelcastSessionDao.java    From dpCms with Apache License 2.0 5 votes vote down vote up
public HazelcastSessionDao() {
    log.info("Initializing Hazelcast Shiro session persistence..");

    // configure Hazelcast instance
    final Config cfg = new Config();
    cfg.setInstanceName(hcInstanceName);
    // group configuration
    cfg.setGroupConfig(new GroupConfig(HC_GROUP_NAME, HC_GROUP_PASSWORD));
    // network configuration initialization
    final NetworkConfig netCfg = new NetworkConfig();
    netCfg.setPortAutoIncrement(true);
    netCfg.setPort(HC_PORT);
    // multicast
    final MulticastConfig mcCfg = new MulticastConfig();
    mcCfg.setEnabled(false);
    mcCfg.setMulticastGroup(HC_MULTICAST_GROUP);
    mcCfg.setMulticastPort(HC_MULTICAST_PORT);
    // tcp
    final TcpIpConfig tcpCfg = new TcpIpConfig();
    tcpCfg.addMember("127.0.0.1");
    tcpCfg.setEnabled(false);
    // network join configuration
    final JoinConfig joinCfg = new JoinConfig();
    joinCfg.setMulticastConfig(mcCfg);
    joinCfg.setTcpIpConfig(tcpCfg);
    netCfg.setJoin(joinCfg);
    // ssl
    netCfg.setSSLConfig(new SSLConfig().setEnabled(false));

    // get map
    map = Hazelcast.newHazelcastInstance(cfg).getMap(HC_MAP);
    log.info("Hazelcast Shiro session persistence initialized.");
}
 
Example #7
Source File: KubernetesApiEndpointResolver.java    From hazelcast-kubernetes with Apache License 2.0 5 votes vote down vote up
private int port(KubernetesClient.EndpointAddress address) {
    if (this.port > 0) {
        return this.port;
    }
    if (address.getPort() != null) {
        return address.getPort();
    }
    return NetworkConfig.DEFAULT_PORT;
}
 
Example #8
Source File: HazelcastSessionDao.java    From spring-boot-shiro-orientdb with Apache License 2.0 5 votes vote down vote up
public HazelcastSessionDao() {
    log.info("Initializing Hazelcast Shiro session persistence..");

    // configure Hazelcast instance
    final Config cfg = new Config();
    cfg.setInstanceName(hcInstanceName);
    // group configuration
    cfg.setGroupConfig(new GroupConfig(HC_GROUP_NAME, HC_GROUP_PASSWORD));
    // network configuration initialization
    final NetworkConfig netCfg = new NetworkConfig();
    netCfg.setPortAutoIncrement(true);
    netCfg.setPort(HC_PORT);
    // multicast
    final MulticastConfig mcCfg = new MulticastConfig();
    mcCfg.setEnabled(false);
    mcCfg.setMulticastGroup(HC_MULTICAST_GROUP);
    mcCfg.setMulticastPort(HC_MULTICAST_PORT);
    // tcp
    final TcpIpConfig tcpCfg = new TcpIpConfig();
    tcpCfg.addMember("127.0.0.1");
    tcpCfg.setEnabled(false);
    // network join configuration
    final JoinConfig joinCfg = new JoinConfig();
    joinCfg.setMulticastConfig(mcCfg);
    joinCfg.setTcpIpConfig(tcpCfg);
    netCfg.setJoin(joinCfg);
    // ssl
    netCfg.setSSLConfig(new SSLConfig().setEnabled(false));

    // get map
    map = Hazelcast.newHazelcastInstance(cfg).getMap(HC_MAP);
    log.info("Hazelcast Shiro session persistence initialized.");
}
 
Example #9
Source File: SessionConfig.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "shutdown")
public HazelcastInstance hazelcastInstance() {
	Config config = new Config();
	NetworkConfig networkConfig = config.getNetworkConfig();
	networkConfig.setPort(0);
	networkConfig.getJoin().getMulticastConfig().setEnabled(false);
	MapAttributeConfig attributeConfig = new MapAttributeConfig()
			.setName(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
			.setExtractor(PrincipalNameExtractor.class.getName());
	config.getMapConfig(HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME)
			.addMapAttributeConfig(attributeConfig).addMapIndexConfig(
					new MapIndexConfig(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false));
	return Hazelcast.newHazelcastInstance(config);
}
 
Example #10
Source File: Initializer.java    From spring-session with Apache License 2.0 5 votes vote down vote up
private HazelcastInstance createHazelcastInstance() {
	Config config = new Config();
	NetworkConfig networkConfig = config.getNetworkConfig();
	networkConfig.setPort(0);
	networkConfig.getJoin().getMulticastConfig().setEnabled(false);
	config.getMapConfig(SESSION_MAP_NAME).setTimeToLiveSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS);
	return Hazelcast.newHazelcastInstance(config);
}
 
Example #11
Source File: HazelcastITestUtils.java    From spring-session with Apache License 2.0 5 votes vote down vote up
/**
 * Creates {@link HazelcastInstance} for use in integration tests.
 * @return the Hazelcast instance
 */
static HazelcastInstance embeddedHazelcastServer() {
	Config config = new Config();
	NetworkConfig networkConfig = config.getNetworkConfig();
	networkConfig.setPort(0);
	networkConfig.getJoin().getMulticastConfig().setEnabled(false);
	MapAttributeConfig attributeConfig = new MapAttributeConfig()
			.setName(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
			.setExtractor(PrincipalNameExtractor.class.getName());
	config.getMapConfig(HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME)
			.addMapAttributeConfig(attributeConfig).addMapIndexConfig(
					new MapIndexConfig(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false));
	return Hazelcast.newHazelcastInstance(config);
}
 
Example #12
Source File: DnsEndpointResolver.java    From hazelcast-kubernetes with Apache License 2.0 4 votes vote down vote up
private static int getHazelcastPort(int port) {
    if (port > 0) {
        return port;
    }
    return NetworkConfig.DEFAULT_PORT;
}
 
Example #13
Source File: HazelcastManager.java    From lumongo with Apache License 2.0 4 votes vote down vote up
public void init(Set<HazelcastNode> nodes, String hazelcastName) throws Exception {

		// force Hazelcast to use log4j

		int hazelcastPort = localNodeConfig.getHazelcastPort();

		Config cfg = new Config();
		cfg.setProperty(GroupProperty.LOGGING_TYPE.getName(), "log4j");
		// disable Hazelcast shutdown hook to allow LuMongo to handle
		cfg.setProperty(GroupProperty.SHUTDOWNHOOK_ENABLED.getName(), "false");
		cfg.setProperty(GroupProperty.REST_ENABLED.getName(), "false");

		cfg.getGroupConfig().setName(hazelcastName);
		cfg.getGroupConfig().setPassword(hazelcastName);
		cfg.getNetworkConfig().setPortAutoIncrement(false);
		cfg.getNetworkConfig().setPort(hazelcastPort);
		cfg.setInstanceName("" + hazelcastPort);

		cfg.getManagementCenterConfig().setEnabled(false);

		NetworkConfig network = cfg.getNetworkConfig();
		JoinConfig joinConfig = network.getJoin();

		joinConfig.getMulticastConfig().setEnabled(false);
		joinConfig.getTcpIpConfig().setEnabled(true);
		for (HazelcastNode node : nodes) {
			joinConfig.getTcpIpConfig().addMember(node.getAddress() + ":" + node.getHazelcastPort());
		}

		hazelcastInstance = Hazelcast.newHazelcastInstance(cfg);
		self = hazelcastInstance.getCluster().getLocalMember();

		hazelcastInstance.getCluster().addMembershipListener(this);
		hazelcastInstance.getLifecycleService().addLifecycleListener(this);

		log.info("Initialized hazelcast");
		Set<Member> members = hazelcastInstance.getCluster().getMembers();

		Member firstMember = members.iterator().next();

		if (firstMember.equals(self)) {
			log.info("Member is owner of cluster");
			indexManager.loadIndexes();
		}

		log.info("Current cluster members: <" + members + ">");
		indexManager.openConnections(members);

		initLock.writeLock().unlock();

	}