RocketMQ Windows下VC客户端之Bridge试验

Windows平台客户端调用,有些麻烦,特别是一般个人用户。反正鹅是找了好久Windows下C#的免费客户端,找到那个OpenJDK编译的dll,还不好用。

于是设想,使用JNI写一个java的MqBridge,java类中有3个方法

public class test.MqBridge {
  org.apache.rocketmq.client.producer.DefaultMQProducer producer;
    descriptor: Lorg/apache/rocketmq/client/producer/DefaultMQProducer;

  public test.MqBridge();
    descriptor: ()V

  //带NAMESRV_ADDR
  public void start(java.lang.String) throws org.apache.rocketmq.client.exception.MQClientException, java.lang.InterruptedException;
    descriptor: (Ljava/lang/String;)V
  
  //使用环境变量的NAMESRV_ADDR
  public void start() throws org.apache.rocketmq.client.exception.MQClientException, java.lang.InterruptedException;
    descriptor: ()V

  public void stop();
    descriptor: ()V
  
  //发送,第1参数是topic,第2个参数是消息内容
  public int send(java.lang.String, java.lang.String);
    descriptor: (Ljava/lang/String;Ljava/lang/String;)I
}

经过使用VC调用测试,发送正常

RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent0).
RocketMQLog:WARN Please initialize the logger system properly.
Call java start()
SendResult [sendStatus=SEND_OK, msgId=7F0000010EA000387F443B6B330C0000, offsetMsgId=C0A8008200002AA70000000000
0B1772, messageQueue=MessageQueue [topic=device_data, brokerName=broker-a, queueId=3], queueOffset=426]
Call java send()
Call java stop()


调用过程代码
    jclass clsBridge = env->FindClass("test/MqBridge");//类
    if(NULL==clsBridge){
        cout<<"Can't find/load the MqBridge class ";printErrMsg(GetLastError());cout<<endl;
        return false;
    }
    jobject objBridge = env->AllocObject(clsBridge);//实例
    if(NULL==objBridge){
        cout<<"Create instance of MqBridge class failed"<<endl;
        return false;
    }
    
    //调用start方法
    char addr[]="192.168.0.130:9876";
    jstring addrJ=env->NewStringUTF(addr);
    jmethodID midStart = env->GetMethodID(clsBridge, "start", "(Ljava/lang/String;)V");
    env->CallVoidMethod(objBridge, midStart, addrJ);
    cout<<"Call java start()"<<endl;
    
    //发送消息,可以反复调用send
    char topic[]="device_data";
    char msg[]="test rockmq";
    jstring topicJ=env->NewStringUTF(topic);
    jstring msgJ  =env->NewStringUTF(msg);
    jmethodID midSend = env->GetMethodID(clsBridge, "send", "(Ljava/lang/String;Ljava/lang/String;)I");
    env->CallObjectMethod(objBridge, midSend, topicJ, msgJ);
    cout<<"Call java send()"<<endl;
    
    //结束,关停
    jmethodID midStop = env->GetMethodID(clsBridge, "stop", "()V");
    env->CallVoidMethod(objBridge, midStop);
    cout<<"Call java stop()"<<endl;
    

测试环境 1(32位编译):
JDK1.8(32位) + VC2008+Windows7 X64

测试环境 2(64位编译):
JDK1.8(64位) + VC2019+Windows7 X64

测试环境 3(32位编译):
JDK1.8(32位) + VC2019+Windows7 X64

 

关键的classpath
classPath=-Djava.class.path=D:/vc_java/bin;C:/jdk1.8.0_311_x86/lib;D:/rocketmq-all-4.8.0-bin-release/lib/rocketmq-client-4.8.0.jar;D:/rocketmq-all-4.8.0-bin-release/lib/rocketmq-common-4.8.0.jar;D:/rocketmq-all-4.8.0-bin-release/lib/rocketmq-remoting-4.8.0.jar;D:/rocketmq-all-4.8.0-bin-release/lib/rocketmq-logging-4.8.0.jar;D:/rocketmq-all-4.8.0-bin-release/lib/netty-all-4.0.42.Final.jar;D:/rocketmq-all-4.8.0-bin-release/lib/commons-lang3-3.4.jar;D:/rocketmq-all-4.8.0-bin-release/lib/fastjson-1.2.69.jar;D:/rocketmq-all-4.8.0-bin-release/lib/commons-validator-1.6.jar


一般的性能要求是可以应付的,对于性能要求不是极致的话,可以这样考虑。也可以编译成.dll,供其它语言使用。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章