天天看点

rocketmq client for c#

基于ikvm的rocketmq的c#客户端,由于阿里对c#不敏感,对这方面的东西缺少。因为工作需要弄了一个,分享给大家 https://github.com/franknew/RocketMQ-Client

如何使用:

1.添加引用

rocketmq client for c#
rocketmq client for c#

IKVM.OpenJDK.Charsets.dll
IKVM.OpenJDK.Core.dll
IKVM.OpenJDK.Jdbc.dll
IKVM.OpenJDK.Management.dll
IKVM.OpenJDK.Security.dll
IKVM.OpenJDK.SwingAWT.dll
IKVM.OpenJDK.Text.dll
IKVM.OpenJDK.Util.dll
IKVM.Runtime.dll
netty-all-4.0.36.Final.dll
rocketmq-client-4.2.0-incubating.dll
rocketmq-common-4.2.0-incubating.dll
rocketmq-remoting-4.2.0-incubating.dll
slf4j-api-1.7.5.dll
fastjson-1.2.12.dll      

View Code

2.生产消息

rocketmq client for c#
rocketmq client for c#
DefaultMQProducer p = new DefaultMQProducer("test");
p.setNamesrvAddr("192.168.100.3:9876");
p.start();
var data = Encoding.UTF8.GetBytes(txbMessage.Text);
com.alibaba.rocketmq.common.message.Message m = new com.alibaba.rocketmq.common.message.Message("defaulttopic1", data);
p.send(m);
p.shutdown();      

3.消费消息

rocketmq client for c#
rocketmq client for c#
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("test");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
consumer.setNamesrvAddr("192.168.100.3:9876");
consumer.subscribe("defaulttopic", "*");
consumer.registerMessageListener(new TestListener());
consumer.start();      

4.推送消息的监听器

rocketmq client for c#
rocketmq client for c#
public class TestListener : MessageListenerConcurrently
    {
        public ConsumeConcurrentlyStatus consumeMessage(List list, ConsumeConcurrentlyContext ccc)
        {
            for (int i = 0; i < list.size(); i++)
            {
                var msg = list.get(i) as Message;
                byte[] body = msg.getBody();
                var str = Encoding.UTF8.GetString(body);
                if (body.Length == 2 && body[0] == 0 && body[1] == 0)
                {
                    
                    //System.out.println("Got the end signal");
                    continue;
                }

            }
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        }
    }      

 建议安装JDK1.7及以上版本