对RabbitMQ使用Spring Framework会降低性能

时间:2011-06-15 14:47:42

标签: spring rabbitmq

我创建了一个生产者,它使用com.rabbitmq.client.connectionFactory并在100秒内发送1,000,000条消息(40字节)。

但现在我想要一个春天的抽象。我无法使用com.rabbitmq.client.connectionFactory,而是必须使用org.springframework.amqp.rabbit.connection.SingleConnectionFactory。使用此连接工厂,在100秒内仅向代理发送100,000条消息(40字节)。

有没有人有经验为什么性能会降低这么多(约90%)。

使用“import com.rabbitmq.client.ConnectionFactory;”的代码是 - >

package Multiple_queues_multiple_consumers;
import java.io.IOException;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer {
    private static Connection myConnection;
    private static Channel myChannel;
    public static String myQueueName;

    public static void main(String[] args) throws IOException {
        long startTime=0;
        int count=0;
        ConnectionFactory myFactory=new ConnectionFactory();
        myFactory.setHost("localhost");
        try {
            myConnection = myFactory.newConnection();
            myChannel = myConnection.createChannel();
            String myExchange = "wxyzabc";
            String myBody = "This is a message : message numberxxxxxx";
            String myRoutingKey = "RoutingKey";
            myQueueName = "new_Queue";
            myChannel.exchangeDeclare(myExchange, "direct", true, false, null);
            myChannel.queueDeclare(myQueueName, true, false, false, null);
            myChannel.queueBind(myQueueName, myExchange, myRoutingKey);
            startTime=System.currentTimeMillis();
            AMQP.BasicProperties properties = new AMQP.BasicProperties();
            properties.setDeliveryMode(2);
            startTime=System.currentTimeMillis();
            while(count++<=10000){
                myChannel.basicPublish(myExchange, myRoutingKey, true, true, properties, myBody.getBytes() );
            }
            System.out.println(System.currentTimeMillis()-startTime);
        } catch (Exception e){
            System.exit(0);
        }
    }
}

使用SpringFramework的代码是: - &gt;

Producer1.java

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Producer1 {
    public static void main(String[] args) {

        ConfigurableApplicationContext context = new         ClassPathXmlApplicationContext("Producer1.xml");
    AmqpAdmin amqpAdmin = context.getBean(RabbitAdmin.class);
    Queue queue = new Queue("sampleQueue");
    DirectExchange exchange = new DirectExchange("myExchange");
    Binding binding = new Binding(queue, exchange, "");
    amqpAdmin.declareQueue(queue);
    amqpAdmin.declareExchange(exchange);
    amqpAdmin.declareBinding(binding);
    RabbitTemplate rabbitTemplate = context.getBean(RabbitTemplate.class);
    String routingKey = "";
    String myBody = "This is a message : message numberxxxxxx";
    Message Msg = new Message(myBody.getBytes(), null);
    int count=0;
    long CurrTime = System.currentTimeMillis();
    while(count++<=10000){
        rabbitTemplate.send(routingKey, Msg);
        //System.out.println("Message Sent");
    }
    System.out.println(System.currentTimeMillis()-CurrTime);
}
}

Producer1.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<!-- Define a connectionFactory -->
<bean id="rabbitConnectionFactory" class="com.rabbitmq.client.ConnectionFactory">
<property name="host" value="localhost" />
</bean>

<bean id="connectionFactory"         class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
<constructor-arg ref="rabbitConnectionFactory"/>
</bean>

<!-- Tell the Admin bean about that connectionFactory and initialize it, create a     queue and an exchange on Rabbit Broker using the RabbitTemplate provided by Spring framework-Rabbit APIs -->
<bean id="Admin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>

<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"
p:connectionFactory-ref="connectionFactory"
p:routingKey="myRoutingKey"
p:exchange="myExchange" />

</beans>

2 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- Define a connectionFactory -->
<bean id="connectionFactory" class="com.rabbitmq.client.connectionFactory">
<constructor-arg value="localhost" />
<property name="username" value="guest" />
<property name="password" value="guest" />
</bean>

<bean id="Admin" class="org.springframework.amqp.rabbit.core.RabbitAdmin">
<constructor-arg ref="connectionFactory" />
</bean>

</beans> 

使用此xml文件,出现错误,说org.springframework.amqp.rabbit.core.RabbitAdmin无法为connectionfactory bean投射com.rabbitmq.client.connectionFactory。 确切的错误是:“嵌套异常是java.lang.IllegalStateException:无法将[com.rabbitmq.client.ConnectionFactory]类型的值转换为所需类型[org.springframework.amqp.rabbit.core.RabbitTemplate]:没有匹配的编辑器或转换策略找到“。

因此我必须使用bean:

<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.SingleConnectionFactory">
</bean>

答案 1 :(得分:1)

您确定使用了相同的Rabbit MQ代理吗?你可以在不同的服务器上使用代理,还是在RabbitMQ的升级/降级版本中使用?

另一件要看的是你的jvm。是否有可能您没有配置足够的内存,现在垃圾收集器正在颠簸?运行top并查看jvm的内存使用情况是否接近配置的内存大小。

您使用的是旧版RabbitMQ吗?许多Linux发行版包括RabbitMQ 1.7.2,这是一个旧版本,有大量消息的问题。 Large很难定义,因为它取决于你的RAM,但是RabbitMQ不喜欢使用超过40%的RAM,因为它需要复制持久性事务日志以便处理它并清理它以进行日志翻转。这可能导致RabbitMQ崩溃,当然,处理大型日志会降低速度。 RabbitMQ 2.4.1以更小的块更好地处理持久性日志,并且它还具有更快,更快的消息路由代码。

这听起来像是一个Java问题,要么Spring只是一只猪而且非常低效,或者你没有给你的jvm足够的RAM以避免频繁的gc运行。您对-Xmx使用了什么设置?