|
1 | 1 | /* |
2 | | - * Copyright (c) 2016, Quancheng-ec.com All right reserved. This software is the |
3 | | - * confidential and proprietary information of Quancheng-ec.com ("Confidential |
4 | | - * Information"). You shall not disclose such Confidential Information and shall |
5 | | - * use it only in accordance with the terms of the license agreement you entered |
6 | | - * into with Quancheng-ec.com. |
| 2 | + * Copyright (c) 2016, Quancheng-ec.com All right reserved. This software is the confidential and |
| 3 | + * proprietary information of Quancheng-ec.com ("Confidential Information"). You shall not disclose |
| 4 | + * such Confidential Information and shall use it only in accordance with the terms of the license |
| 5 | + * agreement you entered into with Quancheng-ec.com. |
7 | 6 | */ |
8 | 7 | package com.quancheng.saluki.core.grpc.client; |
9 | 8 |
|
|
13 | 12 | import com.google.protobuf.Message; |
14 | 13 | import com.quancheng.saluki.core.common.Constants; |
15 | 14 | import com.quancheng.saluki.core.common.GrpcURL; |
16 | | -import com.quancheng.saluki.core.grpc.util.MethodDescriptorUtil; |
17 | | -import com.quancheng.saluki.core.grpc.util.SerializerUtils; |
| 15 | +import com.quancheng.saluki.core.grpc.util.GrpcUtil; |
| 16 | +import com.quancheng.saluki.core.grpc.util.SerializerUtil; |
18 | 17 | import com.quancheng.saluki.serializer.exception.ProtobufException; |
19 | 18 |
|
20 | 19 | import io.grpc.Channel; |
|
26 | 25 | */ |
27 | 26 | public interface GrpcRequest { |
28 | 27 |
|
29 | | - public Message getRequestArg() throws ProtobufException; |
| 28 | + public Message getRequestArg() throws ProtobufException; |
30 | 29 |
|
31 | | - public MethodDescriptor<Message, Message> getMethodDescriptor(); |
| 30 | + public MethodDescriptor<Message, Message> getMethodDescriptor(); |
32 | 31 |
|
33 | | - public Channel getChannel(); |
| 32 | + public Channel getChannel(); |
34 | 33 |
|
35 | | - public void returnChannel(Channel channel); |
| 34 | + public void returnChannel(Channel channel); |
36 | 35 |
|
37 | | - public String getServiceName(); |
| 36 | + public String getServiceName(); |
38 | 37 |
|
39 | | - public GrpcURL getRefUrl(); |
| 38 | + public GrpcURL getRefUrl(); |
40 | 39 |
|
41 | | - public MethodRequest getMethodRequest(); |
| 40 | + public MethodRequest getMethodRequest(); |
42 | 41 |
|
43 | | - public void setMethodRequest(MethodRequest methodRequest); |
| 42 | + public void setMethodRequest(MethodRequest methodRequest); |
44 | 43 |
|
45 | | - public static class Default implements GrpcRequest, Serializable { |
| 44 | + public static class Default implements GrpcRequest, Serializable { |
46 | 45 |
|
47 | | - private static final long serialVersionUID = 1L; |
| 46 | + private static final long serialVersionUID = 1L; |
48 | 47 |
|
49 | | - private final GrpcURL refUrl; |
| 48 | + private final GrpcURL refUrl; |
50 | 49 |
|
51 | | - private final GrpcProtocolClient.ChannelCall chanelPool; |
| 50 | + private final GrpcProtocolClient.ChannelCall chanelPool; |
52 | 51 |
|
53 | | - private MethodRequest methodRequest; |
| 52 | + private MethodRequest methodRequest; |
54 | 53 |
|
55 | | - public Default(GrpcURL refUrl, GrpcProtocolClient.ChannelCall chanelPool){ |
56 | | - super(); |
57 | | - this.refUrl = refUrl; |
58 | | - this.chanelPool = chanelPool; |
59 | | - } |
60 | | - |
61 | | - @Override |
62 | | - public Message getRequestArg() throws ProtobufException { |
63 | | - Object arg = this.getMethodRequest().getArg(); |
64 | | - return SerializerUtils.Pojo2Protobuf(arg); |
65 | | - } |
| 54 | + public Default(GrpcURL refUrl, GrpcProtocolClient.ChannelCall chanelPool) { |
| 55 | + super(); |
| 56 | + this.refUrl = refUrl; |
| 57 | + this.chanelPool = chanelPool; |
| 58 | + } |
66 | 59 |
|
67 | | - @Override |
68 | | - public MethodDescriptor<Message, Message> getMethodDescriptor() { |
69 | | - Message argsReq = MethodDescriptorUtil.buildDefaultInstance(this.getMethodRequest().getRequestType()); |
70 | | - Message argsRep = MethodDescriptorUtil.buildDefaultInstance(this.getMethodRequest().getResponseType()); |
71 | | - return MethodDescriptorUtil.createMethodDescriptor(this.getServiceName(), |
72 | | - this.getMethodRequest().getMethodName(), argsReq, |
73 | | - argsRep); |
74 | | - } |
| 60 | + @Override |
| 61 | + public Message getRequestArg() throws ProtobufException { |
| 62 | + Object arg = this.getMethodRequest().getArg(); |
| 63 | + return SerializerUtil.pojo2Protobuf(arg); |
| 64 | + } |
75 | 65 |
|
76 | | - @Override |
77 | | - public Channel getChannel() { |
78 | | - return chanelPool.borrowChannel(refUrl); |
79 | | - } |
| 66 | + @Override |
| 67 | + public MethodDescriptor<Message, Message> getMethodDescriptor() { |
| 68 | + Message argsReq = GrpcUtil.createDefaultInstance(this.getMethodRequest().getRequestType()); |
| 69 | + Message argsRep = GrpcUtil.createDefaultInstance(this.getMethodRequest().getResponseType()); |
| 70 | + return GrpcUtil.createMethodDescriptor(this.getServiceName(), |
| 71 | + this.getMethodRequest().getMethodName(), argsReq, argsRep); |
| 72 | + } |
80 | 73 |
|
81 | | - @Override |
82 | | - public void returnChannel(Channel channel) { |
83 | | - chanelPool.returnChannel(refUrl, channel); |
84 | | - } |
| 74 | + @Override |
| 75 | + public Channel getChannel() { |
| 76 | + return chanelPool.borrowChannel(refUrl); |
| 77 | + } |
85 | 78 |
|
86 | | - @Override |
87 | | - public String getServiceName() { |
88 | | - return refUrl.getServiceInterface(); |
89 | | - } |
| 79 | + @Override |
| 80 | + public void returnChannel(Channel channel) { |
| 81 | + chanelPool.returnChannel(refUrl, channel); |
| 82 | + } |
90 | 83 |
|
91 | | - @Override |
92 | | - public MethodRequest getMethodRequest() { |
93 | | - return methodRequest; |
94 | | - } |
| 84 | + @Override |
| 85 | + public String getServiceName() { |
| 86 | + return refUrl.getServiceInterface(); |
| 87 | + } |
95 | 88 |
|
96 | | - @Override |
97 | | - public void setMethodRequest(MethodRequest methodRequest) { |
98 | | - this.methodRequest = methodRequest; |
99 | | - } |
| 89 | + @Override |
| 90 | + public MethodRequest getMethodRequest() { |
| 91 | + return methodRequest; |
| 92 | + } |
100 | 93 |
|
101 | | - @Override |
102 | | - public GrpcURL getRefUrl() { |
103 | | - Object arg = this.methodRequest.getArg(); |
104 | | - return this.refUrl.addParameter(Constants.METHOD_KEY, this.methodRequest.getMethodName())// |
105 | | - .addParameterAndEncoded(Constants.ARG_KEY, new Gson().toJson(arg)); |
106 | | - } |
| 94 | + @Override |
| 95 | + public void setMethodRequest(MethodRequest methodRequest) { |
| 96 | + this.methodRequest = methodRequest; |
| 97 | + } |
107 | 98 |
|
| 99 | + @Override |
| 100 | + public GrpcURL getRefUrl() { |
| 101 | + Object arg = this.methodRequest.getArg(); |
| 102 | + return this.refUrl.addParameter(Constants.METHOD_KEY, this.methodRequest.getMethodName())// |
| 103 | + .addParameterAndEncoded(Constants.ARG_KEY, new Gson().toJson(arg)); |
108 | 104 | } |
109 | 105 |
|
110 | | - public static class MethodRequest implements Serializable { |
| 106 | + } |
111 | 107 |
|
112 | | - private static final long serialVersionUID = 5280935790994972153L; |
| 108 | + public static class MethodRequest implements Serializable { |
113 | 109 |
|
114 | | - private final String methodName; |
| 110 | + private static final long serialVersionUID = 5280935790994972153L; |
115 | 111 |
|
116 | | - private final Class<?> requestType; |
| 112 | + private final String methodName; |
117 | 113 |
|
118 | | - private final Class<?> responseType; |
| 114 | + private final Class<?> requestType; |
119 | 115 |
|
120 | | - private final Object arg; |
| 116 | + private final Class<?> responseType; |
121 | 117 |
|
122 | | - private final int callType; |
| 118 | + private final Object arg; |
123 | 119 |
|
124 | | - private final int callTimeout; |
| 120 | + private final int callType; |
125 | 121 |
|
126 | | - public MethodRequest(String methodName, Class<?> requestType, Class<?> responseType, Object arg, int callType, |
127 | | - int callTimeout){ |
128 | | - super(); |
129 | | - this.methodName = methodName; |
130 | | - this.requestType = requestType; |
131 | | - this.responseType = responseType; |
132 | | - this.arg = arg; |
133 | | - this.callType = callType; |
134 | | - this.callTimeout = callTimeout; |
135 | | - } |
| 122 | + private final int callTimeout; |
136 | 123 |
|
137 | | - public String getMethodName() { |
138 | | - return methodName; |
139 | | - } |
| 124 | + public MethodRequest(String methodName, Class<?> requestType, Class<?> responseType, Object arg, |
| 125 | + int callType, int callTimeout) { |
| 126 | + super(); |
| 127 | + this.methodName = methodName; |
| 128 | + this.requestType = requestType; |
| 129 | + this.responseType = responseType; |
| 130 | + this.arg = arg; |
| 131 | + this.callType = callType; |
| 132 | + this.callTimeout = callTimeout; |
| 133 | + } |
140 | 134 |
|
141 | | - public Class<?> getRequestType() { |
142 | | - return requestType; |
143 | | - } |
| 135 | + public String getMethodName() { |
| 136 | + return methodName; |
| 137 | + } |
144 | 138 |
|
145 | | - public Class<?> getResponseType() { |
146 | | - return responseType; |
147 | | - } |
| 139 | + public Class<?> getRequestType() { |
| 140 | + return requestType; |
| 141 | + } |
148 | 142 |
|
149 | | - public Object getArg() { |
150 | | - return arg; |
151 | | - } |
| 143 | + public Class<?> getResponseType() { |
| 144 | + return responseType; |
| 145 | + } |
152 | 146 |
|
153 | | - public int getCallType() { |
154 | | - return callType; |
155 | | - } |
| 147 | + public Object getArg() { |
| 148 | + return arg; |
| 149 | + } |
156 | 150 |
|
157 | | - public int getCallTimeout() { |
158 | | - return callTimeout; |
159 | | - } |
| 151 | + public int getCallType() { |
| 152 | + return callType; |
| 153 | + } |
160 | 154 |
|
| 155 | + public int getCallTimeout() { |
| 156 | + return callTimeout; |
161 | 157 | } |
| 158 | + |
| 159 | + } |
162 | 160 | } |
0 commit comments