我想我会研究 Pivotal 新发布的反应器框架,用于我正在编写的一个简单程序,该程序需要一些多线程来及时完成。
我编写了以下示例项目来了解框架并使用它来了解它的使用方式:
主.java:
package reactortest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) throws InterruptedException {
try(AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfiguration.class)) {
MyProducer producer = context.getBean(MyProducer.class);
producer.run();
}
}
}
MyProducer.java:
package reactortest;
import java.util.concurrent.CountDownLatch;
import reactor.core.Reactor;
import reactor.event.Event;
public class MyProducer {
private final Reactor reactor;
private final Integer messagesToPrint;
private final CountDownLatch countDownLatch;
public MyProducer(final Reactor reactor, final Integer messagesToPrint, CountDownLatch countDownLatch) {
this.reactor = reactor;
this.messagesToPrint = messagesToPrint;
this.countDownLatch = countDownLatch;
}
public void run() throws InterruptedException {
for(int i = 0; i < messagesToPrint; ++i) {
reactor.notify(Event.wrap("String event: " + i));
}
countDownLatch.await();
System.out.println("Finished. Remaining count is: " + countDownLatch.getCount());
}
}
MyConsumer.java:
package reactortest;
import java.util.concurrent.CountDownLatch;
import reactor.event.Event;
import reactor.function.Consumer;
public class MyConsumer implements Consumer<Event<String>> {
private final CountDownLatch countDownLatch;
public MyConsumer(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void accept(Event<String> message) {
System.out.println(message);
countDownLatch.countDown();
}
}
最后是 MainConfiguration.java:
package reactortest;
import java.util.concurrent.CountDownLatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.Environment;
import reactor.core.Reactor;
import reactor.core.spec.Reactors;
import reactor.spring.context.config.EnableReactor;
@Configuration
@EnableReactor
public class MainConfiguration {
private final Integer MESSAGESTOPRINT = 10;
@Autowired private Environment environment;
@Bean
public CountDownLatch countDownLatch() {
CountDownLatch countDownLatch = new CountDownLatch(MESSAGESTOPRINT);
return countDownLatch;
}
@Bean
public Reactor reactor() {
Reactor reactor = Reactors.reactor().env(environment).dispatcher(Environment.THREAD_POOL).randomEventRouting().get();
reactor.on(consumer());
return reactor;
}
@Bean
public MyProducer producer() {
MyProducer producer = new MyProducer(reactor(), MESSAGESTOPRINT, countDownLatch());
return producer;
}
@Bean
public MyConsumer consumer() {
MyConsumer consumer = new MyConsumer(countDownLatch());
return consumer;
}
}
我的问题是程序永远不会完成。消费者每次运行时也会打印出不同的信息。从连续三个运行它打印:
1st run:
Event{id=null, headers=null, replyTo=null, data=String event: 0}
Event{id=null, headers=null, replyTo=null, data=String event: 1}
Event{id=null, headers=null, replyTo=null, data=String event: 7}
Event{id=null, headers=null, replyTo=null, data=String event: 8}
2nd run:
Event{id=null, headers=null, replyTo=null, data=String event: 0}
Event{id=null, headers=null, replyTo=null, data=String event: 1}
Event{id=null, headers=null, replyTo=null, data=String event: 5}
Event{id=null, headers=null, replyTo=null, data=String event: 6}
Event{id=null, headers=null, replyTo=null, data=String event: 9}
3rd run:
Event{id=null, headers=null, replyTo=null, data=String event: 2}
Event{id=null, headers=null, replyTo=null, data=String event: 4}
Event{id=null, headers=null, replyTo=null, data=String event: 6}
似乎我一定错过了一些非常明显的东西,因为除了这个是 javaconfig 而不是配置的注释,并且没有与外界进行任何交互之外,我看不出这与这里的示例有何不同。