有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

独立ApacheQPID(amqp)Junit测试的java示例

有没有人有在独立测试中使用apacheqpid的例子

理想情况下,我希望能够动态创建一个队列,以便在测试中放置/获取MSG。 因此,我不会在测试中测试QPid,我将使用集成测试来实现这一点,但是,这对于测试处理MSG的方法非常有用,因为它们必须模拟服务负载


共 (4) 个答案

  1. # 1 楼答案

    我在qpid broker@6.1.1上的解决方案,添加到pom中。xml

    <dependency>
        <groupId>org.apache.qpid</groupId>
        <artifactId>qpid-broker</artifactId>
        <version>6.1.1</version>
        <scope>test</scope>
    </dependency>
    

    qpid配置文件为:

    {
      "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\" }"
      } ]
    }
    

    启动qpid服务器的代码

    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);
    

    停止qpid服务器的代码

    broker.shutdown();
    

    由于我使用的是匿名模式,所以客户端应该执行以下操作:

    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();
    

    就这些。 更多关于如何在其他版本上执行此操作的信息

    您可以从官方网站下载qpid代理二进制软件包。下载并解压缩后,您可以将其作为服务器运行,以针对您的案例进行测试。在您的案例连接好服务器后,使用命令行生成或只是复制QPID_工作中的初始配置文件,删除无用的id文件并将其用于上述嵌入式服务器

    最复杂的是身份验证。您可以选择普通模式,但必须在初始配置中添加用户名和密码。我选择匿名模式,连接时需要一些代码。对于其他身份验证模式,您必须指定密码文件或密钥/证书存储,我没有尝试

    如果它仍然不工作,您可以阅读qpid代理工件中的qpid borker文档和主类代码,这些代码显示了命令行如何为每个设置工作

  2. # 2 楼答案

    我能想出的最好办法是:

    PropertiesConfiguration properties = new PropertiesConfiguration();
    properties.addProperty("virtualhosts.virtualhost.name", "test");
    properties.addProperty("security.principal-databases.principal-database.name", "testPasswordFile");
    properties.addProperty("security.principal-databases.principal-database.class", "org.apache.qpid.server.security.auth.database.PropertiesPrincipalDatabase");
    ServerConfiguration config = new ServerConfiguration(properties);
    ApplicationRegistry.initialise(new ApplicationRegistry(config) {
        @Override
        protected void createDatabaseManager(ServerConfiguration configuration) throws Exception {
            Properties users = new Properties();
            users.put("guest","guest");
            users.put("admin","admin");
            _databaseManager = new PropertiesPrincipalDatabaseManager("testPasswordFile", users);
        }
    });
    TransportConnection.createVMBroker(ApplicationRegistry.DEFAULT_INSTANCE);
    

    URL为:

    amqp://admin:admin@/test?brokerlist='vm://:1?sasl_mechs='PLAIN''
    

    最大的问题是配置和授权。差异可能会有所不同

  3. # 3 楼答案

    下面是我在QPID0.30中使用的设置方法(我在Spock测试中使用了这个方法,但是应该可以移植到Junit的Java中,没有问题)。这支持SSL连接、HTTP管理,并且仅使用内存启动。启动时间为亚秒。与出于相同目的使用ActiveMQ相比,QPID的配置很难实现,但QPID符合AMQP,并允许对AMQP客户端进行平稳、中立的测试(显然,使用Exchange不能模仿RabbitMQs实现,但就基本目的而言,这就足够了)

    首先,我创建了一个最小的测试配置。我放在resources文件夹中的json:

    {
      "name": "${broker.name}",
      "modelVersion": "2.0",
      "defaultVirtualHost" : "default",
      "authenticationproviders" : [ {
        "name" : "passwordFile",
        "type" : "PlainPasswordFile",
        "path" : "${qpid.home_dir}${file.separator}etc${file.separator}passwd",
        "preferencesproviders" : [{
            "name": "fileSystemPreferences",
            "type": "FileSystemPreferences",
            "path" : "${qpid.work_dir}${file.separator}user.preferences.json"
        }]
      } ],
      "ports" : [  {
        "name" : "AMQP",
        "port" : "${qpid.amqp_port}",
        "authenticationProvider" : "passwordFile",
        "keyStore" : "default",
        "protocols": ["AMQP_0_10", "AMQP_0_8", "AMQP_0_9", "AMQP_0_9_1" ],
        "transports" : [ "SSL" ]
      }, {
        "name" : "HTTP",
        "port" : "${qpid.http_port}",
        "authenticationProvider" : "passwordFile",
        "protocols" : [ "HTTP" ]
      }],
      "virtualhostnodes" : [ {
        "name" : "default",
        "type" : "JSON",
        "virtualHostInitialConfiguration" : "{ \"type\" : \"Memory\" }"
      } ],
      "plugins" : [ {
        "type" : "MANAGEMENT-HTTP",
        "name" : "httpManagement"
      }],
      "keystores" : [ {
         "name" : "default",
            "password" : "password",
          "path": "${qpid.home_dir}${file.separator}keystore.jks"
    
        }]
    }
    

    我 我还需要创建一个密钥库。jks文件,因为QPID代理和RabbitMQ客户端不喜欢通过未加密的通道进行通信。我还在“integTest/resources/etc”中添加了一个名为“passwd”的文件,该文件包含以下内容:

    客人:密码

    以下是单元测试设置中的代码:

    类级别变量:

    def tmpFolder = Files.createTempDir()
    Broker broker
    
    def amqpPort = PortFinder.findFreePort()
    def httpPort = PortFinder.findFreePort()
    
    def qpidHomeDir = 'src/integTest/resources/'
    def configFileName = "/test-config.json"
    

    setup()方法的代码:

       def setup() {
    
        broker = new Broker();
        def brokerOptions = new BrokerOptions()
    
        File file = new File(qpidHomeDir)
        String homePath = file.getAbsolutePath();
        log.info(' qpid home dir=' + homePath)
        log.info(' qpid work dir=' + tmpFolder.absolutePath)
    
        brokerOptions.setConfigProperty('qpid.work_dir', tmpFolder.absolutePath);
    
        brokerOptions.setConfigProperty('qpid.amqp_port',"${amqpPort}")
        brokerOptions.setConfigProperty('qpid.http_port', "${httpPort}")
        brokerOptions.setConfigProperty('qpid.home_dir', homePath);
    
    
        brokerOptions.setInitialConfigurationLocation(homePath + configFileName)
        broker.startup(brokerOptions)
        log.info('broker started')
    }
    

    清理代码()

    broker.shutdown()
    

    要从Rabbit MQ客户端建立AMQP连接,请执行以下操作:

            ConnectionFactory factory = new ConnectionFactory();
            factory.setUri("amqp://guest:password@localhost:${amqpPort}");
            factory.useSslProtocol()
    
            log.info('about to make connection')
    
    
            def connection = factory.newConnection();
            //get a channel for sending the "kickoff" message
            def channel = connection.createChannel();
    
  4. # 4 楼答案

    Qpid项目有许多使用嵌入式代理进行测试的测试。虽然我们使用基本情况来处理启动-关闭,但您可以执行以下操作,以便在测试中简单地集成代理:

    public void setUp()
    {
            int port=1;
    
    // Config is actually a Configuaration File App Registry object, or Configuration Application Registry.
    
            ApplicationRegistry.initialise(config, port);
    
            TransportConnection.createVMBroker(port);        
    }
    
    public void test()
    {...}
    
    public void tearDown()
    {
                TransportConnection.killVMBroker(port);
                ApplicationRegistry.remove(port);
    }
    

    然后,对于连接,您需要为代理指定连接URL。i、 e.borkerlist='vm://1'