Embedded AMQP Java Broker

前端 未结 5 564
無奈伤痛
無奈伤痛 2021-01-31 10:38

I am trying to create integration test for a Scala / Java application that connects to a RabbitMQ broker. To achieve this I would like an embedded broker that speaks AMQP that I

5条回答
  •  梦谈多话
    2021-01-31 11:34

    Here's the solution proposed by OrangeDog adapted to Qpid Broker 7.x, inspired from here:

    Add qpid 7.x as test dependecies. In 7.x these have been separated in core + plugins, depending on what you need. For RabbitMQ AMQP version you'll need qpid-broker-plugins-amqp-0-8-protocol and for running in-memory (sufficient for integration tests) use qpid-broker-plugins-memory-store.

    pom.xml:

    ...
    
        ...
        7.0.2
    
    
    
        ...
        
            org.apache.qpid
            qpid-broker-core
            ${qpid-broker.version}
            test
        
        
            org.apache.qpid
            qpid-broker-plugins-amqp-0-8-protocol
            ${qpid-broker.version}
            test
        
        
            org.apache.qpid
            qpid-broker-plugins-memory-store
            ${qpid-broker.version}
            test
        
    
    ...
    

    Add broker configuration with hard-coded user/password and default in-memory virtual host mapped to default port (5672):

    qpid-config.json:

    {
      "name": "EmbeddedBroker",
      "modelVersion": "7.0",
      "authenticationproviders": [
        {
          "name": "password",
          "type": "Plain",
          "secureOnlyMechanisms": [],
          "users": [{"name": "guest", "password": "guest", "type": "managed"}]
        }
      ],
      "ports": [
        {
          "name": "AMQP",
          "port": "${qpid.amqp_port}",
          "authenticationProvider": "password",
          "virtualhostaliases": [
            {
              "name": "defaultAlias",
              "type": "defaultAlias"
            }
          ]
        }
      ],
      "virtualhostnodes": [
        {
          "name": "default",
          "defaultVirtualHostNode": "true",
          "type": "Memory",
          "virtualHostInitialConfiguration": "{\"type\": \"Memory\" }"
        }
      ]
    }
    

    Define junit ExternalResource and declare as ClassRule (or start and close your embedded broker in your IT @BeforeClass and @AfterClass methods):

    EmbeddedAMQPBroker.java:

    public class EmbeddedAMQPBroker extends ExternalResource {
    
        private final SystemLauncher broker = new SystemLauncher();
    
        @Override
        protected void before() throws Throwable {
            startQpidBroker();
            //createExchange();
        }
    
        @Override
        protected void after() {
            broker.shutdown();
        }
    
        private void startQpidBroker() throws Exception {
            Map attributes = new HashMap<>();
            attributes.put("type", "Memory");
            attributes.put("initialConfigurationLocation", findResourcePath("qpid-config.json"));
            broker.startup(attributes);
        }
    
        private String findResourcePath(final String fileName) {
            return EmbeddedAMQPBroker.class.getClassLoader().getResource(fileName).toExternalForm();
        }
    }
    

    Integration test:

    public class MessagingIT{
        @ClassRule
        public static EmbeddedAMQPBroker embeddedAMQPBroker = new EmbeddedAMQPBroker();
    
        ...
    }
    

提交回复
热议问题