Java Code Examples for org.apache.ignite.IgniteCache#future()

The following examples show how to use org.apache.ignite.IgniteCache#future() . 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: IgnteCacheClientWriteBehindStoreNonCoalescingTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Update random key in async mode.
 *
 * @param cache Cache to use.
 * @return IgniteFuture.
 */
private IgniteFuture<?> updateKey(IgniteCache<Integer, Integer> cache) {
    IgniteCache asyncCache = cache.withAsync();

    // Using EntryProcessor.invokeAll to increment every value in place.
    asyncCache.invoke(rnd.nextInt(100), new EntryProcessor<Integer, Integer, Object>() {
        @Override public Object process(MutableEntry<Integer, Integer> entry, Object... arguments)
            throws EntryProcessorException {
            entry.setValue(entry.getValue() + 1);

            return null;
        }
    });

    return asyncCache.future();
}
 
Example 2
Source File: HelloIgniteAsyn.java    From ignite-book-code-samples with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
    System.out.println("Hello Ignite Asynchronous!!");
    // create a new instance of TCP Discovery SPI
    TcpDiscoverySpi spi = new TcpDiscoverySpi();
    // create a new instance of tcp discovery multicast ip finder
    TcpDiscoveryMulticastIpFinder tcMp = new TcpDiscoveryMulticastIpFinder();
    tcMp.setAddresses(Arrays.asList("localhost")); // change your IP address here
    // set the multi cast ip finder for spi
    spi.setIpFinder(tcMp);
    // create new ignite configuration
    IgniteConfiguration cfg = new IgniteConfiguration();
    cfg.setClientMode(false);
    // set the discovery spi to ignite configuration
    cfg.setDiscoverySpi(spi);
    // Start ignite
    Ignite ignite = Ignition.start(cfg);

    // get or create cache
    IgniteCache<Integer, String> cache = ignite.getOrCreateCache("testCache");
    // get an asynchronous cache
    IgniteCache<Integer, String> asynCache = cache.withAsync();

    // put some cache elements
    for(int i = 1; i <= 100; i++){
        cache.put(i, Integer.toString(i));
    }

    String val = asynCache.withAsync().get(1);
    System.out.println("Non future call:" + val);
    IgniteFuture<String> igniteFuture = asynCache.future();



    igniteFuture.listen(f-> System.out.println("Cache Value:" + f.get()));
    ignite.close();

}
 
Example 3
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception In case of error.
 */
@Test
public void testGetAndPutAsyncOld() throws Exception {
    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    cache.put("key1", 1);
    cache.put("key2", 2);

    cacheAsync.getAndPut("key1", 10);

    IgniteFuture<Integer> fut1 = cacheAsync.future();

    cacheAsync.getAndPut("key2", 11);

    IgniteFuture<Integer> fut2 = cacheAsync.future();

    assertEquals((Integer)1, fut1.get(5000));
    assertEquals((Integer)2, fut2.get(5000));

    assertEquals((Integer)10, cache.get("key1"));
    assertEquals((Integer)11, cache.get("key2"));
}
 
Example 4
Source File: IgniteCacheConfigVariationsFullApiTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutIfAbsentAsyncOld() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;

    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    try {
        cacheAsync.getAndPutIfAbsent("key", 1);

        IgniteFuture<Integer> fut1 = cacheAsync.future();

        assertNull(fut1.get());
        assertEquals((Integer)1, cache.get("key"));

        cacheAsync.getAndPutIfAbsent("key", 2);

        IgniteFuture<Integer> fut2 = cacheAsync.future();

        assertEquals((Integer)1, fut2.get());
        assertEquals((Integer)1, cache.get("key"));

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    if (!storeEnabled())
        return;

    // Check swap.
    cache.put("key2", 1);

    cache.localEvict(Collections.singleton("key2"));

    if (!isLoadPreviousValue())
        cache.get("key2");

    cacheAsync.getAndPutIfAbsent("key2", 3);

    assertEquals((Integer)1, cacheAsync.<Integer>future().get());

    // Check db.
    if (storeEnabled() && isLoadPreviousValue() && !isMultiJvm()) {
        putToStore("key3", 3);

        cacheAsync.getAndPutIfAbsent("key3", 4);

        assertEquals((Integer)3, cacheAsync.<Integer>future().get());
    }

    cache.localEvict(Collections.singleton("key2"));

    if (!isLoadPreviousValue())
        cache.get("key2");

    // Same checks inside tx.
    tx = txShouldBeUsed() ? transactions().txStart() : null;

    try {
        cacheAsync.getAndPutIfAbsent("key2", 3);

        assertEquals(1, cacheAsync.future().get());

        if (tx != null)
            tx.commit();

        assertEquals((Integer)1, cache.get("key2"));
    }
    finally {
        if (tx != null)
            tx.close();
    }
}
 
