Java 为什么CloudWatchConfig接口需要一个步骤持续时间字符串
我使用的是千分尺Cloudwatch 1.1.3,Gradle作为Java 为什么CloudWatchConfig接口需要一个步骤持续时间字符串,java,kotlin,micrometer,spring-micrometer,Java,Kotlin,Micrometer,Spring Micrometer,我使用的是千分尺Cloudwatch 1.1.3,Gradle作为编译'io.千分尺:千分尺注册表Cloudwatch:1.1.3' 在Java中,我可以通过执行以下操作创建CloudWatchConfig: CloudWatchConfig cloudWatchConfig = new CloudWatchConfig() { @Override public String get(String s) { return "my-s
编译'io.千分尺:千分尺注册表Cloudwatch:1.1.3'
在Java中,我可以通过执行以下操作创建CloudWatchConfig
:
CloudWatchConfig cloudWatchConfig = new CloudWatchConfig() {
@Override
public String get(String s) {
return "my-service-metrics";
}
@Override
public boolean enabled() {
return true;
}
@Override
public Duration step() {
return Duration.ofSeconds(30);
}
@Override
public int batchSize() {
return CloudWatchConfig.MAX_BATCH_SIZE;
}
};
val cloudWatchConfig = CloudWatchConfig {
fun get(s:String) = "my-service-metrics"
fun enabled() = true
fun step() = Duration.ofSeconds(30)
fun batchSize() = CloudWatchConfig.MAX_BATCH_SIZE
step().toString()
}
我认为Kotlin中的等价物应该是:
val cloudWatchConfig = CloudWatchConfig {
fun get(s:String) = "my-service-metrics"
fun enabled() = true
fun step() = Duration.ofSeconds(30)
fun batchSize() = CloudWatchConfig.MAX_BATCH_SIZE
}
Koltin编译器失败了,指出了块中的最后一行:fun batchSize()=CloudWatchConfig.MAX\u BATCH\u SIZE
表示它需要字符串类型的值
经过多次调试,我能够通过返回step函数的toString来修复这个问题。您不能只传递任何字符串,因为它将被解析,就像它是由持续时间生成的一样。我的Kotlin代码现在可以工作了,如下所示:
CloudWatchConfig cloudWatchConfig = new CloudWatchConfig() {
@Override
public String get(String s) {
return "my-service-metrics";
}
@Override
public boolean enabled() {
return true;
}
@Override
public Duration step() {
return Duration.ofSeconds(30);
}
@Override
public int batchSize() {
return CloudWatchConfig.MAX_BATCH_SIZE;
}
};
val cloudWatchConfig = CloudWatchConfig {
fun get(s:String) = "my-service-metrics"
fun enabled() = true
fun step() = Duration.ofSeconds(30)
fun batchSize() = CloudWatchConfig.MAX_BATCH_SIZE
step().toString()
}
在查看了CloudWatchConfig、StepRegisteryConfig和MeterRegistryConfig接口之后,我无法理解为什么会出现这种情况。Koltin为什么要这样做,为什么它期望持续时间的toString?在Java中创建一个匿名类的等价物,语法有点不同。您需要使用
对象
关键字,还需要为接口方法包含覆盖
关键字。e、 g
val cloudWatchConfig = object : CloudWatchConfig {
override fun get(key: String) = "my-service-metrics"
override fun enabled() = true
override fun step() = Duration.ofSeconds(30)
override fun batchSize() = CloudWatchConfig.MAX_BATCH_SIZE
}
要在Java中创建与匿名类等效的类,语法有点不同。您需要使用
对象
关键字,还需要为接口方法包含覆盖
关键字。e、 g
val cloudWatchConfig = object : CloudWatchConfig {
override fun get(key: String) = "my-service-metrics"
override fun enabled() = true
override fun step() = Duration.ofSeconds(30)
override fun batchSize() = CloudWatchConfig.MAX_BATCH_SIZE
}