-
Notifications
You must be signed in to change notification settings - Fork 68
/
SimpleIM.java
141 lines (118 loc) · 4.68 KB
/
SimpleIM.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package examples.v1;
import com.alicloud.openservices.tablestore.timeline.ScanParameter;
import com.alicloud.openservices.tablestore.timeline.ScanParameterBuilder;
import com.alicloud.openservices.tablestore.timeline.Timeline;
import com.alicloud.openservices.tablestore.timeline.TimelineEntry;
import com.alicloud.openservices.tablestore.timeline.message.IMessage;
import com.alicloud.openservices.tablestore.timeline.message.StringMessage;
import com.alicloud.openservices.tablestore.timeline.store.DistributeTimelineConfig;
import com.alicloud.openservices.tablestore.timeline.store.DistributeTimelineStore;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* 基于Timeline的简单IM实现。
*/
public class SimpleIM {
private DistributeTimelineStore store = null;
private DistributeTimelineStore sync = null;
public static void main(String[] args) {
SimpleIM im = new SimpleIM();
im.push();
im.getHistoryMessage();
im.getSyncMessage();
im.close();
}
SimpleIM() {
System.out.println("Start create IM");
DistributeTimelineConfig storeConfig = new DistributeTimelineConfig(
"<Your instance endpoint>",
"<Your access key id>",
"<Your access key secret>",
"<Your instance name>",
"Your table name");
storeConfig.setTtl(365 * 24 * 3600); // one Year
storeConfig.setLimit(100);
DistributeTimelineConfig syncConfig = new DistributeTimelineConfig(
"<Your instance endpoint>",
"<Your access key id>",
"<Your access key secret>",
"<Your instance name>",
"Your table name");
syncConfig.setTtl(30 * 24 * 3600); // one Month
syncConfig.setLimit(100);
store = new DistributeTimelineStore(storeConfig);
sync = new DistributeTimelineStore(syncConfig);
store.drop();
sync.drop();
if (!store.exist()) {
store.create();
}
if (!sync.exist()) {
sync.create();
}
System.out.println("End create IM");
}
void push() {
System.out.println("Start Push message to IM");
List<String> groupMembers = Arrays.asList("user_A", "user_B", "user_C");
sendGroupMessage("group_1", new StringMessage("user_B:阿里云的NoSQL数据库是哪个?"), groupMembers);
sendGroupMessage("group_1", new StringMessage("user_C:是表格存储"), groupMembers);
sendGroupMessage("group_1", new StringMessage("user_B:好,谢谢"), groupMembers);
System.out.println("End Push message to IM");
}
/**
* 读取群历史消息内容
*/
void getHistoryMessage() {
System.out.println("Begin Get history message from IM");
Timeline group = new Timeline("group_1", store);
ScanParameter scanParameter = ScanParameterBuilder
.scanBackward()
.from(Long.MAX_VALUE)
.to(0)
.maxCount(100)
.build();
Iterator<TimelineEntry> entries = group.scan(scanParameter);
while (entries.hasNext()) {
System.out.println(new String(entries.next().getMessage().serialize()));
}
System.out.println("End Get history message from IM");
}
/**
* 读取最新的同步消息
*/
void getSyncMessage() {
System.out.println("Begin Get sync message from IM");
Timeline userSync = new Timeline("user_A", sync);
ScanParameter scanParameter = ScanParameterBuilder
.scanForward()
.from(0)
.to(Long.MAX_VALUE)
.maxCount(100)
.build();
Iterator<TimelineEntry> iterator = userSync.scan(scanParameter);
while(iterator.hasNext()) {
TimelineEntry entry = iterator.next();
try {
String content = new String(entry.getMessage().serialize(), "UTF-8");
System.out.println(String.format("sequence_id:%d, message:%s", entry.getSequenceID(), content));
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
System.out.println("Begin End sync message from IM");
}
private void sendGroupMessage(String groupName, IMessage message, List<String> groupMembers) {
Timeline sender = new Timeline(groupName, store);
sender.store(message);
for (String user : groupMembers) {
Timeline receiver = new Timeline(user, sync);
receiver.store(message);
}
}
void close() {
store.close();
sync.close();
}
}