@Test public void testDiscoverNodesAutomatically(){
Set<HostAndPort> jedisClusterNode=new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1",7379));
JedisCluster jc=new JedisCluster(jedisClusterNode);
assertEquals(3,jc.getClusterNodes().size());
}
@Test public void testCalculateConnectionPerSlot(){
Set<HostAndPort> jedisClusterNode=new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1",7379));
JedisCluster jc=new JedisCluster(jedisClusterNode);
jc.set("foo","bar");
jc.set("test","test");
assertEquals("bar",node3.get("foo"));
assertEquals("test",node2.get("test"));
}
@Test public void testRecalculateSlotsWhenMoved() throws InterruptedException {
Set<HostAndPort> jedisClusterNode=new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1",7379));
JedisCluster jc=new JedisCluster(jedisClusterNode);
int slot51=JedisClusterCRC16.getSlot("51");
node2.clusterDelSlots(slot51);
node3.clusterDelSlots(slot51);
node3.clusterAddSlots(slot51);
JedisClusterTestUtil.waitForClusterReady(node1,node2,node3);
jc.set("51","foo");
assertEquals("foo",jc.get("51"));
}
@Test public void testAskResponse() throws InterruptedException {
Set<HostAndPort> jedisClusterNode=new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1",7379));
JedisCluster jc=new JedisCluster(jedisClusterNode);
int slot51=JedisClusterCRC16.getSlot("51");
node3.clusterSetSlotImporting(slot51,JedisClusterTestUtil.getNodeId(node2.clusterNodes()));
node2.clusterSetSlotMigrating(slot51,JedisClusterTestUtil.getNodeId(node3.clusterNodes()));
jc.set("51","foo");
assertEquals("foo",jc.get("51"));
}
@Test(expected=JedisClusterException.class) public void testThrowExceptionWithoutKey(){
Set<HostAndPort> jedisClusterNode=new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1",7379));
JedisCluster jc=new JedisCluster(jedisClusterNode);
jc.ping();
}
@Test(expected=JedisClusterMaxRedirectionsException.class) public void testRedisClusterMaxRedirections(){
Set<HostAndPort> jedisClusterNode=new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("127.0.0.1",7379));
JedisCluster jc=new JedisCluster(jedisClusterNode);
int slot51=JedisClusterCRC16.getSlot("51");
node2.clusterSetSlotMigrating(slot51,JedisClusterTestUtil.getNodeId(node3.clusterNodes()));
jc.set("51","foo");
}
@Test public void testClusterCountKeysInSlot(){
Set<HostAndPort> jedisClusterNode=new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(),nodeInfo1.getPort()));
JedisCluster jc=new JedisCluster(jedisClusterNode);
for (int index=0; index < 5; index++) {
jc.set("foo{bar}" + index,"hello");
}
int slot=JedisClusterCRC16.getSlot("foo{bar}");
assertEquals(5,node1.clusterCountKeysInSlot(slot).intValue());
}
private void setSuperConnectionHandler(JedisClusterConnectionHandler handler){
try {
Field connectionHandlerField=JedisCluster.class.getDeclaredField("connectionHandler");
connectionHandlerField.setAccessible(true);
connectionHandlerField.set(this,handler);
}
catch ( Exception e) {
e.printStackTrace();
}
}
/**
* Store a job in Redis
* @param jobDetail the {@link JobDetail} object to be stored
* @param replaceExisting if true, any existing job with the same group and name as the given job will be overwritten
* @param jedis a thread-safe Redis connection
* @throws ObjectAlreadyExistsException
*/
@Override @SuppressWarnings("unchecked") public void storeJob(JobDetail jobDetail,boolean replaceExisting,JedisCluster jedis) throws ObjectAlreadyExistsException {
final String jobHashKey=redisSchema.jobHashKey(jobDetail.getKey());
final String jobDataMapHashKey=redisSchema.jobDataMapHashKey(jobDetail.getKey());
final String jobGroupSetKey=redisSchema.jobGroupSetKey(jobDetail.getKey());
if (!replaceExisting && jedis.exists(jobHashKey)) {
throw new ObjectAlreadyExistsException(jobDetail);
}
jedis.hmset(jobHashKey,(Map<String,String>)mapper.convertValue(jobDetail,new TypeReference<HashMap<String,String>>(){
}
));
if (jobDetail.getJobDataMap() != null && !jobDetail.getJobDataMap().isEmpty()) {
jedis.hmset(jobDataMapHashKey,getStringDataMap(jobDetail.getJobDataMap()));
}
jedis.sadd(redisSchema.jobsSet(),jobHashKey);
jedis.sadd(redisSchema.jobGroupsSet(),jobGroupSetKey);
jedis.sadd(jobGroupSetKey,jobHashKey);
}
/**
* Remove the given job from Redis
* @param jobKey the job to be removed
* @param jedis a thread-safe Redis connection
* @return true if the job was removed; false if it did not exist
*/
@Override public boolean removeJob(JobKey jobKey,JedisCluster jedis) throws JobPersistenceException {
final String jobHashKey=redisSchema.jobHashKey(jobKey);
final String jobDataMapHashKey=redisSchema.jobDataMapHashKey(jobKey);
final String jobGroupSetKey=redisSchema.jobGroupSetKey(jobKey);
final String jobTriggerSetKey=redisSchema.jobTriggersSetKey(jobKey);
Long delJobHashKeyResponse=jedis.del(jobHashKey);
jedis.del(jobDataMapHashKey);
jedis.srem(redisSchema.jobsSet(),jobHashKey);
jedis.srem(jobGroupSetKey,jobHashKey);
Set<String> jobTriggerSetResponse=jedis.smembers(jobTriggerSetKey);
jedis.del(jobTriggerSetKey);
Long jobGroupSetSizeResponse=jedis.scard(jobGroupSetKey);
if (jobGroupSetSizeResponse == 0) {
jedis.srem(redisSchema.jobGroupsSet(),jobGroupSetKey);
}
for ( String triggerHashKey : jobTriggerSetResponse) {
final TriggerKey triggerKey=redisSchema.triggerKey(triggerHashKey);
final String triggerGroupSetKey=redisSchema.triggerGroupSetKey(triggerKey);
unsetTriggerState(triggerHashKey,jedis);
jedis.srem(redisSchema.triggersSet(),triggerHashKey);
jedis.srem(redisSchema.triggerGroupsSet(),triggerGroupSetKey);
jedis.srem(triggerGroupSetKey,triggerHashKey);
jedis.del(triggerHashKey);
}
return delJobHashKeyResponse == 1;
}
/**
* Remove (delete) the <code> {@link Trigger}</code> with the given key.
* @param triggerKey the key of the trigger to be removed
* @param removeNonDurableJob if true, the job associated with the given trigger will be removed if it is non-durableand has no other triggers
* @param jedis a thread-safe Redis connection
* @return true if the trigger was found and removed
*/
@Override protected boolean removeTrigger(TriggerKey triggerKey,boolean removeNonDurableJob,JedisCluster jedis) throws JobPersistenceException, ClassNotFoundException {
final String triggerHashKey=redisSchema.triggerHashKey(triggerKey);
final String triggerGroupSetKey=redisSchema.triggerGroupSetKey(triggerKey);
if (!jedis.exists(triggerHashKey)) {
return false;
}
OperableTrigger trigger=retrieveTrigger(triggerKey,jedis);
final String jobHashKey=redisSchema.jobHashKey(trigger.getJobKey());
final String jobTriggerSetKey=redisSchema.jobTriggersSetKey(trigger.getJobKey());
jedis.srem(redisSchema.triggersSet(),triggerHashKey);
jedis.srem(triggerGroupSetKey,triggerHashKey);
jedis.srem(jobTriggerSetKey,triggerHashKey);
if (jedis.scard(triggerGroupSetKey) == 0) {
jedis.srem(redisSchema.triggerGroupsSet(),triggerGroupSetKey);
}
if (removeNonDurableJob) {
Long jobTriggerSetKeySizeResponse=jedis.scard(jobTriggerSetKey);
Boolean jobExistsResponse=jedis.exists(jobHashKey);
if (jobTriggerSetKeySizeResponse == 0 && jobExistsResponse) {
JobDetail job=retrieveJob(trigger.getJobKey(),jedis);
if (!job.isDurable()) {
removeJob(job.getKey(),jedis);
signaler.notifySchedulerListenersJobDeleted(job.getKey());
}
}
}
if (isNullOrEmpty(trigger.getCalendarName())) {
jedis.srem(redisSchema.calendarTriggersSetKey(trigger.getCalendarName()),triggerHashKey);
}
unsetTriggerState(triggerHashKey,jedis);
jedis.del(triggerHashKey);
return true;
}
/**
* Unsets the state of the given trigger key by removing the trigger from all trigger state sets.
* @param triggerHashKey the redis key of the desired trigger hash
* @param jedis a thread-safe Redis connection
* @return true if the trigger was removed, false if the trigger was stateless
* @throws JobPersistenceException if the unset operation failed
*/
@Override public boolean unsetTriggerState(String triggerHashKey,JedisCluster jedis) throws JobPersistenceException {
boolean removed=false;
List<Long> responses=new ArrayList<>(RedisTriggerState.values().length);
for ( RedisTriggerState state : RedisTriggerState.values()) {
responses.add(jedis.zrem(redisSchema.triggerStateKey(state),triggerHashKey));
}
for ( Long response : responses) {
removed=response == 1;
if (removed) {
jedis.del(redisSchema.triggerLockKey(redisSchema.triggerKey(triggerHashKey)));
break;
}
}
return removed;
}
/**
* Store a {@link Calendar}
* @param name the name of the calendar
* @param calendar the calendar object to be stored
* @param replaceExisting if true, any existing calendar with the same name will be overwritten
* @param updateTriggers if true, any existing triggers associated with the calendar will be updated
* @param jedis a thread-safe Redis connection
* @throws JobPersistenceException
*/
@Override public void storeCalendar(String name,Calendar calendar,boolean replaceExisting,boolean updateTriggers,JedisCluster jedis) throws JobPersistenceException {
final String calendarHashKey=redisSchema.calendarHashKey(name);
if (!replaceExisting && jedis.exists(calendarHashKey)) {
throw new ObjectAlreadyExistsException(String.format("Calendar with key %s already exists.",calendarHashKey));
}
Map<String,String> calendarMap=new HashMap<>();
calendarMap.put(CALENDAR_CLASS,calendar.getClass().getName());
try {
calendarMap.put(CALENDAR_JSON,mapper.writeValueAsString(calendar));
}
catch ( JsonProcessingException e) {
throw new JobPersistenceException("Unable to serialize calendar.",e);
}
jedis.hmset(calendarHashKey,calendarMap);
jedis.sadd(redisSchema.calendarsSet(),calendarHashKey);
if (updateTriggers) {
final String calendarTriggersSetKey=redisSchema.calendarTriggersSetKey(name);
Set<String> triggerHashKeys=jedis.smembers(calendarTriggersSetKey);
for ( String triggerHashKey : triggerHashKeys) {
OperableTrigger trigger=retrieveTrigger(redisSchema.triggerKey(triggerHashKey),jedis);
long removed=jedis.zrem(redisSchema.triggerStateKey(RedisTriggerState.WAITING),triggerHashKey);
trigger.updateWithNewCalendar(calendar,misfireThreshold);
if (removed == 1) {
setTriggerState(RedisTriggerState.WAITING,(double)trigger.getNextFireTime().getTime(),triggerHashKey,jedis);
}
}
}
}
/**
* Remove (delete) the <code> {@link Calendar}</code> with the given name.
* @param calendarName the name of the calendar to be removed
* @param jedis a thread-safe Redis connection
* @return true if a calendar with the given name was found and removed
*/
@Override public boolean removeCalendar(String calendarName,JedisCluster jedis) throws JobPersistenceException {
final String calendarTriggersSetKey=redisSchema.calendarTriggersSetKey(calendarName);
if (jedis.scard(calendarTriggersSetKey) > 0) {
throw new JobPersistenceException(String.format("There are triggers pointing to calendar %s, so it cannot be removed.",calendarName));
}
final String calendarHashKey=redisSchema.calendarHashKey(calendarName);
Long deleteResponse=jedis.del(calendarHashKey);
jedis.srem(redisSchema.calendarsSet(),calendarHashKey);
return deleteResponse == 1;
}
/**
* Get the keys of all of the <code> {@link Job}</code> s that have the given group name.
* @param matcher the matcher with which to compare group names
* @param jedis a thread-safe Redis connection
* @return the set of all JobKeys which have the given group name
*/
@Override public Set<JobKey> getJobKeys(GroupMatcher<JobKey> matcher,JedisCluster jedis){
Set<JobKey> jobKeys=new HashSet<>();
if (matcher.getCompareWithOperator() == StringMatcher.StringOperatorName.EQUALS) {
final String jobGroupSetKey=redisSchema.jobGroupSetKey(new JobKey("",matcher.getCompareToValue()));
final Set<String> jobs=jedis.smembers(jobGroupSetKey);
if (jobs != null) {
for ( final String job : jobs) {
jobKeys.add(redisSchema.jobKey(job));
}
}
}
else {
List<Set<String>> jobGroups=new ArrayList<>();
for ( final String jobGroupSetKey : jedis.smembers(redisSchema.jobGroupsSet())) {
if (matcher.getCompareWithOperator().evaluate(redisSchema.jobGroup(jobGroupSetKey),matcher.getCompareToValue())) {
jobGroups.add(jedis.smembers(jobGroupSetKey));
}
}
for ( Set<String> jobGroup : jobGroups) {
if (jobGroup != null) {
for ( final String job : jobGroup) {
jobKeys.add(redisSchema.jobKey(job));
}
}
}
}
return jobKeys;
}

