Example of standalone Apache Qpid (amqp) Junit Test

后端 未结 4 1457
失恋的感觉
失恋的感觉 2020-12-28 09:46

Does anyone have an example of using Apache Qpid within a standalone junit test.

Ideally I want to be able to create a queue on the fly which I can

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-28 10:08

    My solution on qpid-broker @ 6.1.1, add below to pom.xml

    
        org.apache.qpid
        qpid-broker
        6.1.1
        test
    
    

    qpid config file as:

    {
      "name" : "${broker.name}",
      "modelVersion" : "6.1",
      "defaultVirtualHost" : "default",
      "authenticationproviders" : [ {
        "name" : "anonymous",
        "type" : "Anonymous"
      } ],
      "ports" : [ {
        "name" : "AMQP",
        "port" : "${qpid.amqp_port}",
        "authenticationProvider" : "anonymous",
        "virtualhostaliases" : [ {
          "name" : "defaultAlias",
          "type" : "defaultAlias"
        } ]
      } ],
      "virtualhostnodes" : [ {
        "name" : "default",
        "type" : "JSON",
        "defaultVirtualHostNode" : "true",
        "virtualHostInitialConfiguration" : "{ \"type\" : \"Memory\" }"
      } ]
    }
    

    code to start the qpid server

    Broker broker = new Broker();
    BrokerOptions brokerOptions = new BrokerOptions();
    // I use fix port number
    brokerOptions.setConfigProperty("qpid.amqp_port", "20179");
    brokerOptions.setConfigurationStoreType("Memory");
    // work_dir for qpid's log, configs, persist data
    System.setProperty("qpid.work_dir", "/tmp/qpidworktmp");
    // init config of qpid. Relative path for classloader resource or absolute path for non-resource
    System.setProperty("qpid.initialConfigurationLocation", "qpid/qpid-config.json");
    brokerOptions.setStartupLoggedToSystemOut(false);
    broker.startup(brokerOptions);
    

    code to stop qpid server

    broker.shutdown();
    

    Since I use anonymouse mode, client should do like:

    SaslConfig saslConfig = new SaslConfig() {
        public SaslMechanism getSaslMechanism(String[] mechanisms) {
            return new SaslMechanism() {
                public String getName() {
                    return "ANONYMOUS";
                }
    
                public LongString handleChallenge(LongString challenge, String username, String password) {
                    return LongStringHelper.asLongString("");
                }
            };
        }
    };
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setPort(20179);
    factory.setSaslConfig(saslConfig);
    
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    

    That's all. A little more on how to do it on other version.

    You can download qpid-broker binary package from official site. After download and unzip, you can run it to test as server against your case. After your case connected server well, using commandline to generate or just copy the initial config file in QPID_WORK, remove useless id filed and use it for embedded server like above.

    The most complicated thing is the authentication. You can choose PLAIN mode but you have to add the username and password in initial config. I choose anonymous mode which need a little code when connecting. For other authentication mode you have specify the password file or key/cert store, which I didnt try.

    If it still not working, you can read the qpid-borker doc and Main class code in qpid-broker artifact which show how command line works for each settings.

提交回复
热议问题