Using Spring Framework for RabbitMQ reduces performance

I created a producer that used com.rabbitmq.client.connectionFactoryand sent 1,000,000 messages (40 bytes) in 100 seconds.

But now I want an abstraction of spring. I was unable to use com.rabbitmq.client.connectionFactory, instead I had to use org.springframework.amqp.rabbit.connection.SingleConnectionFactory. Using this factory connection, 100,000 messages (40 bytes) are sent to the broker after 100 seconds.

Does anyone have any experience why performance drops so much (about 90%).

Code using "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);
        }
    }
}

Code using SpringFramework: →

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>
+3
source share
2 answers
<?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 com.rabbitmq.client.connectionFactory connectionfactory bean. : " - 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

, MQ Rabbit? / RabbitMQ?

, , - jvm. , , ? top , jvm .

RabbitMQ. Linux RabbitMQ 1.7.2, , . , , RabbitMQ 40% , , . RabbitMQ, , , . RabbitMQ 2.4.1 , , .

- Java, Spring - , RAM gc. -Xmx?

+1

All Articles