();
28 |
29 |
30 | /**
31 | * Sets the trace.
32 | *
33 | * @param trace the new trace
34 | */
35 | public static void setTrace(Trace trace) {
36 | if (trace != null) {
37 | TRACE.set(trace);;
38 | }
39 | }
40 |
41 | /**
42 | * Gets the trace.
43 | *
44 | * @return the trace
45 | */
46 | public static Trace getTrace() {
47 | return TRACE.get();
48 | }
49 |
50 |
51 | /**
52 | * Clear trace.
53 | */
54 | public static void clearTrace() {
55 | TRACE.remove();
56 | }
57 |
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/data/Writerable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.baidu.jprotobuf.pbrpc.data;
18 |
19 |
20 | /**
21 | * A byte deserializable interface.
22 | *
23 | * @author xiemalin
24 | * @since 1.0
25 | */
26 | public interface Writerable {
27 |
28 | /**
29 | * Write.
30 | *
31 | * @return all content as byte array
32 | */
33 | byte[] write();
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/data/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * RPC protocol implements.
3 | *
4 | * @since 1.0
5 | */
6 | package com.baidu.jprotobuf.pbrpc.data;
7 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/intercept/InvokerInterceptor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baidu.jprotobuf.pbrpc.intercept;
17 |
18 | /**
19 | * RPC method invoker intercepter.
20 | *
21 | * @author xiemalin
22 | * @since 3.4.0
23 | */
24 | public interface InvokerInterceptor {
25 |
26 | Object NULL = new Object();
27 |
28 | /**
29 | * This method will call before RPC method invoke.
30 | *
31 | * @param methodInvocation the method invocation
32 | */
33 | void beforeInvoke(MethodInvocationInfo methodInvocation);
34 |
35 | /**
36 | * to do intercept action.
37 | *
38 | * @param methodInvocation the method invocation
39 | * @return if not null, this intercepter will active and this result will replace to real RPC return.
40 | * if return null will continue to another intercepter.
41 | * if method with void return type. please return NULL
to stop next chain invocation.
42 | */
43 | Object process(MethodInvocationInfo methodInvocation);
44 |
45 |
46 | /**
47 | * to call after process called. here need to clear some garbage here
48 | */
49 | void afterProcess();
50 | }
51 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/intercept/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2014 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | * 拦截器模块
18 | *
19 | * @author xiemalin
20 | * @since 3.4.0
21 | */
22 | package com.baidu.jprotobuf.pbrpc.intercept;
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/management/HttpConstants.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baidu.jprotobuf.pbrpc.management;
17 |
18 | /**
19 | * Http constant class.
20 | *
21 | * @author xiemalin
22 | * @since 3.1.0
23 | */
24 | public class HttpConstants {
25 |
26 | /** The Constant LINE_BREAK. */
27 | public static final String LINE_BREAK = "
";
28 |
29 | /** The Constant PRE_STARTS. */
30 | public static final String PRE_STARTS = "";
31 |
32 | /** The Constant PRE_ENDS. */
33 | public static final String PRE_ENDS = "
";
34 |
35 | /** The Constant HTML_HEAD. */
36 | public static final String HTML_HEAD = "";
38 |
39 | /** The Constant HTML_TAIL. */
40 | public static final String HTML_TAIL = "";
41 |
42 | /** The Constant BOLD_FONT. */
43 | public static final String BOLD_FONT = "";
44 |
45 | /** The Constant BOLD_FONT_END. */
46 | public static final String BOLD_FONT_END = "";
47 | }
48 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/management/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2014 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | * Management module for protobuf RPC.
18 | *
19 | * @author xiemalin
20 | * @since 3.1.0
21 | */
22 | package com.baidu.jprotobuf.pbrpc.management;
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/meta/RpcMetaAware.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.baidu.jprotobuf.pbrpc.meta;
18 |
19 | /**
20 | * To get Rpc service method description.
21 | *
22 | * @author xiemalin
23 | * @since 2.1
24 | */
25 | public interface RpcMetaAware {
26 |
27 | /**
28 | * Gets the input meta proto.
29 | *
30 | * @return the input meta proto
31 | */
32 | String getInputMetaProto();
33 |
34 | /**
35 | * Gets the output meta proto.
36 | *
37 | * @return the output meta proto
38 | */
39 | String getOutputMetaProto();
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/meta/RpcServiceMetaService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.baidu.jprotobuf.pbrpc.meta;
18 |
19 | import com.baidu.jprotobuf.pbrpc.ProtobufRPC;
20 |
21 | /**
22 | * {@link RpcServiceMetaService}.
23 | *
24 | * @author xiemalin
25 | * @since 2.1
26 | */
27 | public interface RpcServiceMetaService {
28 |
29 | /**
30 | * Gets the rpc service meta info.
31 | *
32 | * @return the rpc service meta info
33 | */
34 | @ProtobufRPC(serviceName = RpcServiceMetaServiceProvider.RPC_META_SERVICENAME, onceTalkTimeout = 100000)
35 | RpcServiceMetaList getRpcServiceMetaInfo();
36 |
37 | /**
38 | * Ping.
39 | */
40 | @ProtobufRPC(serviceName = RpcServiceMetaServiceProvider.RPC_META_SERVICENAME)
41 | void ping();
42 | }
43 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Protobuf RPC implements.
3 | *
4 | * @since 1.0
5 | */
6 | package com.baidu.jprotobuf.pbrpc;
7 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/registry/NotifyListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baidu.jprotobuf.pbrpc.registry;
17 |
18 | import java.util.List;
19 |
20 | /**
21 | * NotifyListener. (API, Prototype, ThreadSafe)
22 | *
23 | * @author xiemalin
24 | * @since 2.27
25 | */
26 | public interface NotifyListener {
27 |
28 | /**
29 | * 当收到服务变更通知时触发。.
30 | *
31 | * @param urls 已注册信息列表,总不为空,
32 | */
33 | void notify(List urls);
34 |
35 | }
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/registry/RegistryCenterService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baidu.jprotobuf.pbrpc.registry;
17 |
18 | import java.util.List;
19 |
20 | /**
21 | * RegistryService. (SPI, Prototype, ThreadSafe)
22 | *
23 | * @author xiemalin
24 | * @since 2.27
25 | */
26 | public interface RegistryCenterService {
27 |
28 | /**
29 | * 注册数据,比如:提供者地址,消费者地址,路由规则,覆盖规则,等数据。.
30 | *
31 | * @param url 注册信息,不允许为空
32 | */
33 | void register(RegisterInfo url);
34 |
35 | /**
36 | * 取消注册.
37 | *
38 | * @param url 注册信息,不允许为空
39 | */
40 | void unregister(RegisterInfo url);
41 |
42 | /**
43 | * 订阅符合条件的已注册数据,当有注册数据变更时自动推送.
44 | *
45 | * @param url 订阅条件,不允许为空,
46 | * @param listener 变更事件监听器,不允许为空
47 | */
48 | void subscribe(RegisterInfo url, NotifyListener listener);
49 |
50 | /**
51 | * 取消订阅.
52 | *
53 | * @param url 订阅条件,不允许为空
54 | * @param listener 变更事件监听器,不允许为空
55 | */
56 | void unsubscribe(RegisterInfo url, NotifyListener listener);
57 |
58 | /**
59 | * 查询符合条件的已注册数据,与订阅的推模式相对应,这里为拉模式,只返回一次结果。.
60 | *
61 | * @param url 查询条件,不允许为空,
62 | * @return 已注册信息列表,可能为空,
63 | * @see com.alibaba.dubbo.registry.NotifyListener#notify(List)
64 | */
65 | List lookup(RegisterInfo url);
66 |
67 | }
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/server/AbstractServiceExporter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.baidu.jprotobuf.pbrpc.server;
18 |
19 | /**
20 | * Abstract service exporter class.
21 | *
22 | * @author xiemalin
23 | * @param the generic type
24 | * @param the generic type
25 | * @since 1.0
26 | */
27 | public abstract class AbstractServiceExporter implements ServiceExporter {
28 |
29 | /** The service invoker. */
30 | private IDLServiceInvoker serviceInvoker;
31 |
32 | /**
33 | * Gets the service invoker.
34 | *
35 | * @return the service invoker
36 | */
37 | public IDLServiceInvoker getServiceInvoker() {
38 | return serviceInvoker;
39 | }
40 |
41 | /**
42 | * Sets the service invoker.
43 | *
44 | * @param serviceInvoker the new service invoker
45 | */
46 | public void setServiceInvoker(IDLServiceInvoker serviceInvoker) {
47 | this.serviceInvoker = serviceInvoker;
48 | }
49 |
50 |
51 | /** service name. */
52 | private String serviceName;
53 |
54 | /** method name. */
55 | private String methodName;
56 |
57 | /* (non-Javadoc)
58 | * @see com.baidu.jprotobuf.pbrpc.server.ServiceExporter#getServiceName()
59 | */
60 | public String getServiceName() {
61 | return serviceName;
62 | }
63 |
64 | /* (non-Javadoc)
65 | * @see com.baidu.jprotobuf.pbrpc.server.ServiceExporter#getMethodName()
66 | */
67 | public String getMethodName() {
68 | return methodName;
69 | }
70 |
71 | /**
72 | * Sets the service name.
73 | *
74 | * @param serviceName the new service name
75 | */
76 | public void setServiceName(String serviceName) {
77 | this.serviceName = serviceName;
78 | }
79 |
80 | /**
81 | * Sets the method name.
82 | *
83 | * @param methodName the new method name
84 | */
85 | public void setMethodName(String methodName) {
86 | this.methodName = methodName;
87 | }
88 |
89 |
90 | }
91 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/server/IDLServiceInvoker.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.baidu.jprotobuf.pbrpc.server;
18 |
19 | import com.baidu.bjf.remoting.protobuf.IDLProxyObject;
20 |
21 | /**
22 | * IDL service invoker.
23 | *
24 | * @author xiemalin
25 | * @since 1.0
26 | */
27 | public interface IDLServiceInvoker {
28 |
29 | /**
30 | * RPC service call back method.
31 | *
32 | * @param input
33 | * request IDL proxy object by protobuf deserialized
34 | * @param output
35 | * return back IDL proxy object to serialized
36 | * @throws Exception
37 | * in case of any exception
38 | */
39 | void invoke(IDLProxyObject input, IDLProxyObject output) throws Exception;
40 |
41 |
42 | /**
43 | * Gets the input.
44 | *
45 | * @return the input
46 | */
47 | IDLProxyObject getInput();
48 |
49 | /**
50 | * Gets the output.
51 | *
52 | * @return the output
53 | */
54 | IDLProxyObject getOutput();
55 | }
56 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/server/RpcServiceHandleContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2014 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baidu.jprotobuf.pbrpc.server;
17 |
18 | import io.netty.channel.ChannelHandlerContext;
19 |
20 | /**
21 | * The context class around RPC service method handle scope to get ChannelHandlerContext
.
22 | *
23 | * @author xiemalin
24 | * @since 4.1.3
25 | */
26 | public class RpcServiceHandleContext {
27 |
28 | /** The Constant CHANNEL_CONTEXT. */
29 | private static final ThreadLocal CHANNEL_CONTEXT = new ThreadLocal();
30 |
31 |
32 | /**
33 | * Sets the channel handler context.
34 | *
35 | * @param chc the new channel handler context
36 | */
37 | public static void setChannelHandlerContext(ChannelHandlerContext chc) {
38 | CHANNEL_CONTEXT.set(chc);
39 | }
40 |
41 |
42 | /**
43 | * Gets the channel handler context.
44 | *
45 | * @return the channel handler context
46 | */
47 | public static ChannelHandlerContext getChannelHandlerContext() {
48 | return CHANNEL_CONTEXT.get();
49 | }
50 |
51 | /**
52 | * Clear channel handler context.
53 | */
54 | public static void clearChannelHandlerContext() {
55 | CHANNEL_CONTEXT.remove();
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/server/ServiceExporter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.baidu.jprotobuf.pbrpc.server;
18 |
19 |
20 | /**
21 | * RPC service exporter interface.
22 | *
23 | * @author xiemalin
24 | * @param input parameter
25 | * @param response result
26 | */
27 | public interface ServiceExporter {
28 |
29 | /**
30 | * Gets the service name.
31 | *
32 | * @return the service name
33 | */
34 | String getServiceName();
35 |
36 | /**
37 | * Gets the method name.
38 | *
39 | * @return the method name
40 | */
41 | String getMethodName();
42 |
43 | /**
44 | * execute service action.
45 | *
46 | * @param input the input
47 | * @return the o
48 | * @throws Exception the exception
49 | */
50 | O execute(I input) throws Exception;
51 | }
52 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/server/package-info.java:
--------------------------------------------------------------------------------
1 | /**
2 | * RPC server supports.
3 | *
4 | * @since 1.0
5 | */
6 | package com.baidu.jprotobuf.pbrpc.server;
7 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/AbstractChannelPoolSharableFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
3 | */
4 | package com.baidu.jprotobuf.pbrpc.transport;
5 |
6 | import java.net.InetSocketAddress;
7 | import java.util.Map;
8 |
9 | /**
10 | * A factory for creating GlobalChannelPoolSharable objects.
11 | *
12 | * @author xiemalin
13 | * @since 3.5.20
14 | */
15 | public abstract class AbstractChannelPoolSharableFactory implements ChannelPoolSharableFactory {
16 |
17 | protected abstract Map getRpcChannelMap();
18 |
19 | /**
20 | * Gets the host address.
21 | *
22 | * @param host the host
23 | * @param port the port
24 | * @return the host address
25 | */
26 | protected String getHostAddress(String host, int port) {
27 | InetSocketAddress address;
28 | if (host == null) {
29 | address = new InetSocketAddress(port);
30 | } else {
31 | address = new InetSocketAddress(host, port);
32 | }
33 | return address.toString();
34 | }
35 |
36 | /*
37 | * (non-Javadoc)
38 | *
39 | * @see com.baidu.jprotobuf.pbrpc.transport.ChannelPoolSharableFactory#getOrCreateChannelPool(java.lang.String, int)
40 | */
41 | @Override
42 | public RpcChannel getOrCreateChannelPool(RpcClient rpcClient, String host, int port) {
43 |
44 | String hostAddress = getHostAddress(host, port);
45 | if (!getRpcChannelMap().containsKey(hostAddress)) {
46 | RpcChannel rpcChannel = new RpcChannel(rpcClient, host, port);
47 | getRpcChannelMap().put(hostAddress, rpcChannel);
48 | return rpcChannel;
49 | }
50 |
51 | return getRpcChannelMap().get(hostAddress);
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/ChannelPoolSharableFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
3 | */
4 | package com.baidu.jprotobuf.pbrpc.transport;
5 |
6 | /**
7 | * A factory for creating ChannelSharable objects.
8 | *
9 | * @author xiemalin
10 | * @since 3.5.20
11 | */
12 | public interface ChannelPoolSharableFactory {
13 |
14 | /**
15 | * Gets the or create channel pool.
16 | *
17 | * @param rpcClient the rpc client
18 | * @param host the host
19 | * @param port the port
20 | * @return the or create channel pool
21 | */
22 | RpcChannel getOrCreateChannelPool(RpcClient rpcClient, String host, int port);
23 | }
24 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/ExceptionCatcher.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baidu.jprotobuf.pbrpc.transport;
17 |
18 | /**
19 | * The Interface ExceptionCatcher.
20 | *
21 | * @author xiemalin
22 | * @since 3.5.13
23 | */
24 | public interface ExceptionCatcher {
25 |
26 | /**
27 | * On exception.
28 | *
29 | * @param e the e
30 | * @return the rpc error message
31 | */
32 | RpcErrorMessage onException(Exception e);
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/ExceptionHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.baidu.jprotobuf.pbrpc.transport;
17 |
18 | /**
19 | * The Interface ExceptionHandler.
20 | *
21 | * @author xiemalin
22 | * @since 3.5.13
23 | */
24 | public interface ExceptionHandler {
25 |
26 | /**
27 | * Handle exception. if return null
ignore the exception.
28 | *
29 | * @param error the error
30 | * @return the exception
31 | */
32 | Exception handleException(RpcErrorMessage error);
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/GlobalChannelPoolSharableFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
3 | */
4 | package com.baidu.jprotobuf.pbrpc.transport;
5 |
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | /**
10 | * A factory for creating GlobalChannelPoolSharable objects.
11 | *
12 | * @author xiemalin
13 | * @since 3.5.20
14 | */
15 | public class GlobalChannelPoolSharableFactory extends AbstractChannelPoolSharableFactory {
16 |
17 | /** The rpc channel map. */
18 | private static Map rpcChannelMap = new HashMap();
19 |
20 | protected Map getRpcChannelMap() {
21 | return rpcChannelMap;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/RpcErrorMessage.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
3 | */
4 | package com.baidu.jprotobuf.pbrpc.transport;
5 |
6 | /**
7 | * The Class RpcErrorMessage.
8 | *
9 | * @author xiemalin
10 | * @since 3.5.13
11 | */
12 | public class RpcErrorMessage {
13 |
14 | /** The error code. */
15 | private int errorCode;
16 |
17 | /** The error message. */
18 | private String errorMessage;
19 |
20 | /**
21 | * Gets the error code.
22 | *
23 | * @return the error code
24 | */
25 | public int getErrorCode() {
26 | return errorCode;
27 | }
28 |
29 | /**
30 | * Sets the error code.
31 | *
32 | * @param errorCode the new error code
33 | */
34 | public void setErrorCode(int errorCode) {
35 | this.errorCode = errorCode;
36 | }
37 |
38 | /**
39 | * Gets the error message.
40 | *
41 | * @return the error message
42 | */
43 | public String getErrorMessage() {
44 | return errorMessage;
45 | }
46 |
47 | /**
48 | * Sets the error message.
49 | *
50 | * @param errorMessage the new error message
51 | */
52 | public void setErrorMessage(String errorMessage) {
53 | this.errorMessage = errorMessage;
54 | }
55 |
56 | /**
57 | * Instantiates a new rpc error.
58 | *
59 | * @param errorCode the error code
60 | * @param errorMessage the error message
61 | */
62 | public RpcErrorMessage(int errorCode, String errorMessage) {
63 | super();
64 | this.errorCode = errorCode;
65 | this.errorMessage = errorMessage;
66 | }
67 |
68 | /* (non-Javadoc)
69 | * @see java.lang.Object#hashCode()
70 | */
71 | @Override
72 | public int hashCode() {
73 | final int prime = 31;
74 | int result = 1;
75 | result = prime * result + errorCode;
76 | return result;
77 | }
78 |
79 | /* (non-Javadoc)
80 | * @see java.lang.Object#equals(java.lang.Object)
81 | */
82 | @Override
83 | public boolean equals(Object obj) {
84 | if (this == obj)
85 | return true;
86 | if (obj == null)
87 | return false;
88 | if (getClass() != obj.getClass())
89 | return false;
90 | RpcErrorMessage other = (RpcErrorMessage) obj;
91 | if (errorCode != other.errorCode)
92 | return false;
93 | return true;
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/SimpleChannelPoolSharableFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
3 | */
4 | package com.baidu.jprotobuf.pbrpc.transport;
5 |
6 | import java.util.HashMap;
7 | import java.util.Map;
8 |
9 | /**
10 | * A factory for creating GlobalChannelPoolSharable objects.
11 | *
12 | * @author xiemalin
13 | * @since 3.5.20
14 | */
15 | public class SimpleChannelPoolSharableFactory extends AbstractChannelPoolSharableFactory {
16 |
17 | /** The rpc channel map. */
18 | private Map rpcChannelMap = new HashMap();
19 |
20 | protected Map getRpcChannelMap() {
21 | return rpcChannelMap;
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/jprotobuf-rpc-core/src/main/java/com/baidu/jprotobuf/pbrpc/transport/handler/RpcDataPackageCompressHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2002-2007 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.baidu.jprotobuf.pbrpc.transport.handler;
18 |
19 | import java.util.List;
20 |
21 | import com.baidu.jprotobuf.pbrpc.compress.Compress;
22 | import com.baidu.jprotobuf.pbrpc.compress.GZipCompress;
23 | import com.baidu.jprotobuf.pbrpc.compress.SnappyCompress;
24 | import com.baidu.jprotobuf.pbrpc.data.RpcDataPackage;
25 | import com.baidu.jprotobuf.pbrpc.data.RpcMeta;
26 |
27 | import io.netty.channel.ChannelHandler.Sharable;
28 | import io.netty.channel.ChannelHandlerContext;
29 | import io.netty.handler.codec.MessageToMessageEncoder;
30 |
31 | /**
32 | * Do data compress handler.
33 | *
34 | * @author xiemalin
35 | * @since 1.4
36 | */
37 | @Sharable
38 | public class RpcDataPackageCompressHandler extends
39 | MessageToMessageEncoder {
40 |
41 | /* (non-Javadoc)
42 | * @see io.netty.handler.codec.MessageToMessageEncoder#encode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List)
43 | */
44 | @Override
45 | protected void encode(ChannelHandlerContext ctx, RpcDataPackage msg,
46 | List