覆盖.properties文件中指定的Hystrix命令属性

时间:2018-01-15 06:47:43

标签: hystrix

我需要覆盖application.properties文件中指定的命令超时属性。这是我试过的

    @Test
    public void testTokenQueryTimeout() throws Exception
    {
      String propertyToSet ="hystrix.command.quickbaseTokenQueryCommand.execution.isolation.thread.timeoutInMilliseconds";
      String prop="";
      try {
        prop = ConfigurationManager.getConfigInstance().getProperty(
            propertyToSet).toString();
        logger.info("\n\n\noriginal quickbaseTokenQueryCommand timeout ="+prop);

        System.setProperty(
            propertyToSet,"10");

        prop = ConfigurationManager.getConfigInstance().getProperty(
            propertyToSet).toString();
        logger.info("\n\n\nupdated quickbaseTokenQueryCommand timeout ="+prop);

        String response = accountValidation.isValidToken(token);
        logger.info(response);
        Assert.assertFalse(true);
      }
      catch (AccountValidationServiceException e)
      {
        Assert.assertTrue(Constants.ERRCODE_TOKEN_QUERY_TIMED_OUT.equals(e.getErrorCode()));
      }
      finally {
        ConfigurationManager.getConfigInstance().clearProperty(propertyToSet);
        System.clearProperty(propertyToSet);
        if(!GeneralUtil.isObjectEmpty(System.getProperty(
            propertyToSet)))prop = System.getProperty(
            propertyToSet);

        logger.info("Updated testTokenQueryTimeout timeout ="+prop);
      }
    }

注意,System.setProperty(propertyToSet,“10”)。使用这种方法,这个测试用例通过,即属性被更改并且命令超时,但是由于此命令超时,另一个测试用例失败,尽管我正在清除System中的属性。

我还尝试使用ConfigurationManager.getConfigInstance()设置属性.setProperty(                 propertyToSet)的ToString(), “10”);但在这种情况下,此属性更改无效,命令不会超时。

我在这里缺少什么。

请帮忙。

1 个答案:

答案 0 :(得分:0)

尝试使用ConcurrentCompositeConfiguration类

application.properties

hystrix.command.HelloWorldCommand.execution.isolation.thread.timeoutInMilliseconds=200

命令

public class HelloWorldCommand extends HystrixCommand<String> {

  public HelloWorldCommand() {
    super(HystrixCommandGroupKey.Factory.asKey("HelloWorldGroup"));
  }

  @Override
  protected String run() throws Exception {
    TimeUnit.MILLISECONDS.sleep(1100);
    return "Hello";
  }
}

测试

public class HelloWorldCommandTest {

  @Test
  public void commandConfigTest() {
    String propertyKey = "hystrix.command.HelloWorldCommand.execution.isolation.thread.timeoutInMilliseconds";
    ConcurrentCompositeConfiguration config = (ConcurrentCompositeConfiguration) ConfigurationManager.getConfigInstance();

    Integer originalTimeout = (Integer) config.getProperty(propertyKey);
    config.setOverrideProperty(propertyKey, 1200);
    String result = new HelloWorldCommand().execute();
    assertThat(result, is("Hello"));

    config.setOverrideProperty(propertyKey, originalTimeout);
    Integer timeoutValue = (Integer) config.getProperty(propertyKey);
    assertThat(timeoutValue, is(originalTimeout));
  }
}