Search in sources :

Example 1 with ClientCache

use of org.apache.ignite.client.ClientCache in project ignite by apache.

the class JavaThinClient method transactionConfiguration.

@Test
void transactionConfiguration() {
    // tag::transaction-config[]
    ClientConfiguration cfg = new ClientConfiguration();
    cfg.setAddresses("localhost:10800");
    cfg.setTransactionConfiguration(new ClientTransactionConfiguration().setDefaultTxTimeout(10000).setDefaultTxConcurrency(TransactionConcurrency.OPTIMISTIC).setDefaultTxIsolation(TransactionIsolation.REPEATABLE_READ));
    IgniteClient client = Ignition.startClient(cfg);
    // end::transaction-config[]
    ClientCache cache = client.createCache(new ClientCacheConfiguration().setName("test").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
    // tag::tx-custom-properties[]
    ClientTransactions tx = client.transactions();
    try (ClientTransaction t = tx.txStart(TransactionConcurrency.OPTIMISTIC, TransactionIsolation.REPEATABLE_READ)) {
        cache.put(1, "new value");
        t.commit();
    }
// end::tx-custom-properties[]
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) ClientTransaction(org.apache.ignite.client.ClientTransaction) ClientTransactionConfiguration(org.apache.ignite.configuration.ClientTransactionConfiguration) ClientCache(org.apache.ignite.client.ClientCache) ClientTransactions(org.apache.ignite.client.ClientTransactions) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) Test(org.junit.jupiter.api.Test)

Example 2 with ClientCache

use of org.apache.ignite.client.ClientCache in project ignite by apache.

the class CacheEntryListenersTest method testListenersWithRemoteFilter.

/**
 * Test continuous queries and JCache entry listeners with remote filters.
 */
