风之语者

博客园 首页 新随笔 联系 订阅 管理

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

如何使用:

1.添加引用

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.生产消息

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();
View Code

3.消费消息

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();
View Code

4.推送消息的监听器

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;
        }
    }
View Code

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

posted on 2017-08-07 15:49  风之语者  阅读(2259)  评论(1编辑  收藏  举报