Redisson redis client Java code

Contents

Redisson redis client Java code

Redisson Transaction :

TransactionHandle tHandle = new TransactionHandle();

RTransaction transaction = redissonClient.createTransaction(TransactionOptions.defaults()); tHandle.transaction = transaction;

try { for (String IDENTITY : IDENTITYList) { logger.info(“entry”); RMapCache<String, List<String>> identityMap = transaction .getMapCache(_INSTANCE_$IDENTITY + IDENTITY.toString());

System.out.println(“keys:” + _INSTANCE_$IDENTITY + IDENTITY.toString() + identityMap.keySet());

for (String key : identityMap.keySet()) { List<String> temp = identityMap.get(key);

System.out.println(“remove: ” + temp.remove(nfId) + temp); identityMap.put(key, temp);

}

}

tHandle.transaction.getBucket(_INSTANCE_TA_$ID+ nfId).delete(); tHandle.future = transaction.commitAsync();

// Put && Get in stream form instead of a map handle

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken
import io.vertx.core.json.JsonObject;
import org.junit.Test;
import org.redisson.Redisson;
import org.redisson.api.*;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
public class RedisClientJUnitTest {
@Test
public void testPutGet() throws InterruptedException, IllegalArgumentException {
Config config = new Config();
config.useClusterServers().addNodeAddress(“redis://redis://ip:port”);
//config.useSingleServer().setAddress(“redis://127.0.0.1:6379”);
RedissonClient redissonClient= Redisson.create(config);
long startTime = System.nanoTime();
for(int i=0 ;i < 100000 ;i++) { Map val = new HashMap<>();
val.put(“1”, “1”);
String rediskey=”MAP“+i;
RBinaryStream obj = redissonClient.getBinaryStream(rediskey);
RFuture upfuture = obj.setAsync(val.toString().getBytes());
upfuture.handle((conn, exception) -> {

if (exception != null) {
System.out.println(“Fail”);
String msg = exception.getMessage();
try {
throw new Exception(
” fastPutAsync failed ” + “exception msg: ” + msg,
exception.getCause()
);
} catch (Exception e) {
}
}
return conn;
});
RFuture future = obj.getAsync();
future.whenComplete((res, exception) -> {
String msg = “”;
JsonObject resp = null;
if(res == null) {
//System.out.println(“key doesnot exist” );
}
else{
//System.out.println(“val: ” + res);
}
});
}
long spentTime = System.nanoTime() – startTime;
System.out.println(“get spentTime: ” + spentTime);
redissonClient.shutdown();
}

//Put && Get using a map handle
@Test
public void testMapPutGet() throws InterruptedException, IllegalArgumentException {
Config config = new Config();
config.useClusterServers().addNodeAddress(“redis://ip:port”)
RedissonClient redissonClient= Redisson.create(config);
long startTime = System.nanoTime();
RLocalCachedMap > mapCache = redissonClient.getLocalCachedMap(“-mapping-test”, StringCodec.INSTANCE, LocalCachedMapOptions.defaults());
for(int i=0 ;i < 100000 ;i++) { Map val = new HashMap<>();
val.put(“1″,”1”);
RFuture createFut = mapCache.fastPutAsync(i,val);
createFut.handle((conn, exception) -> {
if (conn) {
//System.out.println(“Pass”);
} else {
//System.out.println(“Fail”);
}
if (exception != null) {
String msg = exception.getMessage();
try {
throw new Exception(
” fastPutAsync failed ” + “exception msg: ” + msg,
exception.getCause()
);
} catch (Exception e) {
}
}
return conn;
});
RFuture> future = mapCache.getAsync(i);
future.handle((conn, exception) -> {
// async operation success? replacement for future.isSuccess() ??
if (conn !=null) {
//System.out.println(“val: ” + val);
} else {
}
if (exception != null) {
String msg = exception.getMessage();
try {
throw new Exception(
” fastPutAsync failed ” + “exception msg: ” + msg,
exception.getCause()
);
} catch (Exception e) {
}
}
return conn;
});
}
long spentTime = System.nanoTime() – startTime;
System.out.println(“get spentTime: ” + spentTime);
redissonClient.shutdown();
}

// Redisson batch put && get query , Storing Class Object in value
@Test
public void testBatchPut() throws InterruptedException, IllegalArgumentException {

Config config = new Config();
config.useClusterServers().addNodeAddress(“redis://ip:port”);
RedissonClient redissonClient= Redisson.create(config);

long startTime = System.nanoTime();
Aai t1 = new Aai();
t1.setTac(“2”);
redissonClient.getMap(“redispojo”).put(“11”,t1);
RBatch batch = redissonClient.createBatch(BatchOptions.defaults());
batch.getMap(“redispojo”).getAsync(“11”); // returns false if value is updated
batch.getMap(“test5”).getAsync(“12”);
batch.getMap(“test6”).getAsync(“22”);
RFuture future = batch.getAtomicLong(“counter1”).incrementAndGetAsync();
batch.getAtomicLong(“counter1”).incrementAndGetAsync();
// result could be acquired through RFuture object returned by batched method
// or
// through result list by corresponding index
future.whenComplete((res, exception) -> {
System.out.println(“WhenComplete:” + res);
// …
});

// Generics

//Following Batch command will output different datatype values

//so Generics is used.
BatchResult<?> res = batch.execute();
Aai tai = (Aai) res.getResponses().get(0);
System.out.println(“res0″+”:” + tai.getTac());
for(int i=1 ;i<res.getResponses().size()-2;i++) {
System.out.println(“res”+i+”:” + res.getResponses().get(i));
Long counter = (Long) res.getResponses().get(res.getResponses().size()-1);
try {
future.get().equals(counter);
}
catch(ExecutionException e)
{
e.printStackTrace();
}
}
redissonClient.shutdown();
long spentTime = System.nanoTime() – startTime;
System.out.println(“get spentTime: ” + spentTime);
}
}

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at [email protected] along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.