RabbitMQ性能问题

时间:2013-07-03 13:14:43

标签: java performance rabbitmq

我有一个应用程序,它使用RabbitMQ接收来自某个制作人的消息,解析它们并发送som到其他地方。问题是我的应用程序没有完全使用CPU电源。它使用不超过CPU的25%。从profiler看这个截图: enter image description here

以下是执行处理的最大部分的代码:

public class Consumer {

    private static final String QUEUE_NAME = "MdnaMessagesQueue";
    private static int i = 1;
    private final ConnectionFactory factory;
    private final boolean autoAck = false;
    private final Connection connection;
    private final Channel channel;
    private String response;
    private Sender sender;
    private Logger LOG = Logger.getLogger(Consumer.class);
    private ExecutorService es;


    public Consumer() throws IOException{
        es = Executors.newFixedThreadPool(8);
        factory = new ConnectionFactory();
        factory.setHost("localhost");
        connection = factory.newConnection(es);
        channel = connection.createChannel();
        sender = new Sender();
        channel.queueDeclare(QUEUE_NAME, true, false, false, null);
        channel.basicQos(25);
        Properties props = new Properties();
        try {
            props.load(new FileInputStream("/home/mikhail/bzrrep/DLP/DLPServer/src/main/java/log4j.properties"));
        } catch (Exception e){
            LOG.error(e);
        }
        PropertyConfigurator.configure(props);
    }

    /**
     * is used to receive messages from the queue
     * @param customerId the id of current customer
     * @throws IOException
     * @throws InterruptedException
     */
    public void receive(final String customerId) throws IOException,InterruptedException{




        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");



        final QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, autoAck, "myConsumerTag",
                new DefaultConsumer(channel){
                    @Override
                    public void handleDelivery(String consumerTag,
                                               Envelope envelope,
                                               AMQP.BasicProperties properties,
                                               byte[] body)
                            throws IOException{                           
                        BasicProperties replyProperties = new BasicProperties.Builder().correlationId(properties.getCorrelationId()).build();
                        long deliveryTag = envelope.getDeliveryTag();
                        try{
                            LOG.info(" [" +(i++) +"] Received from first queue ");
                            byte[] dataArr = body;
                            MimeParser mimeParser = new MimeParser(true);
                            Filter filter = new Filter();
                            ByteArrayInputStream bais1 = new ByteArrayInputStream(dataArr);
                            MessageInfo mi = mimeParser.parseMessages(bais1);
                            //checking for compliance with rules
                            boolean messageAccepted = filter.getMessageAcceptability(mi.getPlainText(), customerId);
                            response = filter.getResult();
                            if(messageAccepted){
                                //sending to the other queue
                                sender.send(dataArr);
                                channel.basicPublish("", properties.getReplyTo(), replyProperties, response.getBytes());//need to add responce
                            } else {
                                channel.basicPublish("", properties.getReplyTo(), replyProperties, response.getBytes());
                            }
                        } catch (Exception e){
                            LOG.error(e);
                        }finally {
                            channel.basicAck(deliveryTag, false);
                        }

                    }
                });
    }
}

以下是分析器的快照: enter image description here

如何解决这个问题?

0 个答案:

没有答案