com.avanza.astrix.remoting.util.GsUtil Maven / Gradle / Ivy
/*
* Copyright 2014 Avanza Bank AB
*
* Licensed 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 com.avanza.astrix.remoting.util;
import java.util.ArrayList;
import java.util.List;
import org.openspaces.remoting.SpaceRemotingResult;
import com.avanza.astrix.core.AstrixRemoteResult;
import com.avanza.astrix.core.CorrelationId;
import com.avanza.astrix.core.RemoteServiceInvocationException;
import com.avanza.astrix.core.ServiceInvocationException;
import com.gigaspaces.async.AsyncFuture;
import com.gigaspaces.async.AsyncFutureListener;
import com.gigaspaces.async.AsyncResult;
import rx.Observable;
import rx.Observable.OnSubscribe;
import rx.Subscriber;
import rx.functions.Func1;
/**
* @author joasah
*/
public class GsUtil {
@Deprecated
public static List> convertToAstrixRemoteResults(SpaceRemotingResult[] results) {
List> converted = new ArrayList>(results.length);
for (SpaceRemotingResult result : results) {
if (result.getException() != null) {
converted.add(AstrixRemoteResult.failure(toRuntimeException(result.getException()), CorrelationId.undefined()));
} else {
converted.add(AstrixRemoteResult.successful(result.getResult()));
}
}
return converted;
}
private static ServiceInvocationException toRuntimeException(Throwable exception) {
if (exception instanceof RuntimeException) {
return (ServiceInvocationException) exception;
} else {
return new RemoteServiceInvocationException("Remote service threw exception: " + exception.getMessage(), exception.getClass().getName());
}
}
public static void subscribe(final AsyncFuture asyncFuture, final Subscriber super T> t1) {
asyncFuture.setListener(new AsyncFutureListener() {
@Override
public void onResult(AsyncResult result) {
if (result.getException() == null) {
t1.onNext(result.getResult());
t1.onCompleted();
} else {
t1.onError(result.getException());
}
}
});
}
public static Func1>, Observable> asyncResultListToObservable() {
return new Func1>, Observable>() {
@Override
public Observable call(final List> asyncRresults) {
return Observable.create(new OnSubscribe() {
@Override
public void call(Subscriber super T> subscriber) {
for (AsyncResult asyncInvocationResponse : asyncRresults) {
if (asyncInvocationResponse.getException() != null) {
subscriber.onError(asyncInvocationResponse.getException());
return;
}
subscriber.onNext(asyncInvocationResponse.getResult());
}
subscriber.onCompleted();
}
});
}};
}
}