小编use*_*312的帖子

如何从Java中的常量为注释提供枚举值

我无法使用从常量中取得的枚举作为注释中的参数.我得到这个编译错误:"注释属性[attribute]的值必须是枚举常量表达式".

这是Enum代码的简化版本:

public enum MyEnum {
    APPLE, ORANGE
}
Run Code Online (Sandbox Code Playgroud)

对于注释:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface MyAnnotation {
    String theString();

    int theInt();

    MyEnum theEnum();
}
Run Code Online (Sandbox Code Playgroud)

和班级:

public class Sample {
    public static final String STRING_CONSTANT = "hello";
    public static final int INT_CONSTANT = 1;
    public static final MyEnum MYENUM_CONSTANT = MyEnum.APPLE;

    @MyAnnotation(theEnum = MyEnum.APPLE, theInt = 1, theString = "hello")
    public void methodA() {

    }

    @MyAnnotation(theEnum = MYENUM_CONSTANT, theInt = INT_CONSTANT, theString = STRING_CONSTANT)
    public void methodB() {

    }

} …
Run Code Online (Sandbox Code Playgroud)

java enums annotations

65
推荐指数
4
解决办法
5万
查看次数

卡夫卡模式订阅.新主题没有触发重新平衡

根据关于kafka javadocs的文档,如果我:

  • 订阅模式
  • 创建与模式匹配的主题

应该发生重新平衡,这使消费者从该新主题中读取.但那并没有发生.

如果我停止并启动消费者,它确实会选择新主题.所以我知道新主题与模式匹配.在/sf/ask/2598437621/中可能存在此问题的重复,但这个问题无处可去.

我看到kafka日志并没有错误,它只是不会触发重新平衡.当消费者加入或死,而不是在创建新的主题重新平衡触发(甚至当分区被添加到现有的话题,但这是另一个主题).

我正在使用kafka 0.10.0.0和"新消费者API"的官方Java客户端,意思是代理GroupCoordinator而不是胖客户端+ zookeeper.

这是示例消费者的代码:

public class SampleConsumer {
public static void main(String[] args) throws IOException {
    KafkaConsumer<String, String> consumer;
    try (InputStream props = Resources.getResource("consumer.props").openStream()) {
        Properties properties = new Properties();
        properties.load(props);
        properties.setProperty("group.id", "my-group");

        System.out.println(properties.get("group.id"));
        consumer = new KafkaConsumer<>(properties);
    }
    Pattern pattern = Pattern.compile("mytopic.+");
    consumer.subscribe(pattern, new SampleRebalanceListener());
    while (true) {
        ConsumerRecords<String, String> records = consumer.poll(1000);
        for (ConsumerRecord<String, String> record : records) {
            System.out.printf("%s %s\n", record.topic(), record.value());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

} …

java apache-kafka kafka-consumer-api

5
推荐指数
1
解决办法
2659
查看次数