AMOP function usage code example

The Java SDK supports the on-chain messenger protocol AMOP (Advanced Messages Onchain Protocol), and users can exchange messages with other organizations through the AMOP protocol. From this statement, our application can be used as an instant messaging system, but it runs on chain nodes, similar to Kafka.
This article completes the AMOP message topic creation, publishing, broadcasting, subscribing, and responding function code examples, which can be applied to project work.
1 amop's functional flow chart
2 Features of broadcast messages
It should be noted that the unicast message has a callback function and can receive a response message. Broadcast messages do not have a callback function.
When there is only one chain node, broadcast a message, and the subscriber receives 1 message, the content is as follows:
 
广播的消息:
==>  try to broadcast message:  welcome Mini!

订阅者收到的消息:
==> receive message from client
     ==> endpoint: 192.168.2.231:20200
     ==> seq: 724333b8512940ad9fb80245fccb26b5
     ==> data:  welcome Mini!

When there are 2 nodes, more messages are broadcasted, and both nodes broadcast once, and the message that the seq is empty is also mixed. Therefore, the application should deduplicate the received broadcast messages.

广播的消息:
==>  try to broadcast message:  welcome Mini!

订阅者收到的消息:
==> receive message from client
     ==> endpoint: 192.168.2.231:20201
     ==> seq:
     ==> data:  welcome Mini!
==> receive message from client
     ==> endpoint: 192.168.2.231:20200
     ==> seq:
     ==> data:  welcome Mini!
==> receive message from client
     ==> endpoint: 192.168.2.231:20200
     ==> seq: 765b6a2769e64857b6b741daf40fd70c
     ==> data:  welcome Mini!
==> receive message from client
     ==> endpoint: 192.168.2.231:20201
     ==> seq: 765b6a2769e64857b6b741daf40fd70c
     ==> data:  welcome Mini!
3 sample code
subscriber code
package org.fisco.bcos.amop;

import org.fisco.bcos.node.FiscoBcos;
import org.fisco.bcos.sdk.jni.amop.AmopRequestCallback;
import org.fisco.bcos.sdk.v3.amop.Amop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Subscribe {

    private static final Logger logger = LoggerFactory.getLogger(Subscribe.class);

    public static void main(String[] args)  throws Exception {

        String topic = "HelloWorld";
        System.out.println(" ====== AMOP subscribe, topic: " + topic);

        FiscoBcos.initialize();
        Amop amop = FiscoBcos.bcosSDK.getAmop();
        amop.start();
//  简化写法
//        amop.subscribeTopic(
//                topic,
//                (endpoint, seq, data) -> {
//                    System.out.println(" ==> receive message from client");
//                    System.out.println(" \t==> endpoint: " + endpoint);
//                    System.out.println(" \t==> seq: " + seq);
//                    System.out.println(" \t==> data: " + new String(data));
//
//                    amop.sendResponse(endpoint, seq, data);
//                });


        amop.subscribeTopic(
                topic,
                new AmopRequestCallback() {
                    @Override
                    public void onRequest(String endpoint, String seq, byte[] data) {
                        System.out.println(" ==> receive message from client");
                        System.out.println(" \t==> endpoint: " + endpoint);
                        System.out.println(" \t==> seq: " + seq);
                        System.out.println(" \t==> data: " + new String(data));


                        amop.sendResponse(endpoint, seq, data);
                    }
                });


        while (true) {


            Thread.sleep(5000);
        }
    }
}

 unicast message code

package org.fisco.bcos.amop;


import org.fisco.bcos.node.FiscoBcos;
import org.fisco.bcos.sdk.jni.amop.AmopResponseCallback;
import org.fisco.bcos.sdk.jni.common.Response;
import org.fisco.bcos.sdk.v3.amop.Amop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Publish {
    private static final Logger logger = LoggerFactory.getLogger(Publish.class);

    public static void main(String[] args)
            throws Exception {

        String topic = "HelloWorld";
        String msg = " welcome Tom!";
        System.out.println(" ====== AMOP publish, topic: " + topic + " ,msg: " + msg);


        FiscoBcos.initialize();
        Amop amop = FiscoBcos.bcosSDK.getAmop();
        amop.start();


        //每5秒发送一次消息
        while (true) {
            System.out.println(" ====== AMOP publish send message");
            amop.sendAmopMsg(
                    topic,
                    msg.getBytes(),
                    0,
                    new AmopResponseCallback() {
                        @Override
                        public void onResponse(Response response) {
                            System.out.println(" ==> receive response message from server");
                            if (response.getErrorCode() == 0) {
                                System.out.println(
                                        " \t responseData: " + new String(response.getData()));
                            } else {
                                System.out.println(" \t errorCode: " + response.getErrorCode());
                                System.out.println(
                                        " \t errorMessage: " + response.getErrorMessage());
                            }
                        }
                    });


            Thread.sleep(5000);
        }
    }
}

Broadcast message code:

package org.fisco.bcos.amop;

import org.fisco.bcos.node.FiscoBcos;
import org.fisco.bcos.sdk.v3.amop.Amop;

public class Broadcast {

    public static void main(String[] args)
            throws Exception {

        String topic = "HelloWorld";
        String msg = " welcome Mini!";

        System.out.println(" ====== AMOP broadcast, topic: " + topic + " ,msg: " + msg);

        FiscoBcos.initialize();
        Amop amop = FiscoBcos.bcosSDK.getAmop();
        amop.start();

        while (true) {
            System.out.println(" ==>  try to broadcast message: " + msg);
            amop.broadcastAmopMsg(topic, msg.getBytes());
            Thread.sleep(10000);
        }
    }
}

 

Guess you like

Origin blog.csdn.net/u012084827/article/details/130369955