Package com.hazelcast.core

Examples of com.hazelcast.core.IdGenerator


        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
        ServiceReference reference = context.getServiceReference("com.hazelcast.core.HazelcastInstance");
        HazelcastInstance instance = (HazelcastInstance) context.getService(reference);
        context.ungetService(reference);
        try {
            IdGenerator idGenerator = instance.getIdGenerator("cellar-smaple-generator");
            Long id = idGenerator.newId();
            topic = instance.getTopic("cellar-sample-topic");
            topic.addMessageListener(messageListener);
            topic.publish(new Message("id="+id));
        } catch (Exception ex) {
            ex.printStackTrace();
View Full Code Here


        testInit(1, true, 2);
        testInit(10, true, 11);
    }

    private void testInit(int initialValue, boolean expected, long expectedValue) {
        IdGenerator idGenerator = createIdGenerator();

        boolean initialized = idGenerator.init(initialValue);
        assertEquals(expected, initialized);

        long newId = idGenerator.newId();
        assertEquals(expectedValue, newId);
    }
View Full Code Here

        return hz.getIdGenerator("id-" + UUID.randomUUID().toString());
    }

    @Test
    public void testInitWhenAlreadyInitialized() {
        IdGenerator idGenerator = createIdGenerator();
        long first = idGenerator.newId();

        boolean initialized = idGenerator.init(10);
        assertFalse(initialized);

        long actual = idGenerator.newId();
        assertEquals(first + 1, actual);
    }
View Full Code Here

        assertEquals(first + 1, actual);
    }

    @Test
    public void testNewId_withExplicitInit() {
        IdGenerator idGenerator =createIdGenerator();
        assertTrue(idGenerator.init(10));

        long result = idGenerator.newId();
        assertEquals(11, result);
    }
View Full Code Here

        assertEquals(11, result);
    }

    @Test
    public void testNewId_withoutExplictInit() {
        IdGenerator idGenerator = createIdGenerator();
        long result = idGenerator.newId();
        assertEquals(0, result);
    }
View Full Code Here

        assertEquals(0, result);
    }

    @Test
    public void testGeneratingMultipleBlocks() {
        IdGenerator idGenerator = createIdGenerator();

        long expected = 0;
        for (int k = 0; k < 3 * IdGeneratorProxy.BLOCK_SIZE; k++) {
            assertEquals(expected, idGenerator.newId());
            expected++;
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testDestroy() {
        IdGenerator idGenerator = createIdGenerator();
        String id = idGenerator.getName();
        idGenerator.newId();
        idGenerator.newId();

        idGenerator.destroy();

        IdGenerator newIdGenerator = hz.getIdGenerator(id);
        long actual = newIdGenerator.newId();
        assertEquals(0, actual);
    }
View Full Code Here

    public void testInitFailsOnNegativeValues() {
        assertFalse(idGenerator.init(-1));
    }

    private static void assertNewIdAfterInit(int initialValue) {
        IdGenerator idGenerator = createIdGenerator();

        assertTrue(idGenerator.init(initialValue));

        assertEquals(initialValue + 1, idGenerator.newId());
    }
View Full Code Here

    @Test
    public void testIdGenerator() throws Exception {
        String partitionKey = "hazelcast";
        HazelcastInstance hz = getHazelcastInstance(partitionKey);

        IdGenerator idGenerator = hz.getIdGenerator("idgenerator@" + partitionKey);
        idGenerator.newId();
        assertEquals("idgenerator@" + partitionKey, idGenerator.getName());
        assertEquals(partitionKey, idGenerator.getPartitionKey());

        AtomicLongService service = getNodeEngine(hz).getService(AtomicLongService.SERVICE_NAME);
        assertTrue(service.containsAtomicLong("hz:atomic:idGenerator:" + idGenerator.getName()));
    }
View Full Code Here

   * A reasonable method for generating unique version ids: combining a timestamp (in seconds) with a distributed unique
   * Id from Hazelcast. In this way if the Hazelcast counter is restarted (for example after a cluster shutdown or
   * restart) then the Ids will still be unique afterwards.
   */
  public long uniqueVersionId() {
    IdGenerator idGenerator = hz.getIdGenerator(CoordinationStructures.VERSION_GENERATOR);
    long id = idGenerator.newId();
    int timeSeconds = (int) (System.currentTimeMillis() / 1000);
    long paddedId = id << 32;
    return paddedId + timeSeconds;
  }
View Full Code Here

TOP

Related Classes of com.hazelcast.core.IdGenerator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.