在java中将听众转变为未来 [英] Turning a listener into a future in java
问题描述
我正在尝试将侦听器转换为Future,用于异步连接。我不习惯使用java期货,我有一些javascript承诺的经验,但我没有看到如何在java中编写它(我在Java 8中看到CompletableFuture可能解决了我的问题,不幸的是我是坚持java 7)。以下是我到目前为止所做的事情:
I'm trying to turn a listener into a Future, for asynchronous connection. I'm not used to using java futures yet, I've some experience with javascript promises but I fail to see how to write it in java (I've seen "CompletableFuture" in Java 8 may solve my problem, unfortunately I'm stuck with java 7). Here's what I've done so far:
public Future<Boolean> checkEmailClientConfiguration(final EmailClientConfiguration config) {
final Future<Boolean> future = ???;
// In some other languages I would create a deferred
Transport transport = null;
try {
transport = session.getTransport("smtp");
transport.addConnectionListener(new ConnectionListener() {
@Override
public void opened(ConnectionEvent connectionEvent) {
System.out.println("!!!opened!!! ; connected=" + ((SMTPTransport) connectionEvent.getSource()).isConnected());
// HERE I would like to make my future "resolved"
}
@Override
public void disconnected(ConnectionEvent connectionEvent) {
}
@Override
public void closed(ConnectionEvent connectionEvent) {
}
});
transport.connect(config.getMailSMTPHost(),
config.getMailSMTPPort(),
config.getMailUsername(),
config.getMailPassword());
return future;
} catch (final MessagingException e) {
throw e;
} finally{
if(transport != null){
transport.close();
}
}
}
我找不到任何简单的方法。到目前为止,我发现的唯一解决方案是扩展FutureTask并在Callable运行结束时等待/休眠,直到某个状态变量设置为已解决。我真的不喜欢在我的业务代码中等待/休眠的想法,可能存在一些已经存在的东西使其延迟? (在java中,还是像Apache commons或guava这样的流行库?)
I can't find any easy way to do it. The only solution I've found so far is to extend FutureTask and at the end of the Callable run, wait/sleep until some state variable is set as resolved. I don't really like the idea of waiting/sleeping in my business code, there is probably something that already exist to make it deferred? (in java, or popular libraries such as Apache commons or guava?)
推荐答案
我终于得到了同事的回答。我正在寻找的是Guava:SettableFuture。以下是代码的样子:
I finally got my answer from a colleague. What I'm looking for exists in Guava: SettableFuture. Here's how the code looks like:
final SettableFuture<Boolean> future = SettableFuture.create();
Transport transport = null;
try {
transport = session.getTransport("smtp");
transport.addConnectionListener(new ConnectionListener() {
@Override
public void opened(ConnectionEvent connectionEvent) {
future.set(((SMTPTransport) connectionEvent.getSource()).isConnected());
}
@Override
public void disconnected(ConnectionEvent connectionEvent) {
}
@Override
public void closed(ConnectionEvent connectionEvent) {
}
});
transport.connect(config.getMailSMTPHost(),
config.getMailSMTPPort(),
config.getMailUsername(),
config.getMailPassword());
} catch (final MessagingException e) {
future.setException(e);
} finally{
if(transport != null){
transport.close();
}
}
return future;
这篇关于在java中将听众转变为未来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!