/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.ignite.internal.client; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.ignite.compute.ComputeJob; import org.apache.ignite.compute.ComputeJobAdapter; import org.apache.ignite.compute.ComputeJobResult; import org.apache.ignite.compute.ComputeJobResultPolicy; import org.apache.ignite.compute.ComputeTaskSplitAdapter; import static org.apache.ignite.compute.ComputeJobResultPolicy.FAILOVER; import static org.apache.ignite.compute.ComputeJobResultPolicy.WAIT; /** * Test task summarizes length of all strings in the arguments list. * <p> * The argument of the task is a collection of objects to calculate string length sum of. */ public class ClientTcpTask extends ComputeTaskSplitAdapter<List<String>, Integer> { /** {@inheritDoc} */ @Override protected Collection<? extends ComputeJob> split(int gridSize, List<String> list) { Collection<ComputeJobAdapter> jobs = new ArrayList<>(); if (list != null) for (final String val : list) jobs.add(new ComputeJobAdapter() { @Override public Object execute() { try { Thread.sleep(5); } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); } return val == null ? 0 : val.length(); } }); return jobs; } /** {@inheritDoc} */ @Override public Integer reduce(List<ComputeJobResult> results) { int sum = 0; for (ComputeJobResult res : results) sum += res.<Integer>getData(); return sum; } /** {@inheritDoc} */ @Override public ComputeJobResultPolicy result(ComputeJobResult res, List<ComputeJobResult> rcvd) { if (res.getException() != null) return FAILOVER; return WAIT; } }