-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
193 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/moilioncircle/redis/replicator/EventHandlerWorker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2016 leon chen | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.moilioncircle.redis.replicator; | ||
|
||
import com.moilioncircle.redis.replicator.cmd.Command; | ||
import com.moilioncircle.redis.replicator.rdb.datatype.KeyValuePair; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
/** | ||
* Created by leon on 8/25/16. | ||
*/ | ||
class EventHandlerWorker extends Thread implements Closeable { | ||
private static final Log logger = LogFactory.getLog(EventHandlerWorker.class); | ||
|
||
private final AbstractReplicator replicator; | ||
private AtomicBoolean isClosed = new AtomicBoolean(false); | ||
|
||
public EventHandlerWorker(AbstractReplicator replicator) { | ||
this.replicator = replicator; | ||
setDaemon(true); | ||
setName("event-handler-worker"); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
while (!isClosed.get()) { | ||
try { | ||
Object object = replicator.eventQueue.take(); | ||
if (object instanceof KeyValuePair<?>) { | ||
KeyValuePair<?> kv = (KeyValuePair<?>) object; | ||
if (!replicator.doRdbFilter(kv)) continue; | ||
replicator.doRdbHandler(kv); | ||
} else if (object instanceof Command) { | ||
Command command = (Command) object; | ||
if (!replicator.doCommandFilter(command)) continue; | ||
replicator.doCommandHandler(command); | ||
} else { | ||
throw new AssertionError(object); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} catch (Throwable e) { | ||
exceptionHandler(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
isClosed.compareAndSet(false, true); | ||
} | ||
|
||
public boolean isClosed() { | ||
return isClosed.get(); | ||
} | ||
|
||
protected void exceptionHandler(Throwable e) { | ||
logger.error(e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.