Created
February 24, 2014 10:56
-
-
Save daschl/9185619 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private final Queue<Event<CouchbaseRequest>> queue = new ArrayDeque<Event<CouchbaseRequest>>(); | |
final class EventResponseDecoder extends MessageToMessageDecoder<CouchbaseResponse> { | |
@Override | |
protected void decode(ChannelHandlerContext ctx, CouchbaseResponse in, List<Object> out) throws Exception { | |
Event<CouchbaseRequest> event = queue.poll(); | |
((Deferred) event.getReplyTo()).accept(in); | |
} | |
} | |
final class EventRequestEncoder extends MessageToMessageEncoder<Event<CouchbaseRequest>> { | |
@Override | |
protected void encode(ChannelHandlerContext ctx, Event<CouchbaseRequest> msg, List<Object> out) throws Exception { | |
queue.offer(msg); | |
out.add(msg.getData()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public <R extends CouchbaseResponse> Promise<R> send(final CouchbaseRequest request) { | |
final Deferred<R,Promise<R>> deferred = Promises.defer(env.reactorEnv()); | |
if (state() == LifecycleState.CONNECTED) { | |
Event<CouchbaseRequest> event = Event.wrap(request); | |
event.setReplyTo(deferred); | |
channel.write(event).addListener(new GenericFutureListener<Future<Void>>() { | |
@Override | |
public void operationComplete(final Future<Void> future) throws Exception { | |
if (!future.isSuccess()) { | |
deferred.accept(new Exception("woops :(")); | |
} | |
} | |
}); | |
} else { | |
deferred.accept(new Exception("Not connected :(")); | |
} | |
return deferred.compose(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment