Code Monkey home page Code Monkey logo

easyrocketmq's Introduction

对阿里云.net 客户端ons的进一步封装,ons是基于cpp dll的PInvoke调用封装,使用起来非常不方便,因此对ons进一步封装,方程序调用便. 示例代码:

  1. 生产消息

    private static ProducerClient producerClient = new ProducerClient(AccessKeyId, AccessKeySecret, ProducerId);
    
    private static void Main(string[] args)
    {
        producerClient.Start();
    
        var stopWatch = new Stopwatch();
        stopWatch.Start();
    
        var taskList = new List<Task>();
        for (int threadIndex = 1; threadIndex <= ProducerThreadCount; threadIndex++)
        {
            // 生产消费
            var task = Task.Factory.StartNew(() => {
                for (int messageIndex = 1; messageIndex <= MessageCountPerThread; messageIndex++)
                {
                    string content = "线程ID=" + Thread.CurrentThread.ManagedThreadId + ", 我要测试rocketmq message";
                    //producerClient.SendMessage(ShardingKey, Topic, content, Tag);
                    producerClient.SendMessage(Topic, content, Tag);
    
                    Console.WriteLine(content);
                }
            }, TaskCreationOptions.LongRunning);
    
            taskList.Add(task);
        }
    
        Task.WaitAll(taskList.ToArray());
        stopWatch.Stop();
    
        // 一定要关闭,不然会有内存泄漏
        producerClient.Shutdown();
    
        Console.WriteLine("发送消息:{0}条, 使用时间{1}毫秒", MessageCountPerThread * ProducerThreadCount, stopWatch.ElapsedMilliseconds);
        Console.ReadLine();
    }
    
  2. 消费消息

    private static PushConsumerClient consumerClient = new PushConsumerClient(AccessKeyId, AccessKeySecret, Topic, ConsumerId, SubExpression);
    
    private static int count = 0;
    
    private class MyMsgListener : DefaultMessageListener
    {
        public override ons.Action consume(Message message, ConsumeContext context)
        {
            Console.WriteLine("消息序号: {0}, 当前线程ID = {1}, 内容为: {2}", ++count, Thread.CurrentThread.ManagedThreadId, message.getBody());
            return ons.Action.CommitMessage;
        }
    }
    
    private static void Main(string[] args)
    {
        var listener = new MyMsgListener();
        consumerClient.setMessageListener(listener);
        consumerClient.Start();
    
        Console.ReadLine();
        consumerClient.Shutdown();
    }
    

easyrocketmq's People

Contributors

antaintan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.