@Test
@SuppressWarnings("deprecation")
public void testListenersWithRemoteFilter() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testListenersWithRmtFilter");
        CacheEntryEventSerializableFilter<Integer, Integer> rmtFilter = evt -> (evt.getKey() & 1) == 0;
        ContinuousQueryListener<Integer, Integer> lsnr1 = new ContinuousQueryListener<>();
        ContinuousQueryListener<Integer, Integer> lsnr2 = new ContinuousQueryListener<>();
        cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr1).setRemoteFilterFactory(() -> rmtFilter));
        cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr2).setRemoteFilter(rmtFilter));
        JCacheEntryListener<Integer, Integer> lsnr3 = new JCacheEntryListener<>();
        cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr3, () -> rmtFilter, true, false));
        for (int i = 0; i < 10; i++) cache.put(i, i);
        Map<EventType, Map<Integer, Integer>> expRes = F.asMap(EventType.CREATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> i * 2)));
        assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
        for (int i = 0; i < 10; i++) cache.put(i, -i);
        expRes = F.asMap(EventType.UPDATED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
        assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
        for (int i = 0; i < 10; i++) cache.remove(i);
        expRes = F.asMap(EventType.REMOVED, IntStream.range(0, 5).boxed().collect(Collectors.toMap(i -> i * 2, i -> -i * 2)));
        assertEquals(expRes, aggregateListenerEvents(lsnr1, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr2, 5));
        assertEquals(expRes, aggregateListenerEvents(lsnr3, 5));
        assertTrue(lsnr1.isQueueEmpty());
        assertTrue(lsnr2.isQueueEmpty());
        assertTrue(lsnr3.isQueueEmpty());
    }
}
Also used : IntStream(java.util.stream.IntStream) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) Person(org.apache.ignite.client.Person) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) ClientCache(org.apache.ignite.client.ClientCache) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Cache(javax.cache.Cache) CacheEntryCreatedListener(javax.cache.event.CacheEntryCreatedListener) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Duration(javax.cache.expiry.Duration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EnumMap(java.util.EnumMap) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) T2(org.apache.ignite.internal.util.typedef.T2) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryRemovedListener(javax.cache.event.CacheEntryRemovedListener) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) EventType(javax.cache.event.EventType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 3 with ClientCache

use of org.apache.ignite.client.ClientCache in project ignite by apache.

the class JavaThinCompatibilityTest method testContinuousQueries.

/**
 */
private void testContinuousQueries() throws Exception {
    X.println(">>>> Testing continuous queries");
    try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(ADDR))) {
        ClientCache<Object, Object> cache = client.getOrCreateCache("testContinuousQueries");
        List<CacheEntryEvent<?, ?>> allEvts = new ArrayList<>();
        cache.query(new ContinuousQuery<>().setLocalListener(evts -> evts.forEach(allEvts::add)));
        cache.put(0, 0);
        cache.put(0, 1);
        cache.remove(0);
        assertTrue(GridTestUtils.waitForCondition(() -> allEvts.size() == 3, 1_000L));
    }
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) BinaryObject(org.apache.ignite.binary.BinaryObject) ClientFeatureNotSupportedByServerException(org.apache.ignite.client.ClientFeatureNotSupportedByServerException) ClientCache(org.apache.ignite.client.ClientCache) ServiceContext(org.apache.ignite.services.ServiceContext) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Cache(javax.cache.Cache) IgniteBinary(org.apache.ignite.IgniteBinary) QueryEntity(org.apache.ignite.cache.QueryEntity) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) Parameterized(org.junit.runners.Parameterized) SERVICE_INVOKE_CALLCTX(org.apache.ignite.internal.client.thin.ProtocolBitmaskFeature.SERVICE_INVOKE_CALLCTX) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) GET_SERVICE_DESCRIPTORS(org.apache.ignite.internal.client.thin.ProtocolBitmaskFeature.GET_SERVICE_DESCRIPTORS) IgniteException(org.apache.ignite.IgniteException) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) IgniteProductVersion(org.apache.ignite.lang.IgniteProductVersion) NotNull(org.jetbrains.annotations.NotNull) ScanQuery(org.apache.ignite.cache.query.ScanQuery) Person(org.apache.ignite.client.Person) ComputeJob(org.apache.ignite.compute.ComputeJob) ComputeTaskAdapter(org.apache.ignite.compute.ComputeTaskAdapter) RunWith(org.junit.runner.RunWith) PlatformType(org.apache.ignite.platform.PlatformType) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ClusterNode(org.apache.ignite.cluster.ClusterNode) ComputeJobResult(org.apache.ignite.compute.ComputeJobResult) ClientTransaction(org.apache.ignite.client.ClientTransaction) IgniteClient(org.apache.ignite.client.IgniteClient) PlatformExpiryPolicy(org.apache.ignite.internal.processors.platform.cache.expiry.PlatformExpiryPolicy) Assume(org.junit.Assume) Duration(javax.cache.expiry.Duration) F(org.apache.ignite.internal.util.typedef.F) ServiceCallContext(org.apache.ignite.services.ServiceCallContext) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Ignite(org.apache.ignite.Ignite) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ClientServiceDescriptor(org.apache.ignite.client.ClientServiceDescriptor) TimeUnit(java.util.concurrent.TimeUnit) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Ignition(org.apache.ignite.Ignition) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) ServiceContextResource(org.apache.ignite.resources.ServiceContextResource) Service(org.apache.ignite.services.Service) CacheMode(org.apache.ignite.cache.CacheMode) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteClient(org.apache.ignite.client.IgniteClient) ArrayList(java.util.ArrayList) BinaryObject(org.apache.ignite.binary.BinaryObject) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration)

Example 4 with ClientCache

use of org.apache.ignite.client.ClientCache in project ignite by apache.

the class CacheEntryListenersTest method testJCacheListeners.

/**
 * Test JCache entry listeners.
 */
@Test
public void testJCacheListeners() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testJCacheListeners");
        JCacheEntryListener<Integer, Integer> lsnr = new JCacheEntryListener<>();
        cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr, null, true, false));
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertEquals(F.asMap(EventType.CREATED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> i))), aggregateListenerEvents(lsnr, 10));
        for (int i = 0; i < 10; i++) cache.put(i, -i);
        assertEquals(F.asMap(EventType.UPDATED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> -i))), aggregateListenerEvents(lsnr, 10));
        for (int i = 0; i < 10; i++) cache.remove(i);
        assertEquals(F.asMap(EventType.REMOVED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> -i))), aggregateListenerEvents(lsnr, 10));
        assertTrue(lsnr.isQueueEmpty());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) Person(org.apache.ignite.client.Person) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) ClientCache(org.apache.ignite.client.ClientCache) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Cache(javax.cache.Cache) CacheEntryCreatedListener(javax.cache.event.CacheEntryCreatedListener) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Duration(javax.cache.expiry.Duration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EnumMap(java.util.EnumMap) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) T2(org.apache.ignite.internal.util.typedef.T2) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryRemovedListener(javax.cache.event.CacheEntryRemovedListener) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) IgniteClient(org.apache.ignite.client.IgniteClient) Test(org.junit.Test)

