Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HZLauncher {
- private final static Logger logger = LoggerFactory.getLogger(HZLauncher.class);
- public static final int WAIT_NODES_TIME_IN_SECOND = 1;
- public static final int MAX_WAIT_NODES_TIME_IN_SECOND = 30;
- public static void main(String args[]) throws Exception {
- int nodeCount = parseNodeCount(args, 1);
- HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(loadConfig(args));
- waitForNodes(nodeCount, hazelcastInstance);
- initStoredMaps(hazelcastInstance);
- installAllocationSamplerIfNeeded(args);
- }
- private static void initStoredMaps(HazelcastInstance hazelcastInstance) {
- final Map<String, MapConfig> mapConfigs = hazelcastInstance.getConfig().getMapConfigs();
- ArrayList<String> storesNames = new ArrayList<>();
- for (Map.Entry<String, MapConfig> ent : mapConfigs.entrySet()) {
- final String name = ent.getKey();
- final MapConfig config = ent.getValue();
- if (config.getMapStoreConfig() != null && config.getMapStoreConfig().isEnabled()) {
- storesNames.add(name);
- }
- }
- logger.info("maps with store: {}", storesNames);
- for (Map.Entry<String, MapConfig> ent : mapConfigs.entrySet()) {
- final String name = ent.getKey();
- final MapConfig config = ent.getValue();
- if (config.getMapStoreConfig() != null && config.getMapStoreConfig().isEnabled()) {
- IMap imap = hazelcastInstance.getMap(name);
- logger.info("stored map {} size: {}", imap.getName(), imap.size());
- }
- }
- logger.info("maps with store are ready");
- }
- private static void waitForNodes(int nodeCount, HazelcastInstance hazelcastInstance) throws InterruptedException {
- int i = 0;
- while (hazelcastInstance.getCluster().getMembers().size() < nodeCount &&
- i * WAIT_NODES_TIME_IN_SECOND < MAX_WAIT_NODES_TIME_IN_SECOND) {
- logger.info("wait other nodes. node count: {}, joined: {}, seconds: {}",
- nodeCount, hazelcastInstance.getCluster().getMembers().size(), i * WAIT_NODES_TIME_IN_SECOND);
- Thread.sleep(WAIT_NODES_TIME_IN_SECOND * 1000);
- i++;
- }
- Thread.sleep(2000);
- }
- private static int parseNodeCount(String[] args, int defNodeCount) {
- String nodeCountStr = getArg(args, "nodeCount");
- if (nodeCountStr != null) {
- try {
- return Integer.parseInt(nodeCountStr);
- } catch (NumberFormatException e) {
- logger.error("error parse nodeCount", e);
- }
- }
- return defNodeCount;
- }
- private static Config loadConfig(String[] args) throws FileNotFoundException {
- Config c1 = null;
- String argPath = getArg(args, "config");
- if (argPath != null) {
- String homePath = System.getProperty("user.home");
- File localConfigFile = new File(homePath + "/" + argPath);
- if (!localConfigFile.exists()) {
- c1 = new ClasspathXmlConfig(argPath);
- } else {
- c1 = new FileSystemXmlConfig(localConfigFile);
- }
- }
- return c1;
- }
- private static void installAllocationSamplerIfNeeded(String[] args) {
- final String threadOfInterest = getArg(args, "allo-sample-thread");
- final String sampleRateStr = getArg(args, "allo-sample-rate");
- final boolean sampleEnabled = sampleRateStr != null;
- if (sampleEnabled) {
- final String classFilterDesc = getArg(args, "allo-sample-class");
- final Boolean classFilterIsArray = Boolean.parseBoolean(getArg(args, "allo-sample-class-isarray"));
- AllocationRecorder.addSampler(
- new AllocationRecorderSampler(threadOfInterest,
- new AllocationRecorderSampler.ClassFilter(classFilterDesc, classFilterIsArray),
- Integer.parseInt(sampleRateStr)));
- }
- }
- private static String getArg(String[] args, String name) {
- int argPos = -1;
- for (int i = 0; i < args.length; i ++) {
- if (args[i].equals(name)) {
- argPos = i + 1;
- }
- }
- if (argPos < 0 || argPos > args.length - 1) {
- return null;
- } else {
- return args[argPos];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement