bradforj287
bradforj287

Reputation: 1048

Resolving a ChannelPromise with a return value in Netty?

I have an infrequent and slow operation that sometimes occurs on my channels. When this operation is needed, it is required to stop all processing on the channel until the operation completes and the return value is inspected. My thinking is to handle this asynchrounously and turn AUTO_READ off while this operation is outstanding. I do not want to block the IO thread for this operation.

My initial thought was to have my service code resolve a ChannelPromise and pass a return value for the operation. But it appears as if there is no way to set a return value for a ChannelPromise?

Another thought is to have the service return a CompletableFuture and have my Handler code register an async callback using thenApply(). The callback code would then place the callback logic onto the channel's eventloop for thread safety.

Is there a preferred or built in mechanism to get the result of an async operation with Netty? Why does ChannelPromise provide no way to set a return value?

Upvotes: 0

Views: 438

Answers (1)

Norman Maurer
Norman Maurer

Reputation: 23567

You can just use a Promise<T> for it.

You can create one via:

Promise<ReturnType> promise = channel.eventLoop().newPromise<ReturnType>();

Upvotes: 2

Related Questions