Example 5 with ClientCache

use of org.apache.ignite.client.ClientCache in project ignite by apache.

the class CacheEntryListenersTest method testContinuousQueries.

/**
 * Test continuous queries.
 */
@Test
public void testContinuousQueries() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Integer, Integer> cache = client.getOrCreateCache("testCQ");
        ContinuousQueryListener<Integer, Integer> lsnr = new ContinuousQueryListener<>();
        cache.query(new ContinuousQuery<Integer, Integer>().setLocalListener(lsnr));
        for (int i = 0; i < 10; i++) cache.put(i, i);
        assertEquals(F.asMap(EventType.CREATED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> i))), aggregateListenerEvents(lsnr, 10));
        for (int i = 0; i < 10; i++) cache.put(i, -i);
        assertEquals(F.asMap(EventType.UPDATED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> -i))), aggregateListenerEvents(lsnr, 10));
        for (int i = 0; i < 10; i++) cache.remove(i);
        assertEquals(F.asMap(EventType.REMOVED, IntStream.range(0, 10).boxed().collect(Collectors.toMap(i -> i, i -> -i))), aggregateListenerEvents(lsnr, 10));
        assertTrue(lsnr.isQueueEmpty());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntStream(java.util.stream.IntStream) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) Person(org.apache.ignite.client.Person) GridTestUtils.waitForCondition(org.apache.ignite.testframework.GridTestUtils.waitForCondition) CacheEntryEventSerializableFilter(org.apache.ignite.cache.CacheEntryEventSerializableFilter) U(org.apache.ignite.internal.util.typedef.internal.U) HashMap(java.util.HashMap) CacheEntryExpiredListener(javax.cache.event.CacheEntryExpiredListener) ClientCache(org.apache.ignite.client.ClientCache) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) Map(java.util.Map) Cache(javax.cache.Cache) CacheEntryCreatedListener(javax.cache.event.CacheEntryCreatedListener) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) Duration(javax.cache.expiry.Duration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) F(org.apache.ignite.internal.util.typedef.F) EventType(javax.cache.event.EventType) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) EnumMap(java.util.EnumMap) FactoryBuilder(javax.cache.configuration.FactoryBuilder) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) UUID(java.util.UUID) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) T2(org.apache.ignite.internal.util.typedef.T2) GridTestUtils(org.apache.ignite.testframework.GridTestUtils) TimeUnit(java.util.concurrent.TimeUnit) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryRemovedListener(javax.cache.event.CacheEntryRemovedListener) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) ScanQuery(org.apache.ignite.cache.query.ScanQuery) ClientConnectorConfiguration(org.apache.ignite.configuration.ClientConnectorConfiguration) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteClient(org.apache.ignite.client.IgniteClient) Test(org.junit.Test)

Aggregations

ClientCache (org.apache.ignite.client.ClientCache)7 IgniteClient (org.apache.ignite.client.IgniteClient)7 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)6 ThinClientConfiguration (org.apache.ignite.configuration.ThinClientConfiguration)6 Map (java.util.Map)5 TimeUnit (java.util.concurrent.TimeUnit)5 Cache (javax.cache.Cache)5 CacheEntryEvent (javax.cache.event.CacheEntryEvent)5 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)5 Duration (javax.cache.expiry.Duration)5 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)5 ScanQuery (org.apache.ignite.cache.query.ScanQuery)5 Person (org.apache.ignite.client.Person)5 ClientConnectorConfiguration (org.apache.ignite.configuration.ClientConnectorConfiguration)5 F (org.apache.ignite.internal.util.typedef.F)5 EnumMap (java.util.EnumMap)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 UUID (java.util.UUID)4