Skip to content

Commit c2c6d0c

Browse files
authored
sync update topic func to 2.2.3 branch (#521) (#526)
* sync update topic func to 2.2.3 branch (#521) * add topics update * add Channel2ServerUpdateTopics.java * sync update topic func to 2.2.3 branch * update keepalive nodes count log to trace level.
1 parent 53377e3 commit c2c6d0c

File tree

6 files changed

+111
-9
lines changed

6 files changed

+111
-9
lines changed

Changelog.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
### v2.2.3
2+
3+
(2020-03-03)
4+
5+
* 新增
6+
1. Service对象添加updateTopicsToNode接口,SDK可以运行时更新订阅的topic
7+
8+
* 兼容
9+
1. 适配fisco-bcos 2.2.0版本,支持Channel Message v1/V2/V3协议
10+
111
### v2.2.2
212

313
(2020-01-17)

release_note.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v2.2.2
1+
v2.2.3

src/main/java/org/fisco/bcos/channel/client/Service.java

+48-2
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,52 @@ public void run(Timeout timeout) throws Exception {
716716
}
717717
}
718718

719+
/**
720+
* When SDK start, the initial subscribed topics information set by user will be sent to the
721+
* node. User can update subscribed topics again by following steps: 1. Set the topics you want
722+
* to subscribe to again Service service // Servcie object Set<String> topics // topics that
723+
* subscribe again service.setTopics(topics) 2. send update topics message to all nodes
724+
* service.updateTopicsToNode();
725+
*/
726+
public void updateTopicsToNode() {
727+
728+
logger.info(" updateTopicToNode, groupId: {}, topics: {}", groupId, getTopics());
729+
730+
// select send node
731+
ChannelConnections channelConnections =
732+
allChannelConnections
733+
.getAllChannelConnections()
734+
.stream()
735+
.filter(x -> x.getGroupId() == groupId)
736+
.findFirst()
737+
.get();
738+
739+
if (Objects.isNull(channelConnections)) {
740+
throw new IllegalArgumentException(
741+
" No group configuration was found, groupId: " + groupId);
742+
}
743+
744+
ConnectionCallback callback = (ConnectionCallback) channelConnections.getCallback();
745+
if (Objects.isNull(callback)) {
746+
throw new IllegalArgumentException(
747+
" No callback was found for ChannelConnections, service is not initialized");
748+
}
749+
callback.setTopics(getTopics());
750+
751+
/** send update topic message to all connected nodes */
752+
Map<String, ChannelHandlerContext> networkConnections =
753+
channelConnections.getNetworkConnections();
754+
for (ChannelHandlerContext ctx : networkConnections.values()) {
755+
if (Objects.nonNull(ctx) && ChannelHandlerContextHelper.isChannelAvailable(ctx)) {
756+
try {
757+
callback.sendUpdateTopicMessage(ctx);
758+
} catch (Exception e) {
759+
logger.debug(" e: ", e);
760+
}
761+
}
762+
}
763+
}
764+
719765
public void asyncMulticastChannelMessageForVerifyTopic(ChannelRequest request) {
720766
String toTopic = request.getToTopic();
721767
request.setToTopic(getNeedVerifyTopics(toTopic));
@@ -1133,7 +1179,7 @@ public void onReceiveChannelMessage2(ChannelHandlerContext ctx, ChannelMessage2
11331179

11341180
push.setSeq(message.getSeq());
11351181
push.setMessageID(message.getSeq());
1136-
logger.info("msg:{}", Arrays.toString(message.getData()));
1182+
logger.debug("msg:{}", Arrays.toString(message.getData()));
11371183
push.setContent(message.getData());
11381184
pushCallback.onPush(push);
11391185
} else {
@@ -1145,7 +1191,7 @@ public void onReceiveChannelMessage2(ChannelHandlerContext ctx, ChannelMessage2
11451191
}
11461192

11471193
} else if (message.getType() == ChannelMessageType.AMOP_RESPONSE.getType()) {
1148-
logger.info("channel message:{}", message.getSeq());
1194+
logger.debug("channel message:{}", message.getSeq());
11491195
if (callback != null) {
11501196
logger.debug("found callback response");
11511197

src/main/java/org/fisco/bcos/channel/handler/ChannelConnections.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ public void reconnect() {
664664
}
665665
}
666666

667-
logger.info(" Keepalive nodes count: {}", aliveConnectionCount);
667+
logger.trace(" Keepalive nodes count: {}", aliveConnectionCount);
668668

669669
for (ConnectionInfo connectionInfo : connectionInfoList) {
670670

src/main/java/org/fisco/bcos/channel/handler/ConnectionCallback.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public void onResponse(BcosResponse response) {
177177
.set(channelProtocol);
178178

179179
//
180-
subBlockNotification(ctx);
180+
sendUpdateTopicMessage(ctx);
181181
queryBlockNumber(ctx);
182182
// channelService.getEventLogFilterManager().sendFilter();
183183

@@ -234,7 +234,7 @@ public void onResponse(BcosResponse response) {
234234
logger.info(
235235
" query node version timeout, content: {}",
236236
response.getContent());
237-
subBlockNotification(ctx);
237+
sendUpdateTopicMessage(ctx);
238238
queryBlockNumber(ctx);
239239
return;
240240
} else if (response.getErrorCode() != 0) {
@@ -282,7 +282,7 @@ public void onResponse(BcosResponse response) {
282282
nodeVersion.getResult(),
283283
response.getContent());
284284

285-
subBlockNotification(ctx);
285+
sendUpdateTopicMessage(ctx);
286286
queryBlockNumber(ctx);
287287
// channelService.getEventLogFilterManager().sendFilter();
288288
}
@@ -316,7 +316,7 @@ public void run(Timeout timeout) throws Exception {
316316
channelService.getSeq2Callback().put(seq, callback);
317317
}
318318

319-
private void subBlockNotification(ChannelHandlerContext ctx) throws JsonProcessingException {
319+
public void sendUpdateTopicMessage(ChannelHandlerContext ctx) throws JsonProcessingException {
320320

321321
Message message = new Message();
322322
message.setResult(0);
@@ -336,7 +336,7 @@ private void subBlockNotification(ChannelHandlerContext ctx) throws JsonProcessi
336336
ctx.writeAndFlush(out);
337337

338338
logger.info(
339-
" send sub block notification request, seq: {}, content: {}",
339+
" send update topic message request, seq: {}, content: {}",
340340
message.getSeq(),
341341
content);
342342
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.fisco.bcos.channel.test.amop;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import org.fisco.bcos.channel.client.Service;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.context.ApplicationContext;
9+
import org.springframework.context.support.ClassPathXmlApplicationContext;
10+
11+
public class Channel2ServerUpdateTopics {
12+
private static final Logger logger = LoggerFactory.getLogger(Channel2Server.class);
13+
14+
public static void main(String[] args) throws Exception {
15+
if (args.length < 1) {
16+
System.out.println("Param: topic");
17+
return;
18+
}
19+
String topic = args[0];
20+
logger.debug("init Server");
21+
ApplicationContext context =
22+
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
23+
Service service = context.getBean(Service.class);
24+
PushCallback cb = new PushCallback();
25+
service.setPushCallback(cb);
26+
System.out.println("3s...");
27+
Thread.sleep(1000);
28+
System.out.println("2s...");
29+
Thread.sleep(1000);
30+
System.out.println("1s...");
31+
Thread.sleep(1000);
32+
33+
System.out.println("start test");
34+
System.out.println("===================================================================");
35+
service.run();
36+
37+
Thread.sleep(10000);
38+
39+
System.out.println("set topics");
40+
System.out.println("===================================================================");
41+
Set<String> topics = new HashSet<String>();
42+
topics.add(topic);
43+
service.setTopics(topics);
44+
service.updateTopicsToNode();
45+
}
46+
}

0 commit comments

Comments
 (0)