org.rx.net.rpc.RemotingContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.net.rpc;
import io.netty.util.concurrent.FastThreadLocal;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.rx.net.transport.TcpClient;
import org.rx.net.transport.TcpServer;
import org.rx.util.function.Func;
import static org.rx.core.Extends.require;
@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class RemotingContext {
static final FastThreadLocal CTX = new FastThreadLocal<>();
public static RemotingContext context() {
RemotingContext ctx = CTX.getIfExists();
require(ctx);
return ctx;
}
@SneakyThrows
static T invoke(Func fn, TcpServer rs, TcpClient rc) {
CTX.set(new RemotingContext(rs, rc));
try {
return fn.invoke();
} finally {
CTX.remove();
}
}
final TcpServer server;
final TcpClient client;
}