jedis例子的更多相关文章

  1. Jedis 例子(demo)大全

    第一步:到git下载jedis源码,如果你用maven或者gradle,那么直接下官方的即可,地址:https://github.com/xetorthio/jedis:如果你用ant,下载这个:ht ...

  2. Java中使用Jedis操作Redis(转载)

    整理 1.字符串 添加:set keyname value 查询:get keyname 拼接:append keyname value 删除:del keyname 添加多个: mset keyna ...

  3. Redis客户端开发包:Jedis学习-高级应用

    事务 Jedis中事务的写法是将redis操作写在事物代码块中,如下所示,multi与exec之间为具体的事务. jedis.watch (key1, key2, ...); Transaction ...

  4. Redis客户端开发包:Jedis学习-入门

    添加Jedis依赖 我们可以使用以下三种方式来添加Jedis依赖. 1.下载jar文件 从http://search.maven.org/下载最近的jedis包和Apache Commons Pool ...

  5. redis linux 安装及jedis连接测试

    一.安装配置 1:下载redis下载地址 http://code.google.com/p/redis/downloads/list推荐下载redis-1.2.6.tar.gz,之前这个版本同事已经有 ...

  6. Redis客户端之Spring整合Jedis,ShardedJedisPool集群配置

    Jedis设计 Jedis作为推荐的java语言redis客户端,其抽象封装为三部分: 对象池设计:Pool,JedisPool,GenericObjectPool,BasePoolableObjec ...

  7. redis客户端--jedis

    一.jedis jedis 是 redis推荐的java客户端.通过Jedis我们可以很方便地使用java代码的方式,对redis进行操作.jedis使用起来比较简单,它的操作方法与redis命令相类 ...

  8. Jedis下的ShardedJedis(分布式)使用方法(二)

    上一篇中介绍了ShardedJedis的基本使用方法以及演示了一个简单的例子,在这一篇中我们来介绍了ShardedJedis的原理. 1.ShardedJedis内部实现 首先我们来看一下Sharde ...

  9. jedis操作redis全指南

    package com.wujintao.redis; import java.util.Date; import java.util.HashMap; import java.util.Iterat ...