Example 5
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception In case of error.
 */
@Test
public void testGetAndPutAsyncOld() throws Exception {
    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    cache.put("key1", 1);
    cache.put("key2", 2);

    cacheAsync.getAndPut("key1", 10);

    IgniteFuture<Integer> fut1 = cacheAsync.future();

    cacheAsync.getAndPut("key2", 11);

    IgniteFuture<Integer> fut2 = cacheAsync.future();

    assertEquals((Integer)1, fut1.get(5000));
    assertEquals((Integer)2, fut2.get(5000));

    assertEquals((Integer)10, cache.get("key1"));
    assertEquals((Integer)11, cache.get("key2"));
}
 
Example 6
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testGetAndPutIfAbsentAsyncOld() throws Exception {
    Transaction tx = txShouldBeUsed() ? transactions().txStart() : null;

    IgniteCache<String, Integer> cache = jcache();

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    try {
        cacheAsync.getAndPutIfAbsent("key", 1);

        IgniteFuture<Integer> fut1 = cacheAsync.future();

        assertNull(fut1.get());
        assertEquals((Integer)1, cache.get("key"));

        cacheAsync.getAndPutIfAbsent("key", 2);

        IgniteFuture<Integer> fut2 = cacheAsync.future();

        assertEquals((Integer)1, fut2.get());
        assertEquals((Integer)1, cache.get("key"));

        if (tx != null)
            tx.commit();
    }
    finally {
        if (tx != null)
            tx.close();
    }

    // Check swap.
    cache.put("key2", 1);

    cache.localEvict(Collections.singleton("key2"));

    cacheAsync.getAndPutIfAbsent("key2", 3);

    assertEquals((Integer)1, cacheAsync.<Integer>future().get());

    // Check db.
    if (!isMultiJvm()) {
        storeStgy.putToStore("key3", 3);

        cacheAsync.getAndPutIfAbsent("key3", 4);

        assertEquals((Integer)3, cacheAsync.<Integer>future().get());
    }

    cache.localEvict(Collections.singleton("key2"));

    // Same checks inside tx.
    tx = txShouldBeUsed() ? transactions().txStart() : null;

    try {
        cacheAsync.getAndPutIfAbsent("key2", 3);

        assertEquals(1, cacheAsync.future().get());

        if (tx != null)
            tx.commit();

        assertEquals((Integer)1, cache.get("key2"));
    }
    finally {
        if (tx != null)
            tx.close();
    }
}
 
Example 7
Source File: GridCacheAbstractFullApiSelfTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testInvokeAsyncOld() throws Exception {
    IgniteCache<String, Integer> cache = jcache();

    cache.put("key2", 1);
    cache.put("key3", 3);

    IgniteCache<String, Integer> cacheAsync = cache.withAsync();

    assertNull(cacheAsync.invoke("key1", INCR_PROCESSOR));

    IgniteFuture<?> fut0 = cacheAsync.future();

    assertNull(cacheAsync.invoke("key2", INCR_PROCESSOR));

    IgniteFuture<?> fut1 = cacheAsync.future();

    assertNull(cacheAsync.invoke("key3", RMV_PROCESSOR));

    IgniteFuture<?> fut2 = cacheAsync.future();

    fut0.get();
    fut1.get();
    fut2.get();

    assertEquals((Integer)1, cache.get("key1"));
    assertEquals((Integer)2, cache.get("key2"));
    assertNull(cache.get("key3"));

    for (int i = 0; i < gridCount(); i++)
        assertNull(jcache(i).localPeek("key3", ONHEAP));
}