package cn.cerc.mis.sync;

import cn.cerc.core.ISession;
import cn.cerc.core.Record;
import cn.cerc.db.redis.JedisFactory;
import cn.cerc.mis.core.SystemBuffer;
import cn.cerc.mis.core.SystemBuffer.SyncServer;
import cn.cerc.mis.other.MemoryBuffer;
import redis.clients.jedis.Jedis;

public class SyncServerRedis implements ISyncServer {
    private SyncServer pushQueue;
    private SyncServer popQueue;

    @Override
    public void push(ISession session, Record record) {
        if (pushQueue == null)
            throw new RuntimeException("pushQueue is null");
        String buffKey = MemoryBuffer.buildKey(pushQueue);
        String configKey = "sync." + pushQueue.name().toLowerCase();
        try (Jedis jedis = JedisFactory.getJedis(configKey)) {
            // 从左边推入
            jedis.lpush(buffKey, record.toString());
        }
    }

    @Override
    public void repush(ISession session, Record record) {
        if (pushQueue == null)
            throw new RuntimeException("pushQueue is null");
        String buffKey = MemoryBuffer.buildKey(pushQueue);
        String configKey = "sync." + pushQueue.name().toLowerCase();
        try (Jedis jedis = JedisFactory.getJedis(configKey)) {
            // 从右边推入
            jedis.rpush(buffKey, record.toString());
        }
    }

    @Override
    public int pop(ISession session, IPopProcesser popProcesser, int maxRecords) {
        if (popQueue == null)
            throw new RuntimeException("popQueue is null");
        String buffKey = MemoryBuffer.buildKey(popQueue);
        String configKey = "sync." + popQueue.name().toLowerCase();
        try (Jedis jedis = JedisFactory.getJedis(configKey)) {
            for (int i = 0; i < maxRecords; i++) {
                // 从右边取出
                String data = jedis.rpop(buffKey);
                if (data == null) {
                    return i;
                }

                Record record = new Record();
                record.setJSON(data);
                popProcesser.popRecord(session, record);
            }
        }
        return maxRecords;
    }

    public SyncServer getPushQueue() {
        return pushQueue;
    }

    public void setPushQueue(SyncServer pushQueue) {
        this.pushQueue = pushQueue;
    }

    public SyncServer getPopQueue() {
        return popQueue;
    }

    public void setPopQueue(SyncServer popQueue) {
        this.popQueue = popQueue;
    }

}

@@ -20,16 +20,107 @@
 .sync;%0A%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Aimport cn.cerc.core.ClassConfig;%0A
 import c
@@ -141,16 +141,16 @@
 ession;%0A
-
 import c
@@ -422,16 +422,103 @@
 erver %7B%0A
+    private static final Logger log = LoggerFactory.getLogger(SyncServerRedis.class);%0A%0A
     priv
@@ -1098,35 +1098,34 @@
  %7B%0A        if (p
-ush
+op
 Queue == null)%0A 
@@ -1156,35 +1156,34 @@
 timeException(%22p
-ush
+op
 Queue is null%22);
@@ -1223,35 +1223,34 @@
 uffer.buildKey(p
-ush
+op
 Queue);%0A        
@@ -1271,35 +1271,34 @@
 ey = %22sync.%22 + p
-ush
+op
 Queue.name().toL
@@ -2135,16 +2135,42 @@
 (data);%0A
+                try %7B%0A    
         
@@ -2218,16 +2218,165 @@
 ecord);%0A
+                %7D catch (Exception e) %7B%0A                    log.error(record.toString());%0A                    e.printStackTrace();%0A                %7D%0A
         

Update SyncServerRedis.java