AMOP Practice (General Topics)

Ordinary topics do not need to be created additionally. The message publishing directly sets the topic for this message publishing according to the setTopic method of AmopMsgOut.

This example is a test of using Java SDK, and uses code to complete the creation and subscription of Topic.

1. Subscribe to topics

1.1 Create a subscription test class SubScribeTest

1.2 Load toml configuration file

Note: This example does not implement topic creation and subscription by configuring AMOP items in the *.toml configuration file, so you can directly use the source code reference configuration file config-example.toml

String configFile = SubScribe.class.getClassLoader().getResource("config-subScribe.toml").getPath();

1.3 Initialize BcosSDK and obtain an instance of Amop

BcosSDK sdk =  BcosSDK.build(configFile);
Amop amop = sdk.getAmop();

1.4 Rewrite the callback function after receiving the message

AmopCallback cb = new AmopCallback() {
      @Override
      public byte[] receiveAmopMsg(AmopMsgIn msg) {
      // 收到消息后的处理逻辑。
      }
};

1.5 Initiate subscription

amop.subscribeTopic("testTopic", cb);

2. Send message

2.1 Create a publishing test class PublishTest

2.2 Load toml configuration file

String configFile = Publish.class.getClassLoader().getResource("config-publish.toml").getPath();

2.3  Initialize BcosSDK and obtain an instance of Amop

ConfigOption configOption = Config.load(configFile);
BcosSDK sdk = new BcosSDK(configOption);
Amop amop = sdk.getAmop();

2.4 Configure AmopMsgOut

AmopMsgOut out = new AmopMsgOut();
out.setTopic("testTopic");
out.setType(TopicType.NORMAL_TOPIC);
out.setContent("吃饭了吗".getBytes());
out.setTimeout(6000);

2.5 Set the callback function after the subscriber responds

AmopResponseCallback cb = new AmopResponseCallback() {
      @Override
      public void onResponse(AmopResponse amopResponse) {
             //响应后的逻辑处理代码编写
      }
};

2.6 Send messages to subscribers of the specified topic (unicast)

amop.sendAmopMsg(out, cb);

2.7 Send messages (broadcast) to subscribers of the specified topic

amop.broadcastAmopMsg(out);

Guess you like

Origin blog.csdn.net/m0_47233175/article/details/123832139