Skip to content

Instantly share code, notes, and snippets.

@devnoo
Created June 22, 2016 14:15
Show Gist options
  • Save devnoo/7b11c7cd54db569aac5dcd14b8c6b7f3 to your computer and use it in GitHub Desktop.
Save devnoo/7b11c7cd54db569aac5dcd14b8c6b7f3 to your computer and use it in GitHub Desktop.
CommandGateway that returns observable/single instead of CommandCallback
import org.axonframework.commandhandling.CommandCallback;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.springframework.beans.factory.annotation.Autowired;
import rx.Observable;
import rx.Single;
import rx.Subscriber;
public class DefaultObservableCommandGateway implements ObservableCommandGateway {
@Autowired
private CommandGateway commandGateway;
@Override
public <R> Single<R> send(Object command) {
return Observable.create(new Observable.OnSubscribe<R>() {
@Override
public void call(Subscriber<? super R> subscriber) {
commandGateway.send(command, new CommandCallback<R>() {
@Override
public void onSuccess(R result) {
if (!subscriber.isUnsubscribed()) {
subscriber.onNext(result);
subscriber.onCompleted();
}
}
@Override
public void onFailure(Throwable cause) {
if (!subscriber.isUnsubscribed()) {
subscriber.onError(cause);
}
}
});
}
}).toSingle();
}
}
@devnoo
Copy link
Author

devnoo commented Jun 22, 2016

works great with https://github.com/jmnarloch/rxjava-spring-boot-starter
to have async request with spring controller and axon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment