Search in sources :

Example 11 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class CacheVersionedEntryAbstractTest method testInvoke.

/**
     * @throws Exception If failed.
     */
public void testInvoke() throws Exception {
    Cache<Integer, String> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    final AtomicInteger invoked = new AtomicInteger();
    cache.invoke(100, new EntryProcessor<Integer, String, Object>() {

        @Override
        public Object process(MutableEntry<Integer, String> entry, Object... arguments) throws EntryProcessorException {
            invoked.incrementAndGet();
            CacheEntry<Integer, String> verEntry = entry.unwrap(CacheEntry.class);
            checkVersionedEntry(verEntry);
            return entry;
        }
    });
    assert invoked.get() > 0;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessorException(javax.cache.processor.EntryProcessorException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntry(org.apache.ignite.cache.CacheEntry)

Example 12 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class CacheVersionedEntryAbstractTest method testInvokeAll.

/**
     * @throws Exception If failed.
     */
public void testInvokeAll() throws Exception {
    Cache<Integer, String> cache = grid(0).cache(DEFAULT_CACHE_NAME);
    Set<Integer> keys = new HashSet<>();
    for (int i = 0; i < ENTRIES_NUM; i++) keys.add(i);
    final AtomicInteger invoked = new AtomicInteger();
    cache.invokeAll(keys, new EntryProcessor<Integer, String, Object>() {

        @Override
        public Object process(MutableEntry<Integer, String> entry, Object... arguments) throws EntryProcessorException {
            invoked.incrementAndGet();
            CacheEntry<Integer, String> verEntry = entry.unwrap(CacheEntry.class);
            checkVersionedEntry(verEntry);
            return null;
        }
    });
    assert invoked.get() > 0;
}
Also used : CacheEntry(org.apache.ignite.cache.CacheEntry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessorException(javax.cache.processor.EntryProcessorException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet)

Example 13 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class GridCacheCommandHandler method appendOrPrepend.

/**
     * Handles append and prepend commands.
     *
     * @param ctx Kernal context.
     * @param cache Cache.
     * @param key Key.
     * @param req Request.
     * @param prepend Whether to prepend.
     * @return Future of operation result.
     * @throws IgniteCheckedException In case of any exception.
     */
private static IgniteInternalFuture<?> appendOrPrepend(final GridKernalContext ctx, final IgniteInternalCache<Object, Object> cache, final Object key, GridRestCacheRequest req, final boolean prepend) throws IgniteCheckedException {
    assert cache != null;
    assert key != null;
    assert req != null;
    final Object val = req.value();
    if (val == null)
        throw new IgniteCheckedException(GridRestCommandHandlerAdapter.missingParameter("val"));
    return ctx.closure().callLocalSafe(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            EntryProcessorResult<Boolean> res = cache.invoke(key, new EntryProcessor<Object, Object, Boolean>() {

                @Override
                public Boolean process(MutableEntry<Object, Object> entry, Object... objects) throws EntryProcessorException {
                    try {
                        Object curVal = entry.getValue();
                        if (curVal == null)
                            return false;
                        // Modify current value with appendix one.
                        Object newVal = appendOrPrepend(curVal, val, !prepend);
                        // Put new value asynchronously.
                        entry.setValue(newVal);
                        return true;
                    } catch (IgniteCheckedException e) {
                        throw new EntryProcessorException(e);
                    }
                }
            });
            try {
                return res.get();
            } catch (EntryProcessorException e) {
                throw new IgniteCheckedException(e.getCause());
            }
        }
    }, false);
}
Also used : EntryProcessor(javax.cache.processor.EntryProcessor) EntryProcessorResult(javax.cache.processor.EntryProcessorResult) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) EntryProcessorException(javax.cache.processor.EntryProcessorException) MutableEntry(javax.cache.processor.MutableEntry) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) EntryProcessorException(javax.cache.processor.EntryProcessorException)

Example 14 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class CacheContinuousQueryExecuteInPrimaryTest method executeQuery.

private void executeQuery(IgniteCache<Integer, String> cache, ContinuousQuery<Integer, String> qry, boolean isTransactional) {
    try (QueryCursor<Cache.Entry<Integer, String>> qryCursor = cache.query(qry)) {
        Transaction tx = null;
        if (isTransactional)
            tx = cache.unwrap(Ignite.class).transactions().txStart();
        try {
            for (int key = 0; key < 8; key++) cache.put(key, Integer.toString(key));
            Map<Integer, String> map = new HashMap<>(8);
            for (int key = 8; key < 16; key++) map.put(key, Integer.toString(key));
            cache.putAll(map);
            if (isTransactional)
                tx.commit();
        } finally {
            if (isTransactional)
                tx.close();
        }
        for (int key = 0; key < 8; key++) {
            cache.invoke(key, new EntryProcessor<Integer, String, Object>() {

                @Override
                public Object process(MutableEntry<Integer, String> entry, Object... objects) throws EntryProcessorException {
                    entry.setValue(Integer.toString(entry.getKey() + 1));
                    return null;
                }
            });
        }
        Map<Integer, EntryProcessor<Integer, String, Object>> invokeMap = new HashMap<>(8);
        for (int key = 8; key < 16; key++) {
            invokeMap.put(key, new EntryProcessor<Integer, String, Object>() {

                @Override
                public Object process(MutableEntry<Integer, String> entry, Object... objects) throws EntryProcessorException {
                    entry.setValue(Integer.toString(entry.getKey() - 1));
                    return null;
                }
            });
        }
        cache.invokeAll(invokeMap);
    }
}
Also used : HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MutableEntry(javax.cache.processor.MutableEntry) EntryProcessor(javax.cache.processor.EntryProcessor) Transaction(org.apache.ignite.transactions.Transaction) EntryProcessorException(javax.cache.processor.EntryProcessorException)

