com.hazelcast.core.Hazelcast类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(230)

本文整理了Java中com.hazelcast.core.Hazelcast类的一些代码示例,展示了Hazelcast类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Hazelcast类的具体详情如下:
包路径:com.hazelcast.core.Hazelcast
类名称:Hazelcast

Hazelcast介绍

[英]Factory for HazelcastInstance's, a node in a cluster.
[中]HazelcastInstance的工厂,集群中的节点。

代码示例

代码示例来源:origin: SonarSource/sonarqube

public HazelcastMember build() {
 Config config = new Config();
 config.getGroupConfig().setName("SonarQube");
 NetworkConfig netConfig = config.getNetworkConfig();
 netConfig
  .setPort(port)
  .setPortAutoIncrement(false)
  .setReuseAddress(true);
 netConfig.getInterfaces()
  .setEnabled(true)
 JoinConfig joinConfig = netConfig.getJoin();
 joinConfig.getAwsConfig().setEnabled(false);
 joinConfig.getMulticastConfig().setEnabled(false);
 joinConfig.getTcpIpConfig().setEnabled(true);
 joinConfig.getTcpIpConfig().setMembers(requireNonNull(members, "Members are missing"));
 attributes.setStringAttribute(Attribute.PROCESS_KEY.getKey(), requireNonNull(processId, "Process key is missing").getKey());
 return new HazelcastMemberImpl(Hazelcast.newHazelcastInstance(config));

代码示例来源:origin: apache/usergrid

public void destroy() {
  logger.info( "Shutting down Hazelcast" );
  Hazelcast.shutdownAll();
  logger.info( "Hazelcast shutdown" );
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

/**
   * Create a Hazelcast instance for testing, a server instance not connected to others, so turn off discovery.
   *
   * @return A standalone server instance, auto-closeable
   */
  @Bean
  public HazelcastInstance hazelcastInstance() {
    Config config = new Config();

    config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);

    return Hazelcast.newHazelcastInstance(config);
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    IMap<String, Customer> map = hz.getMap("map");

    map.put("1", new Customer("peter", true, 36));
    map.put("2", new Customer("john", false, 40));
    map.put("3", new Customer("roger", true, 20));

    Set<Customer> employees = (Set<Customer>) map.values(new SqlPredicate("active AND age < 30"));
    System.out.println("Employees: " + employees);

    Hazelcast.shutdownAll();
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

mapConfig.setName(NAME).setQuorumName(NAME);
Config config = new Config();
config.addMapConfig(mapConfig);
config.addQuorumConfig(quorumConfig);
HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
IMap<String, String> map = instance1.getMap(NAME);
map.put("key1", "we have the quorum");
String value = map.get("key1");
System.out.println("'key1' has the value '" + value + "'");
instance2.getLifecycleService().shutdown();
  map.put("key2", "will not succeed");
} catch (QuorumException expected) {
  System.out.println("Put operation failed with expected QuorumException: " + expected.getMessage());
Hazelcast.shutdownAll();

代码示例来源:origin: hazelcast/hazelcast-code-samples

private static HazelcastInstance newHazelcastInstance(int port) {
  Config config = new Config();
  config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
  config.getNetworkConfig().setPort(port).setPortAutoIncrement(false);
  JoinConfig join = config.getNetworkConfig().getJoin();
  join.getMulticastConfig().setEnabled(false);
  join.getTcpIpConfig().setEnabled(true).clear()
      .addMember("127.0.0.1:5701")
      .addMember("127.0.0.1:5702");
  HotRestartPersistenceConfig hotRestartConfig = config.getHotRestartPersistenceConfig();
  hotRestartConfig.setEnabled(true).setBaseDir(new File(HOT_RESTART_ROOT_DIR + port));
  return Hazelcast.newHazelcastInstance(config);
}

代码示例来源:origin: mpilone/hazelcastmq

Config config = new Config();
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config);
 hazelcast.getLifecycleService().shutdown();

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    Map<String, Person> map = hz.getMap("map");

    map.put("Peter", new Person("Peter"));

    Person person = map.get("Peter");
    System.out.println(person);

    Hazelcast.shutdownAll();
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    Config config = new Config();
    config.getGroupConfig().setName("name");
    config.getGroupConfig().setPassword("pwd");
    config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
    config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
    config.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1:5701");

    Hazelcast.newHazelcastInstance(config);
    ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");

    System.out.println("#######  CLIENT BEGIN #######");
    HazelcastInstance client = (HazelcastInstance) context.getBean("client");
    IMap<String, String> map = client.getMap("map");
    map.put("city", "Istanbul");
    System.out.println("City: " + map.get("city"));
    System.out.println("#######  CLIENT END #######");

    Hazelcast.shutdownAll();
    HazelcastClient.shutdownAll();
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
  HazelcastInstance hz = Hazelcast.newHazelcastInstance();
  IMap<String, String> map = hz.getMap("themap");
  map.addInterceptor(new MyMapInterceptor());
  map.put("1", "1");
  System.out.println(map.get("1"));
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    Config config = new Config();
    config.getSerializationConfig()
        .addPortableFactory(SamplePortableFactory.FACTORY_ID, new SamplePortableFactory());
    // Start the Embedded Hazelcast Cluster Member.
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
    //Customer can be used here
    hz.shutdown();
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
  Config config = new Config();
  config.getSerializationConfig().addPortableFactory(PersonPortableFactory.FACTORY_ID, new PersonPortableFactory());
  HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
  IMap<Integer, PersonPortable> map = hz.getMap("map");
  map.put(1, new PersonPortable("Georg", limb("left-leg"), limb("right-leg")));
  map.put(2, new PersonPortable("Peter", limb("left-hand"), limb("right-hand")));
  map.put(3, new PersonPortable("Hans", limb("left-leg"), limb("right-leg")));
  map.put(4, new PersonPortable("Stefanie", limb("left-arm"), limb("right-arm")));
  Set<PersonPortable> employees = (Set<PersonPortable>) map.values(new SqlPredicate("limbs[any].name == right-leg"));
  System.out.println("People: " + employees);
  Hazelcast.shutdownAll();
}

