Search in sources :

Example 51 with Transaction

use of redis.clients.jedis.Transaction in project vertigo by KleeGroup.

the class RedisNodeRegistryPlugin method register.

@Override
public void register(final Node node) {
    try (final Jedis jedis = redisConnector.getResource()) {
        final Boolean isIdUsed = jedis.sismember(VERTIGO_NODES, node.getId());
        Assertion.checkState(!isIdUsed, "A node id must be unique : Id '{0}' is already used ", node.getId());
        // ---
        try (final Transaction tx = jedis.multi()) {
            tx.hset(VERTIGO_NODE + node.getId(), "json", gson.toJson(node));
            tx.sadd(VERTIGO_NODES, node.getId());
            tx.exec();
        } catch (final IOException e) {
            throw WrappedException.wrap(e);
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) IOException(java.io.IOException)

Example 52 with Transaction

use of redis.clients.jedis.Transaction in project new-cloud by xie-summer.

the class JedisSentinelPoolTest method returnResourceShouldResetState.

@Test
public void returnResourceShouldResetState() {
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(1);
    config.setBlockWhenExhausted(false);
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000, "foobared", 2);
    Jedis jedis = pool.getResource();
    Jedis jedis2 = null;
    try {
        jedis.set("hello", "jedis");
        Transaction t = jedis.multi();
        t.set("hello", "world");
        jedis.close();
        jedis2 = pool.getResource();
        assertTrue(jedis == jedis2);
        assertEquals("jedis", jedis2.get("hello"));
    } catch (JedisConnectionException e) {
        if (jedis2 != null) {
            jedis2 = null;
        }
    } finally {
        jedis2.close();
        pool.destroy();
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) Transaction(redis.clients.jedis.Transaction) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Example 53 with Transaction

use of redis.clients.jedis.Transaction in project new-cloud by xie-summer.

the class TransactionCommandsTest method watch.

@Test
public void watch() throws UnknownHostException, IOException {
    jedis.watch("mykey", "somekey");
    Transaction t = jedis.multi();
    nj.connect();
    nj.auth("foobared");
    nj.set("mykey", "bar");
    nj.disconnect();
    t.set("mykey", "foo");
    List<Object> resp = t.exec();
    assertEquals(null, resp);
    assertEquals("bar", jedis.get("mykey"));
    // Binary
    jedis.watch(bmykey, "foobar".getBytes());
    t = jedis.multi();
    nj.connect();
    nj.auth("foobared");
    nj.set(bmykey, bbar);
    nj.disconnect();
    t.set(bmykey, bfoo);
    resp = t.exec();
    assertEquals(null, resp);
    assertTrue(Arrays.equals(bbar, jedis.get(bmykey)));
}
Also used : Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 54 with Transaction

use of redis.clients.jedis.Transaction in project new-cloud by xie-summer.

the class TransactionCommandsTest method testResetStateWhenInWatch.

@Test
public void testResetStateWhenInWatch() {
    jedis.watch("mykey", "somekey");
    // state reset : unwatch
    jedis.resetState();
    Transaction t = jedis.multi();
    nj.connect();
    nj.auth("foobared");
    nj.set("mykey", "bar");
    nj.disconnect();
    t.set("mykey", "foo");
    List<Object> resp = t.exec();
    assertNotNull(resp);
    assertEquals(1, resp.size());
    assertEquals("foo", jedis.get("mykey"));
}
Also used : Transaction(redis.clients.jedis.Transaction) Test(org.junit.Test)

Example 55 with Transaction

use of redis.clients.jedis.Transaction in project new-cloud by xie-summer.

the class TransactionCommandsTest method execGetResponse.

@Test
public void execGetResponse() {
    Transaction t = jedis.multi();
    t.set("foo", "bar");
    t.smembers("foo");
    t.get("foo");
    List<Response<?>> lr = t.execGetResponse();
    try {
        lr.get(1).get();
        fail("We expect exception here!");
    } catch (JedisDataException e) {
    // that is fine we should be here
    }
    assertEquals("bar", lr.get(2).get());
}
Also used : Response(redis.clients.jedis.Response) Transaction(redis.clients.jedis.Transaction) JedisDataException(redis.clients.jedis.exceptions.JedisDataException) Test(org.junit.Test)

Aggregations

Transaction (redis.clients.jedis.Transaction)149 Test (org.junit.Test)90 Jedis (redis.clients.jedis.Jedis)57 JedisDataException (redis.clients.jedis.exceptions.JedisDataException)15 Set (java.util.Set)14 ArrayList (java.util.ArrayList)11 IOException (java.io.IOException)8 List (java.util.List)8 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)7 GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)6 Protocol (redis.clients.jedis.Protocol)4 Response (redis.clients.jedis.Response)4 JedisException (redis.clients.jedis.exceptions.JedisException)4 Header (redis.clients.jedis.graph.Header)4 Record (redis.clients.jedis.graph.Record)4 ResultSet (redis.clients.jedis.graph.ResultSet)4 Node (redis.clients.jedis.graph.entities.Node)4 Property (redis.clients.jedis.graph.entities.Property)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3