@Test
public void producer() throws InterruptedException {
while (true){
String msgKey = "it";
HashMap<String, String> map = new HashMap<>();
map.put("name", UUID.randomUUID().toString());
Random random = new Random();
map.put("age", random.nextInt() + "");
jedis.xadd(msgKey, StreamEntryID.NEW_ENTRY, map);
System.out.println(map.entrySet());
Thread.sleep(1000);
}
}
@Test
public void consumer1() throws InterruptedException {
while (true){
// jedis.xgroupCreate(msgKey, groupName, null, false);
String msgKey = "it";
String groupName = "gp1";
String consumerName = "c1";
int count = 0;
//ack:确保消息至少被消费一次
Map<String,StreamEntryID> t = new HashMap();
t.put(msgKey, null);//null 则为 > 重头读起,也可以为$接受新消息,还可以是上一次未读完的消息id
Map.Entry index = null;
for(Map.Entry c:t.entrySet()){
index=c;
}
List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, count, 0, true, index);
if (msg == null){
System.out.println("consumer1 waiting msg");
Thread.sleep(1000);
}else {
msg.forEach(m -> {
List<StreamEntry> msgList = m.getValue();
msgList.forEach(en -> {
System.out.println("consumer1: " + en.getFields());
});
});
}
}
}
@Test
public void earlistModel() throws InterruptedException {
jedis = new Jedis("127.0.0.1", 6379);
String msgKey = "item_test";
String groupName = "gtest4";
String consumerName = "c1";
try{
jedis.xgroupCreate(msgKey, groupName, new StreamEntryID("0-0"), true);
}catch (Exception e){
System.out.println(e);
}finally {
//触发一次消息记录,使其之后每次都是从0开始消费
HashMap<String, StreamEntryID> map = new HashMap<>();
map.put(msgKey, StreamEntryID.UNRECEIVED_ENTRY);
Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
jedis.xreadGroup(groupName, consumerName, 0, 0, false, idEntry);
}
HashMap<String, StreamEntryID> map = new HashMap<>();
// map.put(msgKey, StreamEntryID.UNRECEIVED_ENTRY);
map.put(msgKey, new StreamEntryID("0-0"));
Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
//ack变成true消费记录会无法保存问题
List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, 0, 0, false, idEntry);
if (msg == null || msg.get(0).getValue().size() == 0){
System.out.println("consumer2 waiting msg");
Thread.sleep(1000);
}else {
System.out.println("-----------------");
print(msg);
}
}
/**
* latest:只读取最新消息
* group 创建topic用 $ 忽略历史消息只读取最新的消息
* read >
* xreadgroup
*/
@Test
public void latestModel() throws InterruptedException {
String msgKey = "news";
String groupName = "g8090";
String consumerName = "c1";
try{
jedis.xgroupCreate(msgKey, groupName, StreamEntryID.LAST_ENTRY, true);
}catch (Exception e){
System.out.println(e);
}
HashMap<String, StreamEntryID> map = new HashMap<>();
map.put(msgKey, null);
Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
while (true){
List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, 0, 0, true, idEntry);
if (msg == null){
System.out.println("consumer2 waiting msg");
Thread.sleep(1000);
}else {
System.out.println("-----------------");
print(msg);
System.out.println(msg.size());
System.out.println("22222222222222222222");
}
}
}
/**
* breakpoint:断点 读取未读取过的消息
* 这种情况必须producer时候就创建group 0-0
* read > :第一次会读取未消费过的数据
*/
@Test
public void breakpointModel() throws InterruptedException {
String msgKey = "news";
String groupName = "g88888";
String consumerName = "c1";
try{
jedis.xgroupCreate(msgKey, groupName, null, true);
}catch (Exception e){
System.out.println(e);
}
HashMap<String, StreamEntryID> map = new HashMap<>();
map.put(msgKey, StreamEntryID.UNRECEIVED_ENTRY);
Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
while (true){
List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, 0, 0, true, idEntry);
if (msg == null){
System.out.println("consumer2 waiting msg");
Thread.sleep(1000);
}else {
System.out.println("-----------------");
print(msg);
System.out.println(msg.size());
System.out.println("22222222222222222222");
}
}
}
/**
* breakpoint:断点 读取未读取过的消息
* 这种情况必须producer时候就创建group 0-0
* read > :第一次会读取未消费过的数据
*/
@Test
public void breakpointModel() throws InterruptedException {
String msgKey = "news";
String groupName = "g88888";
String consumerName = "c1";
try{
jedis.xgroupCreate(msgKey, groupName, null, true);
}catch (Exception e){
System.out.println(e);
}
HashMap<String, StreamEntryID> map = new HashMap<>();
map.put(msgKey, StreamEntryID.UNRECEIVED_ENTRY);
Map.Entry<String, StreamEntryID> idEntry = map.entrySet().iterator().next();
while (true){
List<Map.Entry<String, List<StreamEntry>>> msg = jedis.xreadGroup(groupName, consumerName, 0, 0, true, idEntry);
if (msg == null){
System.out.println("consumer2 waiting msg");
Thread.sleep(1000);
}else {
System.out.println("-----------------");
print(msg);
System.out.println(msg.size());
System.out.println("22222222222222222222");
}
}
}