RabbitMQ by Example: Multiple Threads, Channels and Queues

后端 未结 2 1120
情书的邮戳
情书的邮戳 2020-12-04 04:50

I just read RabbitMQ\'s Java API docs, and found it very informative and straight-forward. The example for how to set up a simple Channel for publishing/consumi

2条回答
  •  不知归路
    2020-12-04 05:35

    How can I set up 1+ Channels to publish/consume to and from multiple queues?

    You can implement using threads and channels. All you need is a way to categorize things, ie all the queue items from the login, all the queue elements from security_events etc. The catagorization can be achived using a routingKey.

    ie: Every time when you add an item to the queue u specify the routing key. It will be appended as a property element. By this you can get the values from a particular event say logging.

    The following Code sample explain how you make it done in client side.

    Eg:

    The routing key is used identify the type of the channel and retrive the types.

    For example if you need to get all the channels about the type Login then you must specify the routing key as login or some other keyword to identify that.

                Connection connection = factory.newConnection();
                Channel channel = connection.createChannel();
    
                channel.exchangeDeclare(EXCHANGE_NAME, "direct");
    
                string routingKey="login";
    
                channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
    

    You can Look here for more details about the Categorization ..


    Threads Part

    Once the publishing part is over you can run the thread part..

    In this part you can get the Published data on the basis of category. ie; routing Key which in your case is logging, security_events and customer_orders etc.

    look in the Example to know how retrieve the data in threads.

    Eg :

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
    //**The threads part is as follows** 
     channel.exchangeDeclare(EXCHANGE_NAME, "direct");      
     String queueName = channel.queueDeclare().getQueue();
        // This part will biend the queue with the severity (login for eg:)
        for(String severity : argv){
                  channel.queueBind(queueName, EXCHANGE_NAME, routingKey);
        }
        boolean autoAck = false;
        channel.basicConsume(queueName, autoAck, "myConsumerTag",
        new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                    Envelope envelope,
                                    AMQP.BasicProperties properties,
                                    byte[] body)
             throws IOException
         {
                 String routingKey = envelope.getRoutingKey();
                 String contentType = properties.contentType;
                 long deliveryTag = envelope.getDeliveryTag();
    
                 // (process the message components here ...)
                 channel.basicAck(deliveryTag, false);
         }
     });
    

    Now a thread that process the Data in the Queue of the type login(routing key) is created. By this way you can create multiple threads. Each serving different purpose.

    look here for more details about the threads part..

提交回复
热议问题