代码示例来源:origin: lumongo/lumongo

Config cfg = new Config();
cfg.setProperty(GroupProperty.LOGGING_TYPE.getName(), "log4j");
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);
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);

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
  Config config = new Config();
  config.getMapConfig("map0").setBackupCount(0);
  config.getMapConfig("map1").setBackupCount(1);
  HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
  HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
  IMap<Object, Object> map0 = instance1.getMap("map0");
  map0.put(0, 0);
  map0.addPartitionLostListener(new MapPartitionLostListener() {
    @Override
    public void partitionLost(MapPartitionLostEvent event) {
  IMap<Object, Object> map1 = instance1.getMap("map1");
  map1.addPartitionLostListener(new MapPartitionLostListener() {
    @Override
    public void partitionLost(MapPartitionLostEvent event) {
  instance2.getLifecycleService().terminate();

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    Config config = new Config();
    config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
    Map<Long, Customer> customerMap = hz.getMap("customers");
    Map<OrderKey, Order> orderMap = hz.getMap("orders");

    long customerId = 100;
    long orderId = 200;
    long articleId = 300;
    Customer customer = new Customer(customerId);
    customerMap.put(customerId, customer);
    OrderKey orderKey = new OrderKey(orderId, customer.getId());
    Order order = new Order(orderKey.getOrderId(), customer.getId(), articleId);
    orderMap.put(orderKey, order);

    PartitionService pService = hz.getPartitionService();
    Partition cPartition = pService.getPartition(customerId);
    Partition oPartition = pService.getPartition(orderKey);
    Partition wPartition = pService.getPartition(orderId);
    System.out.printf("Partition for customer: %s\n", cPartition.getPartitionId());
    System.out.printf("Partition for order with OrderKey: %s\n", oPartition.getPartitionId());
    System.out.printf("Partition for order without OrderKey: %s\n", wPartition.getPartitionId());
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
  HazelcastInstance hz = Hazelcast.newHazelcastInstance();
  IMap<String, Employee> employees = hz.getMap("employees");
  employees.put("John", new Employee(1000));
  employees.put("Mark", new Employee(1000));
  employees.put("Spencer", new Employee(1000));
  employees.executeOnEntries(new EmployeeRaiseEntryProcessor());
  for (Map.Entry<String, Employee> entry : employees.entrySet()) {
    System.out.println(entry.getKey() + " salary: " + entry.getValue().getSalary());
  }
  Hazelcast.shutdownAll();
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(null);
    HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(null);

    // initialize partitions
    instance1.getMap("map1").put(0, 0);

    instance1.getPartitionService().addPartitionLostListener(new PartitionLostListener() {
      @Override
      public void partitionLost(PartitionLostEvent event) {
        System.out.println("Instance2 has lost a partition for data with 0 backup! " + event);
      }
    });

    instance2.getLifecycleService().terminate();
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    IMap<Long, Person> personMap = hz.getMap("personMap");
    Person p = personMap.get(1L);
    System.out.println(p);
    hz.shutdown();
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) throws Exception {
    Config config = new Config();
    config.setManagedContext(new ManagedContextImpl());
    HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);

    Map<String, DummyObject> map = hz.getMap("map");
    DummyObject input = new DummyObject();
    System.out.println(input);

    map.put("1", input);
    DummyObject output = map.get("1");
    System.out.println(output);

    Hazelcast.shutdownAll();
  }
}

代码示例来源:origin: hazelcast/hazelcast-code-samples

public static void main(String[] args) {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    IMap<Long, Person> personMap = hz.getMap("personMap");
    personMap.put(1L, new Person(1L, "Peter"));
    hz.shutdown();
  }
}

相关文章