Example 15 with EntryProcessorException

use of javax.cache.processor.EntryProcessorException in project ignite by apache.

the class IgniteClientReconnectApiExceptionTest method cacheOperationsTest.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("unchecked")
public void cacheOperationsTest() throws Exception {
    clientMode = true;
    final Ignite client = startGrid(serverCount());
    final IgniteCache<Object, Object> dfltCache = client.cache(DEFAULT_CACHE_NAME);
    assertNotNull(dfltCache);
    doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check put and get operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.getAndPut(9999, 9999);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.getAndPut(9999, 9999);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            assertEquals(9999, dfltCache.get(9999));
            return true;
        }
    }), // Check put operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.put(10000, 10000);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            dfltCache.put(10000, 10000);
            return true;
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertTrue((Boolean) o);
            assertEquals(10000, dfltCache.get(10000));
            return true;
        }
    }), // Check get operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.get(10001);
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.get(10001);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            return true;
        }
    }), // Check invoke operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.invoke(10000, new CacheEntryProcessor<Object, Object, Object>() {

                    @Override
                    public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
                        assertTrue(entry.exists());
                        return (int) entry.getValue() * 2;
                    }
                });
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.invoke(10000, new CacheEntryProcessor<Object, Object, Object>() {

                @Override
                public Object process(MutableEntry<Object, Object> entry, Object... arguments) throws EntryProcessorException {
                    assertTrue(entry.exists());
                    return (int) entry.getValue() * 2;
                }
            });
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNotNull(o);
            assertEquals(20000, (int) o);
            return true;
        }
    }), // Check put async operation.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                dfltCache.putAsync(10002, 10002).get();
            } catch (CacheException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return dfltCache.putAsync(10002, 10002).get();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            assertNull(o);
            assertEquals(10002, dfltCache.get(10002));
            return true;
        }
    }), // Check transaction.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.transactions();
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.transactions();
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteTransactions txs = (IgniteTransactions) o;
            assertNotNull(txs);
            return true;
        }
    }), // Check get cache.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.cache(DEFAULT_CACHE_NAME);
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.cache(DEFAULT_CACHE_NAME);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCache<Object, Object> cache0 = (IgniteCache<Object, Object>) o;
            assertNotNull(cache0);
            cache0.put(1, 1);
            assertEquals(1, cache0.get(1));
            return true;
        }
    }), // Check streamer.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.dataStreamer(DEFAULT_CACHE_NAME);
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.dataStreamer(DEFAULT_CACHE_NAME);
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteDataStreamer<Object, Object> streamer = (IgniteDataStreamer<Object, Object>) o;
            streamer.addData(2, 2);
            streamer.close();
            assertEquals(2, client.cache(DEFAULT_CACHE_NAME).get(2));
            return true;
        }
    }), // Check create cache.
    new T2<Callable, C1<Object, Boolean>>(new Callable() {

        @Override
        public Object call() throws Exception {
            boolean failed = false;
            try {
                client.createCache("test_cache");
            } catch (IgniteClientDisconnectedException e) {
                failed = true;
                checkAndWait(e);
            }
            assertTrue(failed);
            return client.createCache("test_cache");
        }
    }, new C1<Object, Boolean>() {

        @Override
        public Boolean apply(Object o) {
            IgniteCache<Object, Object> cache = (IgniteCache<Object, Object>) o;
            assertNotNull(cache);
            cache.put(1, 1);
            assertEquals(1, cache.get(1));
            return true;
        }
    })));
    clientMode = false;
}
Also used : CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteCache(org.apache.ignite.IgniteCache) IgniteTransactions(org.apache.ignite.IgniteTransactions) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheException(javax.cache.CacheException) IgniteClientDisconnectedException(org.apache.ignite.IgniteClientDisconnectedException) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) CacheEntryProcessor(org.apache.ignite.cache.CacheEntryProcessor) Ignite(org.apache.ignite.Ignite) MutableEntry(javax.cache.processor.MutableEntry) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

EntryProcessorException (javax.cache.processor.EntryProcessorException)16 MutableEntry (javax.cache.processor.MutableEntry)5 Ignite (org.apache.ignite.Ignite)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 CacheException (javax.cache.CacheException)4 IgniteCache (org.apache.ignite.IgniteCache)4 IgniteException (org.apache.ignite.IgniteException)4 Transaction (org.apache.ignite.transactions.Transaction)4 HashMap (java.util.HashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 CacheEntryProcessor (org.apache.ignite.cache.CacheEntryProcessor)3 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)3 Data (com.hazelcast.nio.serialization.Data)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 EntryProcessor (javax.cache.processor.EntryProcessor)2 EntryProcessorResult (javax.cache.processor.EntryProcessorResult)2 IgniteTransactions (org.apache.ignite.IgniteTransactions)2