问题描述
我当时正在使用空手道框架来测试我的休息服务,它工作得很好,但是我有一些服务会消耗来自kafka主题的消息,然后坚持使用mongo最终通知kafka.我在空手道项目上创建了一个Java生产者,它被js调用以供功能使用.然后我有一个消费者来检查消息
I was working with karate framework to test my rest service and it work great, however I have service that consume message from kafka topic then persist on mongo to finally notify kafka.I made a java producer on my karate project, it called by js to be used by feature.Then I have a consumer to check the message
功能:
* def kafkaProducer = read('../js/KafkaProducer.js')
JS:
function(kafkaConfiguration){
var Producer = Java.type('x.y.core.producer.Producer');
var producer = new Producer(kafkaConfiguration);
return producer;
}
Java:
public class Producer {
private static final Logger LOGGER = LoggerFactory.getLogger(Producer.class);
private static final String KEY = "C636E8E238FD7AF97E2E500F8C6F0F4C";
private KafkaConfiguration kafkaConfiguration;
private ObjectMapper mapper;
private AESEncrypter aesEncrypter;
public Producer(KafkaConfiguration kafkaConfiguration) {
kafkaConfiguration.getProperties().put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
kafkaConfiguration.getProperties().put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer");
this.kafkaConfiguration = kafkaConfiguration;
this.mapper = new ObjectMapper();
this.aesEncrypter = new AESEncrypter(KEY);
}
public String produceMessage(String payload) {
// Just notify kafka with payload and return id of payload
}
其他班级
public class KafkaConfiguration {
private static final Logger LOGGER = LoggerFactory.getLogger(KafkaConfiguration.class);
private Properties properties;
public KafkaConfiguration(String host) {
try {
properties = new Properties();
properties.put(BOOTSTRAP_SERVERS_CONFIG, host);
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "karate-integration-test");
properties.put(ConsumerConfig.CLIENT_ID_CONFIG, "offset123");
properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
} catch (Exception e) {
LOGGER.error("Fail creating the consumer...", e);
throw e;
}
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
我想像黄瓜一样使用带注释的生产者代码:
I'd would like to use the producer code with anotation like cucumber does like:
@Then("^Notify kafka with payload (-?\\d+)$")
public void validateResult(String payload) throws Throwable {
new Producer(kafkaConfiguration).produceMessage(payload);
}
和功能使用
Then Notify kafka with payload "{example:value}"
我想这样做是因为我想在基础项目上重用该代码,以便将其包含在其他项目中如果注释不起作用,也许您可以建议我另一种方法
I want to do that because I want to reuse that code on base project in order to be included in other projectIf annotation doesn't works, maybe you can suggest me another way to do it
推荐答案
答案很简单,使用常规的Java/Maven概念.将通用Java代码移至"main"包( src/main/java
).现在,您需要做的就是构建一个JAR并将其作为对空手道项目的依赖项添加.
The answer is simple, use normal Java / Maven concepts. Move the common Java code to the "main" packages (src/main/java
). Now all you need to do is build a JAR and add it as a dependency to any Karate project.
最后一个难题是:使用 classpath:
前缀引用JAR中的任何功能或JS文件.空手道将能够接他们.
The last piece of the puzzle is this: use the classpath:
prefix to refer to any features or JS files in the JAR. Karate will be able to pick them up.
对不起,空手道不支持黄瓜或逐步定义.它有一个简单得多的方法.请阅读以下内容以获取详细信息: https://github.com/intuit/karate/issues/398
Sorry Karate does not support Cucumber or step-definitions. It has a much simpler approach. Please read this for details: https://github.com/intuit/karate/issues/398
这篇关于如何在其他空手道项目中重用util java类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!