1 Star 0 Fork 48

Git_OSC/redis-replicator

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
CommandExtensionExample.java 2.92 KB
一键复制 编辑 原始数据 按行查看 历史
chenby 提交于 2019-06-09 21:45 . offset
/*
* Copyright 2016-2018 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.examples.extension;
import static com.moilioncircle.redis.replicator.cmd.CommandParsers.toRune;
import com.moilioncircle.redis.replicator.RedisReplicator;
import com.moilioncircle.redis.replicator.Replicator;
import com.moilioncircle.redis.replicator.cmd.CommandName;
import com.moilioncircle.redis.replicator.cmd.CommandParser;
import com.moilioncircle.redis.replicator.cmd.impl.AbstractCommand;
import com.moilioncircle.redis.replicator.event.Event;
import com.moilioncircle.redis.replicator.event.EventListener;
/**
* @author Leon Chen
* @since 2.1.0
*/
@SuppressWarnings("resource")
public class CommandExtensionExample {
public static void main(String[] args) throws Exception {
final Replicator replicator = new RedisReplicator("redis://127.0.0.1:6379");
replicator.addCommandParser(CommandName.name("APPEND"), new YourAppendParser());
replicator.addEventListener(new EventListener() {
@Override
public void onEvent(Replicator replicator, Event event) {
if (event instanceof YourAppendParser.YourAppendCommand) {
YourAppendParser.YourAppendCommand yourAppendCommand = (YourAppendParser.YourAppendCommand) event;
System.out.println(yourAppendCommand.key);
System.out.println(yourAppendCommand.value);
}
}
});
replicator.open();
}
public static class YourAppendParser implements CommandParser<YourAppendParser.YourAppendCommand> {
@Override
public YourAppendCommand parse(Object[] command) {
return new YourAppendCommand(toRune(command[1]), toRune(command[2]));
}
public static class YourAppendCommand extends AbstractCommand {
/**
*
*/
private static final long serialVersionUID = 1L;
public final String key;
public final String value;
public YourAppendCommand(String key, String value) {
this.key = key;
this.value = value;
}
@Override
public String toString() {
return "YourAppendCommand{" +
"key='" + key + '\'' +
", value='" + value + '\'' +
'}';
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/Git_OSC/redis-replicator.git
git@gitee.com:Git_OSC/redis-replicator.git
Git_OSC
redis-replicator
redis-replicator
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385