如何使用Reactor而不是java的调度器获得相同的效果?
Executors.newSingleThreadScheduledExecutor()
.scheduleAtFixedRate(() -> counter.set(0) , computeDelay(), computePeriod(), TimeUnit.MILLISECONDS)
Run Code Online (Sandbox Code Playgroud)
我试过
Flux
.interval(Duration.ofMillis(computeDelay()), Duration.ofMinutes(RESET_PERIOD_MINUTES))
.doOnNext( counter.set(0))
.subscribe())
Run Code Online (Sandbox Code Playgroud)
但它会产生不必要的 Long 值。我在 Flux API 中找到了一些调度程序,但在尝试创建一个调度程序时,我得到了 Disposable 对象,然后我不知道应该用它做什么
我尝试在“设置”->“代码样式”->“Java 默认可见性”中设置复选框,但它似乎不起作用。此选项仅适用于方法,也许还适用于构造函数(不确定)。我有2018.1 EAP版本
我知道第二个是基于第一个,但我很好奇除了 API 之外还有什么区别?是否可以与 Reactor 建立推送模型连接?我将创建一个实时应用程序,因此我必须找到最适合此目的的应用程序。我将不胜感激任何帮助:)
我有一些用 Spock 编写的测试,其中涵盖了我的 Java 代码。现在我迁移到 Kotlin,问题是我无法模拟最终课程,所以我决定使用这里描述的 Mockito 插件:https : //github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#不可模仿的
问题是我需要用 Mockitos 的 any()、anyString()、when()、then() 等替换每个 '_' '>>' 吗?我试图使用 Mockito 模拟最终课程,但似乎不起作用。
如果我必须替换在这种情况下使用 Spock 测试框架的优势是什么?也许我应该删除它并只和 Mockito 呆在一起?
我有很多类似这样的电话。问题是下一个调用完全取决于前一个调用。如果没有任何对话,从他们那里获取消息是没有意义的,所以我只想打破这个链条。我读了一些霍尔格答案的主题,但我觉得我仍然没有完全理解这一点。有人可以给我一些基于这段代码的例子吗?
public CompletableFuture<Set<Conversation>> fetchConversations(List<Information> data, String sessionId)
{
return myservice
.get(prepareRequest(data, sessionId))
.thenApply(HtmlResponse::getDocument)
.thenApply(this::extractConversationsFromDocument);
}
public CompletableFuture<Elements> fetchMessagesFromConversation(String Url, String sessionId)
{
return mySerice
.get(prepareRequest(url, sessionId))
.thenApply(HtmlResponse::getDocument)
.thenApply(this::extractMessageFromConversation);
}
Run Code Online (Sandbox Code Playgroud) bestSword = {
{name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
{name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
{name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
{name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
{name = 'aaa' , lvl = 60, atk = …Run Code Online (Sandbox Code Playgroud) 我必须使用Streams API从给定文件中找到所有最长的单词.我做了它在几个步骤,但寻找一些"一个班轮",其实我处理整个文件两次,第一次找字和第二的最大长度为所有比较的最大长度,假设它不是最好的表现; P有人能帮帮我吗?看看代码:
public class Test {
public static void main(String[] args) throws IOException {
List<String> words = Files.readAllLines(Paths.get("alice.txt"));
OptionalInt longestWordLength = words.stream().mapToInt(String::length).max();
Map<Integer, List<String>> groupedByLength = words.stream().collect(Collectors.groupingBy(String::length));
List<String> result = groupedByLength.get(longestWordLength.getAsInt());
}
}
Run Code Online (Sandbox Code Playgroud)
我想直截了当:
List<String> words = Files.readAllLines(Paths.get("alice.txt"));
List<String> result = // code
Run Code Online (Sandbox Code Playgroud)
文件每行只包含一个单词,无论如何它并不重要 - 问题是关于正确的流代码.
我读了一些关于测试命名约定的文章,并决定使用带有“should”的文章。它在大多数情况下都工作得很好,例如:
但是我在测试 DecimalRepresentation 类时遇到了问题,该类显示不同数字系统中的数字,只需查看代码:
public class DecimalRepresentationTest {
private DecimalRepresentation decimal;
@BeforeEach
void setup() {
decimal = new DecimalRepresentation();
}
@Test
void shouldReturnZeroIfNumberNotSpecified() {
assertEquals("0", decimal.toBinary());
}
@Test
void shouldReturn10IfNumber2() {
decimal.setNumber(2);
assertEquals("10", decimal.toBinary());
}
@Test
void shouldReturn1111IfNumber15() {
decimal.setNumber(15);
assertEquals("1111", decimal.toBinary());
}
}
Run Code Online (Sandbox Code Playgroud)
现在还不错,但如果我测试负输入,它看起来很糟糕:
@Test
void shouldReturn11111111111111111111111110001000IfNumberNegative120() {
decimal.setNumber(-120);
assertEquals("11111111111111111111111110001000", decimal.toBinary());
}
@Test
void shouldReturn11111111111111111111111111111111IfNumberNegative1() {
decimal.setNumber(-1);
assertEquals("11111111111111111111111111111111", decimal.toBinary());
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我对正输入和负输入进行了两次测试,以确保没有硬编码结果并且算法工作正常,因此我决定将测试分组到嵌套类中以保持约定:
@Nested
@DisplayName("Tests for positive numbers")
class PositiveConverter {
@Test
void shouldReturn10IfNumber2() {
decimal.setNumber(2);
assertEquals("10", decimal.toBinary()); …Run Code Online (Sandbox Code Playgroud) 我不知道发生了什么事,我用 @KafkaListener 注释的 java 客户端消费者没有收到任何消息。当我通过命令行创建消费者时,它可以工作。Producer 也按预期工作(也在 java 中)。有人可以帮助我理解这种行为吗?
应用程序.yml
kafka:
bootstrap-servers: localhost:9092
topic: my-topic
Run Code Online (Sandbox Code Playgroud)
生产者配置:
@Configuration
public class KafkaProducerConfig {
@Value("${kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public ProducerFactory<String, String> producerFactory(){
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<>(configProps);
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate(){
return new KafkaTemplate<>(producerFactory());
}
}
Run Code Online (Sandbox Code Playgroud)
消费者配置:
@EnableKafka
@Configuration
class KafkaConsumerConfig {
@Value("${kafka.bootstrap-servers}")
String bootstrapServers;
@Bean
public ConsumerFactory<String, String> consumerFactory(){
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, …Run Code Online (Sandbox Code Playgroud) java ×7
unit-testing ×2
apache-kafka ×1
class ×1
iterator ×1
java-8 ×1
java-stream ×1
junit ×1
junit5 ×1
lua ×1
lua-table ×1
mockito ×1
netty ×1
real-time ×1
spock ×1
spring-kafka ×1
testing ×1
visibility ×1