3

RabbitMQ を使用するプロジェクトがあります。最良の場合、1 秒あたり 3000 メッセージを受信できます。これが私の消費者コードです:

package com.mdnaRabbit.worker;

import java.io.IOException;
import java.math.RoundingMode;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.mdnaRabbit.worker.data.Data;
import org.apache.commons.lang.SerializationUtils;

public class App {

    private static final String TASK_QUEUE_NAME = "task_queue";
    private static int i = 0;
    private static long timeStart;
    private static long timeFinish;
    private static long messPerSec;
    public static void main (String[] argv) throws IOException,InterruptedException{

        ExecutorService threader = Executors.newFixedThreadPool(20);
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("localhost");

        Connection connection = factory.newConnection(threader);
        final Channel channel = connection.createChannel();

        channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        channel.basicQos(50);

        final QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(TASK_QUEUE_NAME, false, consumer);

        timeStart = System.currentTimeMillis();

        try {

            while (i<100000) {

                try {QueueingConsumer.Delivery delivery = consumer.nextDelivery();
                    Data mess = Data.fromBytes(delivery.getBody());

                    System.out.println(" [" + (i++) +"] Received " + mess.getHeader());

                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                }catch (Exception e){
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }

        timeFinish = System.currentTimeMillis();
        messPerSec = Math.round ((i*1000)/(timeFinish - timeStart));

        System.out.println( "receives " + messPerSec + " per second");

        channel.close();
        connection.close();
    }
}

ご覧のとおり、ExecutorService を使用して速度と channel.basicQos() を上げていますが、あまり役に立ちません。受信/送信速度を上げる方法はありますか(送信速度の増加は受信速度と同じだと思います)

4

1 に答える 1