Search in sources :

Example 56 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class TransactionalMapKeySetMessageTask method innerCall.

@Override
protected Object innerCall() throws Exception {
    final TransactionContext context = endpoint.getTransactionContext(parameters.txnId);
    final TransactionalMap map = context.getMap(parameters.name);
    Set keySet = map.keySet();
    List<Data> list = new ArrayList<Data>(keySet.size());
    for (Object o : keySet) {
        list.add(serializationService.toData(o));
    }
    return list;
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) Set(java.util.Set) TransactionContext(com.hazelcast.transaction.TransactionContext) ArrayList(java.util.ArrayList) Data(com.hazelcast.internal.serialization.Data)

Example 57 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class TransactionalMapPutIfAbsentMessageTask method innerCall.

@Override
protected Object innerCall() throws Exception {
    final TransactionContext context = endpoint.getTransactionContext(parameters.txnId);
    final TransactionalMap map = context.getMap(parameters.name);
    Object response = map.putIfAbsent(parameters.key, parameters.value);
    return serializationService.toData(response);
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 58 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class TransactionalMapRemoveIfSameMessageTask method innerCall.

@Override
protected Object innerCall() throws Exception {
    final TransactionContext context = endpoint.getTransactionContext(parameters.txnId);
    final TransactionalMap map = context.getMap(parameters.name);
    return map.remove(parameters.key, parameters.value);
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) TransactionContext(com.hazelcast.transaction.TransactionContext)

Example 59 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class ClientTxnMapTest method testGetForUpdate.

@Test
public void testGetForUpdate() throws TransactionException {
    final String mapName = randomString();
    final String key = "key";
    final int initialValue = 111;
    final int value = 888;
    final CountDownLatch getKeyForUpdateLatch = new CountDownLatch(1);
    final CountDownLatch afterTryPutResult = new CountDownLatch(1);
    final IMap<String, Integer> map = client.getMap(mapName);
    map.put(key, initialValue);
    final AtomicBoolean tryPutResult = new AtomicBoolean(true);
    Runnable incrementor = new Runnable() {

        public void run() {
            try {
                getKeyForUpdateLatch.await(30, TimeUnit.SECONDS);
                boolean result = map.tryPut(key, value, 0, TimeUnit.SECONDS);
                tryPutResult.set(result);
                afterTryPutResult.countDown();
            } catch (InterruptedException e) {
            }
        }
    };
    new Thread(incrementor).start();
    client.executeTransaction(new TransactionalTask<Boolean>() {

        public Boolean execute(TransactionalTaskContext context) throws TransactionException {
            try {
                final TransactionalMap<String, Integer> txMap = context.getMap(mapName);
                txMap.getForUpdate(key);
                getKeyForUpdateLatch.countDown();
                afterTryPutResult.await(30, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
            }
            return true;
        }
    });
    assertFalse(tryPutResult.get());
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionException(com.hazelcast.transaction.TransactionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 60 with TransactionalMap

use of com.hazelcast.transaction.TransactionalMap in project hazelcast by hazelcast.

the class ClientTxnMapTest method testTnxMapContainsKey.

@Test
public void testTnxMapContainsKey() throws Exception {
    final String mapName = randomString();
    IMap map = client.getMap(mapName);
    map.put("key1", "value1");
    final TransactionContext context = client.newTransactionContext();
    context.beginTransaction();
    final TransactionalMap txMap = context.getMap(mapName);
    txMap.put("key2", "value2");
    assertTrue(txMap.containsKey("key1"));
    assertTrue(txMap.containsKey("key2"));
    assertFalse(txMap.containsKey("key3"));
    context.commitTransaction();
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) IMap(com.hazelcast.map.IMap) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

TransactionalMap (com.hazelcast.transaction.TransactionalMap)95 Test (org.junit.Test)77 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)76 QuickTest (com.hazelcast.test.annotation.QuickTest)76 HazelcastInstance (com.hazelcast.core.HazelcastInstance)63 TransactionException (com.hazelcast.transaction.TransactionException)60 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)55 NightlyTest (com.hazelcast.test.annotation.NightlyTest)42 Config (com.hazelcast.config.Config)39 TransactionContext (com.hazelcast.transaction.TransactionContext)38 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)37 MapStoreConfig (com.hazelcast.config.MapStoreConfig)32 IMap (com.hazelcast.map.IMap)28 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)27 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)14 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)7 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)6 TransactionNotActiveException (com.hazelcast.transaction.TransactionNotActiveException)6 Collection (java.util.Collection)6 ExecutionException (java.util.concurrent.ExecutionException)6