Search code examples
javaspring-bootrabbitmqqpid

How to create queues in qpid with RestTemplate?


I'm trying to write an integration test for an application that uses RabbitMQ and for that I'm using Qpid broker. I managed to start the server and my test is connecting to it, but I need to create the queues in Qpid before startup. Because I have a large number of queues, I create the beans dynamically:

applicationContext.getBeanFactory().registerSingleton(queueName, queue);

And this requires the queues to be created before startup.

This is the qpid config file:

{
  "name": "tst",
  "modelVersion": "2.0",
  "defaultVirtualHost" : "default",
  "authenticationproviders" : [ {
    "name" : "noPassword",
    "type" : "Anonymous",
    "secureOnlyMechanisms": []
        },
    {
      "name" : "passwordFile",
      "type" : "PlainPasswordFile",
      "path" : "/src/test/resources/passwd.txt",
      "secureOnlyMechanisms": [],
      "preferencesproviders" : [{
        "name": "fileSystemPreferences",
        "type": "FileSystemPreferences",
        "path" : "${qpid.work_dir}${file.separator}user.preferences.json"
        }
      ]
    }
   ],
  "ports" : [
    {
      "name": "AMQP",
      "port": "5673",
      "authenticationProvider": "passwordFile",
      "protocols": [
        "AMQP_0_10",
        "AMQP_0_8",
        "AMQP_0_9",
        "AMQP_0_9_1"
      ]
    }],
  "virtualhostnodes" : [ {
    "name" : "default",
    "type" : "JSON",
    "virtualHostInitialConfiguration" : "{ \"type\" : \"Memory\" }"
  }]

}

An from the official documentation(https://qpid.apache.org/releases/qpid-broker-j-7.1.4/book/Java-Broker-Management-Channel-REST-API.html#d0e2130) I read that the queues can be created to REST calls so I tried to use RestTemplate to achieve that, but it doesn't seem to create the queues.

    @BeforeClass
    public static void startup() throws Exception {
        brokerStarter = new BrokerManager();
        brokerStarter.startBroker();

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.put("http://localhost:5673/api/latest/queue/default/queue1", "");
        restTemplate.put("http://localhost:5673/api/latest/queue/default/queue-2", "");
    }

Can someone explain what am I doing wrong? Thank you!


Solution

  • I managed to solve that by using connection factory:

                @Autowired
                ConnectionFactory factory;
    
                ....
                factory.setHost("localhost");
                factory.setPort(qpid_server_port);
                try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
                    String queue = "queue-x";
                    channel.queueDeclare(queue, true, false, false, null);
                    //channel.queueBind(queue, "exchange-x" , "routing-key-x");
    
                } catch (Exception e) {
                    e.printStackTrace();
                }