我试图PUT在循环中调用rest api for request.每个电话都是一个CompletableFuture.每个api调用都返回一个类型的对象RoomTypes.RoomType
我想在不同的列表中收集响应(成功和错误响应).我如何实现这一目标?我确信我无法使用,allOf因为如果任何一个调用无法更新,它将无法获得所有结果.
如何记录每次通话的错误/异常?
public void sendRequestsAsync(Map<Integer, List> map1) {
List<CompletableFuture<Void>> completableFutures = new ArrayList<>(); //List to hold all the completable futures
List<RoomTypes.RoomType> responses = new ArrayList<>(); //List for responses
ExecutorService yourOwnExecutor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
for (Map.Entry<Integer, List> entry :map1.entrySet()) {
CompletableFuture requestCompletableFuture = CompletableFuture
.supplyAsync(
() ->
//API call which returns object of type RoomTypes.RoomType
updateService.updateRoom(51,33,759,entry.getKey(),
new RoomTypes.RoomType(entry.getKey(),map2.get(entry.getKey()),
entry.getValue())),
yourOwnExecutor
)//Supply the task you wanna run, in your case http request …Run Code Online (Sandbox Code Playgroud) 我看过类似的帖子,但我不太确定应该对我的配置进行哪些更改。我最近升级到 gradle 4.10 并开始出现一堆错误。我对 groovy 还很陌生。
描述
我有一个 gradle 文件uiTest.gradle
1. configurations {
2. uiTest
3. }
4.
5. sourceSets {
6. uiTest {
7. java.srcDir file('src/uiTest/java')
8. resources.srcDirs file('src/uiTest/resources')
9. }
10. }
11.
12. dependencies {
13. ...
14. uiTestCompile 'commons-io:commons-io:2.6'
15. }
16.
17. task uiTest(type: Test) {
18. include '**/*Test.class'
19.
20. dependsOn 'cleanUiTest'
21.
22. testClassesDir = sourceSets.uiTest.output.classesDir
23. classpath = sourceSets.uiTest.runtimeClasspath
24.
25. if (project.hasProperty('testEnv') && project.property('testEnv') != 'dev') {
26. maxParallelForks = …Run Code Online (Sandbox Code Playgroud) 我需要计算前五个素数.我想在列表理解中构建它时检查列表的长度.以下代码不起作用.
def checkPrime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
primes = []
primes = [x for x in range(2,30) if(checkPrime(x) and len(primes)<6) ]
print primes
Run Code Online (Sandbox Code Playgroud)
输出:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Run Code Online (Sandbox Code Playgroud)
为什么len(primes)<6不在这里工作.我怎样才能做到这一点?
我想分享一下,我经历了其他类似的帖子,但建议的解决方案对我不起作用,这就是我创建一个单独的线程的原因.我正在尝试使用实体框架中的异步编程加入这两个结果.我有三个方法如下:
该方法PreprocessAppointment()等待其他两种方法GetApptTask()和GetZoneTask()
public async void PreprocessAppointment()
{
var task_1 = GetApptTask();
var task_2 = GetZoneTask();
await Task.WhenAll(task_1, task_2);
}
Run Code Online (Sandbox Code Playgroud)
该方法GetApptTask()不会给出任何错误.
public async Task<IEnumerable<appointments>> GetApptTask(){
var result = db.appointments.Where(d => d.appt_client_id == 15 && d.customer_id == 68009);
return await result.ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
该方法GetZoneTask()给出以下错误.IEnumerable <zones> does not contain definition for ToListAsync().
public async Task <IEnumerable<zones>> GetZoneTask()
{
var result = db.zones.Where(d => d.zone_client_id == "15").AsEnumerable().Distinct<zones>(new zones.Comparer());
return await result.ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
我无法找出什么可能导致这个错误 …
如何使用Optional和lambda编写下面的逻辑? 我有两个列表
List success = new ArrayList <>();
List failures = new ArrayList <>();
并且有一个对象
RoomTypes值
对于这value.getErrorType()可能是空或不和value.getId()回报整数.
我想更新success,并failure与value.getId()根据列表value.getErrorType()为空.
就像是:
if(value.getErrorType().equals(NULL)){
success.add(value.getId())
}
else{
failure.add(value.getId())
}
Run Code Online (Sandbox Code Playgroud) asynchronous ×2
java ×2
java-8 ×2
async-await ×1
c# ×1
gradle ×1
gradlew ×1
if-statement ×1
lambda ×1
linq ×1
list ×1
optional ×1
python ×1
python-2.7 ×1