Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
songxinjianqwe committed Aug 24, 2018
1 parent fb919f0 commit 4c051b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @date 2018/6/10
* 线程私有!
*/
public class RPCThreadLocalContext<T> {
public class RPCThreadLocalContext {
private static final ThreadLocal<RPCThreadLocalContext> RPC_CONTEXT = new ThreadLocal() {
@Override
protected Object initialValue() {
Expand All @@ -20,15 +20,15 @@ protected Object initialValue() {
private RPCThreadLocalContext() {
}

private Future<T> future;
private Future future;
private Invoker invoker;


public static RPCThreadLocalContext getContext() {
return RPC_CONTEXT.get();
}

public Future<T> getFuture() {
public Future getFuture() {
return future;
}

Expand All @@ -40,7 +40,7 @@ public void setInvoker(Invoker invoker) {
this.invoker = invoker;
}

public void setFuture(Future<T> future) {
public void setFuture(Future future) {
this.future = future;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@
/**
* @author sinjinsong
* @date 2018/6/10
*
* 线程共享
* 不可以使用ThreadLocal来存放,因为发出请求的线程(如main)和接受响应的线程(某个eventloop)不能保证是同一个线程
*/
@Slf4j
public class RPCThreadSharedContext {
private static final ConcurrentHashMap<String, CompletableFuture<RPCResponse>> RESPONSES = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String,ServiceConfig<?>> HANDLER_MAP = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, ServiceConfig<?>> HANDLER_MAP = new ConcurrentHashMap<>();

public static void registerResponseFuture(String requestId, CompletableFuture<RPCResponse> future) {
RESPONSES.put(requestId, future);
}

public static CompletableFuture<RPCResponse> getAndRemoveResponseFuture(String requestId) {
return RESPONSES.remove(requestId);
}
public static void registerHandler(String name,ServiceConfig serviceConfig) {

public static void registerHandler(String name, ServiceConfig serviceConfig) {
HANDLER_MAP.put(name,
serviceConfig);
}

public static ServiceConfig getAndRemoveHandler(String name) {
return HANDLER_MAP.remove(name);
return HANDLER_MAP.remove(name);
}
}

0 comments on commit 4c051b8

Please sign in to comment.