Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Playframework 如何向F.Promise.Promise()和/或F.Promise.sequence()添加超时_Playframework_Playframework 2.0_Playframework 2.3 - Fatal编程技术网

Playframework 如何向F.Promise.Promise()和/或F.Promise.sequence()添加超时

Playframework 如何向F.Promise.Promise()和/或F.Promise.sequence()添加超时,playframework,playframework-2.0,playframework-2.3,Playframework,Playframework 2.0,Playframework 2.3,这是我的代码,它有时会超时,因为谷歌有时需要超过1000毫秒才能返回。我可能遗漏了一些非常简单的东西,但找不到任何关于如何做到这一点的像样文档。非常感谢任何帮助 final List<F.Promise<List<Event>>> promises = new ArrayList<F.Promise<List<Event>>>(); F.Promise<List<Event>> pri

这是我的代码,它有时会超时,因为谷歌有时需要超过1000毫秒才能返回。我可能遗漏了一些非常简单的东西,但找不到任何关于如何做到这一点的像样文档。非常感谢任何帮助

    final List<F.Promise<List<Event>>> promises = new ArrayList<F.Promise<List<Event>>>();
    F.Promise<List<Event>> primaryCalendar = F.Promise.promise(() -> getEvents(staff,startDate,endDate,"primary",credential));
    promises.add(primaryCalendar);

    for (ExtraCalendar extraCalendar : staff.extraCalendars) {
        promises.add(F.Promise.promise(() -> getEvents(staff, startDate, endDate, extraCalendar.googleCalendarId, credential)));
    }
    F.Promise<List<List<Event>>> events = F.Promise.sequence(promises);
    return events;
getEvents代码是按要求提供的,但我认为它与此无关—这不是一个承诺

    public static List<Event> getEvents(Staff staff, Date startDate, Date endDate, String googleCalendarId, Credential credential) throws GeneralSecurityException, IOException, NotAuthorizedException {
        com.google.api.services.calendar.Calendar googleClient;
        googleClient = getGoogleClient(credential);
        try {
            if ( Cache.get(getColorCacheKey(staff)) == null ) {
                Colors colors = googleClient.colors().get().execute();
                Cache.set(getColorCacheKey(staff), colors);
            }
            Events events = null;
            DateTime start = new DateTime(startDate);
            DateTime end = new DateTime(endDate);
            // Description might be useful, but it could be big..

            events = googleClient.events().list(googleCalendarId).
                    setFields("items(colorId,summary,end,id,location,start,htmlLink),summary,timeZone").
                    setSingleEvents(true).
                    setTimeMin(start).setTimeMax(end).
                    setMaxResults(1000). // Default 250, max 2500
                    setOrderBy("startTime").
                    execute();
            if ( events.getNextPageToken() != null ) { // This means there are more..  Ignoring for now?
                Logger.warn("events.getNextPageToken() = " + events.getNextPageToken());
            }
            return events.getItems();
        } catch (TokenResponseException | GoogleJsonResponseException e) { // Can happen because they de-authorised?  Or haven't used the site in ages?
            // TODO : Check the exception to see if I can confirm it's really an authError.
            throw new NotAuthorizedException(e);
        }
    }

感谢Megazord在play google group上的回答。注意,上述注释中提到的Java文档中没有这一点,但Scala文档中也有类似的内容

因为剧本使用Akka,你也可以使用它,顺便说一句,大多数时候你需要使用期货和演员,Akka医生会为你提供更好的服务。下面是一个简单的示例,说明如何在Java中执行此操作:

import play.libs.Akka;
import akka.dispatch.Futures;
import akka.dispatch.MessageDispatcher;
import scala.concurrent.Future;
import scala.concurrent.ExecutionContext;

Future<User> future1 = null;
Future<User> future2 = null;
Future<User> future3 = null;
List<Future<User>> futures = Arrays.asList(future1, future2, future3);

ExecutionContext executor = Akka.system().dispatchers().lookup("your-dispacher-name");
// Or to get the default dispacher
// ExecutionContext executor = Akka.system().dispatcher();

Futures.firstCompletedOf(futures, executor).map( ... );

你能展示getEvents代码吗?现在在Salem上面。但我认为这并不相关,因为它本身并不是一个承诺,只是在调用方法中包含了一个承诺。我真的只是想知道如何设置承诺的超时时间。F.Promise.Promise和F.Promise.timeout都是返回承诺的静态调用,因此我不知道如何将它们混合使用?进一步思考,我想我的问题可以简化为如何在返回承诺的控制器方法上设置超时,而在另一个地方,Java文档似乎不如Scala文档。。表演如何做,但观众不知道。如何使用Java完成Future.firstCompleted?为什么,为什么java文档如此糟糕:
import play.libs.Akka;
import akka.dispatch.Futures;
import akka.dispatch.MessageDispatcher;
import scala.concurrent.Future;
import scala.concurrent.ExecutionContext;

Future<User> future1 = null;
Future<User> future2 = null;
Future<User> future3 = null;
List<Future<User>> futures = Arrays.asList(future1, future2, future3);

ExecutionContext executor = Akka.system().dispatchers().lookup("your-dispacher-name");
// Or to get the default dispacher
// ExecutionContext executor = Akka.system().dispatcher();

Futures.firstCompletedOf(futures, executor).map( ... );