本文整理汇总了Java中javaslang.control.Try.of方法的典型用法代码示例。如果您正苦于以下问题:Java Try.of方法的具体用法?Java Try.of怎么用?Java Try.of使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javaslang.control.Try
的用法示例。
在下文中一共展示了Try.of方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: receive
import javaslang.control.Try; //导入方法依赖的package包/类
@Override
public Try<Boolean> receive(Event event) {
requireNonNull(event, "event must be defined.");
return Try.of(() -> {
long timeoutMillis = DURATION * 60000;
Future<Object> future = Patterns.ask(endpoint, new AbstractEventEndpointActor.EventFromEventBusWrapper(event), timeoutMillis);
Object result = Await.result(future, scala.concurrent.duration.Duration.create(DURATION, TimeUnit.MINUTES));
if (LOGGER.isDebugEnabled() && AbstractEventEndpointActor.NO_PROCESSED.equals(result)) {
LOGGER.debug("Following event ignored, drop it from queue:\n{}", Event.convertToPrettyJson(event));
}
if (result instanceof Throwable) {
throw new RuntimeException("Unable to process following event:\n"+ Event.convertToPrettyJson(event), (Throwable) result);
}
return result != null;
});
}
示例2: function
import javaslang.control.Try; //导入方法依赖的package包/类
private Try<String> function (ExecutorService executor, Boolean flag){
return Try.of(() -> {
if(flag) {
Thread.sleep(4000);
return "Good night";
}
else
return "Good morning";
});
}
示例3: canJobRun
import javaslang.control.Try; //导入方法依赖的package包/类
public static Try<Boolean> canJobRun(JobRunOptions options, Class<?> jobClass, int quitePeriod, Logger LOG) {
return Try.of(() -> {
int lastPackage = jobClass.getCanonicalName().lastIndexOf(".");
String className = jobClass.getCanonicalName().substring(lastPackage+1, jobClass.getCanonicalName().length());
Pair<JobStatus, String> jobRunning = DataFlowUtils.isJobRunning(options, className.toLowerCase(), quitePeriod);
if (jobRunning.getKey() != DataFlowUtils.JobStatus.Nothing) {
LOG.warn(LogUtils.prefixLog("job {} {} [{}min QP - {}]"), className,jobRunning.getKey(),options.getQuitePeriod(),jobRunning.getValue());
return false;
}
return true;
});
}
示例4: invoke
import javaslang.control.Try; //导入方法依赖的package包/类
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
final String operation = method.getName();
final Instant t0 = time.get();
final Try<?> result = Try.of(() -> {
try {
return method.invoke(delegate, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
}
});
final long durationMillis = t0.until(time.get(), ChronoUnit.MILLIS);
// TODO: Instrument at a lower level to catch errors with greater granularity. At this level we might not see
// all errors as the implementation might swallow exceptions, perform retries, etc.
result.onFailure(error -> {
if (delegate instanceof DockerRunner) {
reportDockerOperationError(operation, durationMillis, error);
}
});
final String status = (result.isSuccess()) ? "success" : "failure";
if (delegate instanceof Storage) {
stats.recordStorageOperation(operation, durationMillis, status);
} else if (delegate instanceof DockerRunner) {
stats.recordDockerOperation(operation, durationMillis, status);
}
// Propagate failure
if (result.isFailure()) {
throw result.getCause();
}
// Return successful result
return result.get();
}
示例5: parseNumber
import javaslang.control.Try; //导入方法依赖的package包/类
@Nonnull
public static <N extends Number> Try<N> parseNumber(
@Nullable final String numberAsString, @Nonnull final Function<String, N> transformation) {
return Try.of(() -> F.trimToOptional(numberAsString).map(transformation).orElse(null));
}
示例6:
import javaslang.control.Try; //导入方法依赖的package包/类
public static Try<Integer>divide(Integer dividend, Integer divisor)
{
return Try.of(() -> dividend/divisor);
}
示例7: apply
import javaslang.control.Try; //导入方法依赖的package包/类
public Try<CreditCard> apply(){
return Try.of(()-> cc.pay(amount));
}