随机推荐

  1. BZOJ1036[ZJOI2008]树的统计Count 题解

    题目大意: 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.有一些操作:1.把结点u的权值改为t:2.询问从点u到点v的路径上的节点的最大权值 3.询问从点u到点v的路径上的节点的权值和 ...

  2. 51Nod 1079 中国剩余定理 Label:数论

    一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K.例如,K % 2 = 1, K % 3 = 2, K % 5 = 3.符合条件的最小的K = 23.   Input 第1行:1个数 ...

  3. Python拾忆--多线程的socket服务器

    阳光明媚的午后,想想最近要开始从写Java到写Python了,就随手打开电脑来体验一下Python与Java之间的不同吧~ 记得我还在上大二的时候,那个时候才开始学Java,最感兴趣的就是Java书最 ...

  4. UIButton在Disabled状态下标题混乱的问题

    最近开发中遇到的问题汇总 有段时间没有归纳开发中遇到的一些问题了,今天就写一下之前开发中遇到的几个问题.希望这 篇文章能让读者在以后的开发中少走弯路.本文将依次介绍<UIButton在Disab ...

  5. php 小试 mysql-zmq-plugin 和 pthreads

    原文: http://my.oschina.net/neochen/blog/294354 https://github.com/netkiller/mysql-zmq-plugin 有2张表: 表1 ...

  6. GO语言练习:第一个Go语言工程--排序

    1.代码 2.编译 3.运行 1.代码框架 /home/fengbo/sorter $ tree . ├── bin ├── pkg ├── readme.txt └── src ├── algori ...

  7. ArcEngine:空间索引格网大小无效

    参考如下帖子:http://www.cnblogs.com/linhugh/archive/2012/07/24/2606439.html\ C# 代码如下 IFeatureClass pNewFtC ...

  8. SQL server 子查询、设置主键外键、变量及变量查询

    一.子查询 子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这 ...

  9. Cocos2d-x 开发 v3.2 建立新项目并添加库文件

    一.添加其它类库     3.0以上的设计耦合性强,项目中模块常以库的形式存在,需常添加链接库.在3.0中经常用到CocoStudio 编辑器的资源数据,所以需要添加CocoStudio 库. 1.1 ...

  10. 织梦DEDECMS网站后台安全检测提示 加一个开关

    1.进入后台后,点击 系统->系统基本参数->添加变量: 变量名称:cfg_safecheck_switch 变量值:N 变量类型:布尔(Y/N) 参数说明:启用安全监测系统: 2.找到系 ...