简体   繁体   中英

Is RabbitMQ's AMQP.BasicProperties.Builder thread-safe?

Title is pretty much self-explanatory.

Is AMQP.BasicProperties.Builder thread-safe? Citations/sources?

I can't find anything that indicates either way...

No, AMQP.BasicProperties.Builder is not thread-safe. I tested it using the following class:

package com.anarsoft.agent;
import java.util.concurrent.atomic.AtomicInteger;
import com.rabbitmq.client.AMQP;

public class TestRabbitMq extends Thread {

static  final AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
static final AtomicInteger threadCount = new AtomicInteger();

public  void  run() {


    builder.clusterId("444").build();
    threadCount.decrementAndGet();

}

public static void main(String[] args) throws Exception
{


    for( int i = 0 ; i < 8 ; i++)
    {
        TestRabbitMq testNumberFormat = new TestRabbitMq();
        threadCount.addAndGet(1);
        testNumberFormat.start();
    }

    while(threadCount.get() > 0)
    {
        Thread.sleep(1000);
    }   
}
}

http://vmlens.com then shows me that fields are accessed without synchronization. Trace generated by http://vmlens.com :

com/rabbitmq/client/AMQP$BasicProperties$Builder.clusterId@28470003 (8,8)
Thread-0 (9)  

Thread-1 (10)  com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.anarsoft.agent.TestRabbitMq.run

Thread-2 (11)  com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.anarsoft.agent.TestRabbitMq.run

Thread-3 (12)  com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.anarsoft.agent.TestRabbitMq.run

Thread-4 (13)  com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.anarsoft.agent.TestRabbitMq.run

Thread-5 (14)  com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.anarsoft.agent.TestRabbitMq.run

Thread-6 (15)  com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.anarsoft.agent.TestRabbitMq.run

Thread-7 (16)  com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.rabbitmq.client.AMQP$BasicProperties$Builder.clusterId
   com.anarsoft.agent.TestRabbitMq.run

you can also see this the source that its not threadsafe: http://grepcode.com/file/repo1.maven.org/maven2/com.rabbitmq/amqp-client/2.6.1/com/rabbitmq/client/AMQP.java#AMQP.BasicProperties.Builder

public Builder   clusterId(String clusterId)
  {   this.clusterId = clusterId; return this; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM