├── .middleware-common ├── intro.png ├── invoke_types.png ├── msg_protocol.png ├── check_format.sh └── pmd_rule_set.xml ├── src ├── test │ ├── resources │ │ ├── bolt.cer │ │ ├── bolt.pfx │ │ ├── cbolt.cer │ │ ├── cbolt.pfx │ │ ├── log4j2.xml │ │ └── README.md │ └── java │ │ └── com │ │ └── alipay │ │ └── remoting │ │ ├── simpledemo │ │ ├── SimpleResponse.java │ │ ├── SimpleRequest.java │ │ ├── SimpleUserProcessor.java │ │ ├── QuickStartServerAndClient.java │ │ └── QuickStartServerAndClientWithIpv6.java │ │ ├── util │ │ └── ThreadTestUtils.java │ │ ├── benchmark │ │ ├── Request.java │ │ ├── BenchmarkUserProcessor.java │ │ └── BenchmarkServer.java │ │ ├── rpc │ │ ├── userprocessor │ │ │ └── multiinterestprocessor │ │ │ │ └── MultiInterestBaseRequestBody.java │ │ ├── common │ │ │ ├── DISCONNECTEventProcessor.java │ │ │ ├── FifoServerUserProcessor.java │ │ │ └── PortScan.java │ │ ├── heartbeat │ │ │ └── CustomHeartBeatProcessor.java │ │ ├── RpcCommandTest.java │ │ ├── addressargs │ │ │ └── RpcAddressParser_SOFTREF_Test.java │ │ ├── LifeCycleTest.java │ │ └── RpcConfigManagerTest.java │ │ ├── ProcessorManagerTest.java │ │ ├── RemotingServerTest.java │ │ ├── ConnectionEventListenerTest.java │ │ └── serialization │ │ └── HessianSerializerTest.java └── main │ └── java │ └── com │ └── alipay │ └── remoting │ ├── ClientConnectionManager.java │ ├── ServerConnectionManager.java │ ├── Scannable.java │ ├── LifeCycle.java │ ├── ConnectionEventType.java │ ├── LifeCycleException.java │ ├── HeartbeatTrigger.java │ ├── config │ ├── configs │ │ ├── ConfigType.java │ │ └── ConfigItem.java │ ├── switches │ │ ├── Switch.java │ │ └── GlobalSwitch.java │ ├── Configuration.java │ ├── BoltOption.java │ └── BoltOptions.java │ ├── ConnectionEventProcessor.java │ ├── util │ ├── IoUtils.java │ ├── FutureTaskNotCompleted.java │ ├── IDGenerator.java │ ├── FutureTaskNotRunYetException.java │ ├── RunStateRecordedFutureTask.java │ ├── TraceLogUtil.java │ ├── CrcUtil.java │ └── ThreadLocalArriveTimeHolder.java │ ├── ConnectionSelectStrategy.java │ ├── CommandCode.java │ ├── rpc │ ├── RpcCommandType.java │ ├── protocol │ │ ├── RpcProtocolManager.java │ │ ├── MultiInterestUserProcessor.java │ │ ├── AbstractMultiInterestUserProcessor.java │ │ ├── RpcCommandCode.java │ │ ├── RpcProtocolDecoder.java │ │ ├── RpcDeserializeLevel.java │ │ ├── SyncUserProcessor.java │ │ ├── AsyncUserProcessor.java │ │ ├── AsyncMultiInterestUserProcessor.java │ │ └── SyncMultiInterestUserProcessor.java │ ├── HeartbeatCommand.java │ ├── HeartbeatAckCommand.java │ ├── exception │ │ ├── InvokeException.java │ │ ├── RpcServerException.java │ │ ├── InvokeServerException.java │ │ ├── InvokeSendFailedException.java │ │ ├── InvokeServerBusyException.java │ │ └── InvokeTimeoutException.java │ ├── RpcConnectionFactory.java │ ├── RpcCodec.java │ ├── RpcConnectionEventHandler.java │ ├── HeartbeatHandler.java │ └── RequestCommand.java │ ├── codec │ └── Codec.java │ ├── InvokeCallbackListener.java │ ├── DefaultServerConnectionManager.java │ ├── constant │ └── Constants.java │ ├── CommandDecoder.java │ ├── RejectedExecutionPolicy.java │ ├── CommandEncoder.java │ ├── AsyncContext.java │ ├── connection │ └── DefaultConnectionFactory.java │ ├── Reconnector.java │ ├── InvokeCallback.java │ ├── ExtendedNettyChannelHandler.java │ ├── ConnectionHeartbeatManager.java │ ├── exception │ ├── ConnectionClosedException.java │ ├── RemotingException.java │ ├── CodecException.java │ ├── DeserializationException.java │ └── SerializationException.java │ ├── serialization │ └── Serializer.java │ ├── RejectionProcessableInvokeCallback.java │ ├── CommonCommandCode.java │ ├── RemotingProcessor.java │ ├── Protocol.java │ ├── CommandHandler.java │ ├── ConnectionMonitorStrategy.java │ ├── TimerHolder.java │ ├── AbstractLifeCycle.java │ ├── DefaultClientConnectionManager.java │ ├── RemotingAddressParser.java │ ├── ProtocolManager.java │ ├── ProtocolCode.java │ ├── ServerIdleHandler.java │ └── BizContext.java ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── .gitignore ├── .github ├── ISSUE_TEMPLATE │ ├── ask-question.md │ ├── bug_report.md │ └── feature_request.md └── workflows │ ├── maven.yml │ └── release.yml ├── SECURITY.md ├── HEADER ├── .travis.yml ├── plugins └── wireshark │ ├── install.sh │ ├── install.bat │ └── README.md ├── assembly └── assembly.xml └── CONTRIBUTING.md /.middleware-common/intro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofastack/sofa-bolt/HEAD/.middleware-common/intro.png -------------------------------------------------------------------------------- /src/test/resources/bolt.cer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofastack/sofa-bolt/HEAD/src/test/resources/bolt.cer -------------------------------------------------------------------------------- /src/test/resources/bolt.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofastack/sofa-bolt/HEAD/src/test/resources/bolt.pfx -------------------------------------------------------------------------------- /src/test/resources/cbolt.cer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofastack/sofa-bolt/HEAD/src/test/resources/cbolt.cer -------------------------------------------------------------------------------- /src/test/resources/cbolt.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofastack/sofa-bolt/HEAD/src/test/resources/cbolt.pfx -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofastack/sofa-bolt/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.middleware-common/invoke_types.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofastack/sofa-bolt/HEAD/.middleware-common/invoke_types.png -------------------------------------------------------------------------------- /.middleware-common/msg_protocol.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sofastack/sofa-bolt/HEAD/.middleware-common/msg_protocol.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | bin 3 | bak 4 | .pmd 5 | .project 6 | .settings 7 | .classpath 8 | .idea.xml 9 | .idea 10 | *.class 11 | *.bak 12 | *.iml 13 | *.ipr 14 | *.iws 15 | bak 16 | null/ 17 | tree.log 18 | tmp/ 19 | velocity.log 20 | coverage-report 21 | test-output 22 | *.txt 23 | .DS_Store 24 | -------------------------------------------------------------------------------- /.middleware-common/check_format.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # make sure git has no un commit files 4 | if [ -n "$(git status --untracked-files=no --porcelain)" ]; then 5 | echo "Please commit your change before run this shell, un commit files:" 6 | git status --untracked-files=no --porcelain 7 | exit -1 8 | fi -------------------------------------------------------------------------------- /src/test/resources/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/ask-question.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Ask Question 3 | about: Describe this issue template's purpose here. 4 | 5 | --- 6 | 7 | ### Your question 8 | 9 | describe your question clearly 10 | 11 | ### Your scenes 12 | 13 | describe your use scenes (why need this feature) 14 | 15 | ### Your advice 16 | 17 | describe the advice or solution you'd like 18 | 19 | ### Environment 20 | 21 | - SOFABolt version: 22 | - JVM version (e.g. `java -version`): 23 | - OS version (e.g. `uname -a`): 24 | - Maven version: 25 | - IDE version: 26 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | **Describe the bug** 8 | A clear and concise description of what the bug is. 9 | 10 | ### Expected behavior 11 | 12 | ### Actual behavior 13 | 14 | ### Steps to reproduce 15 | 16 | ### Minimal yet complete reproducer code (or GitHub URL to code) 17 | 18 | ### Environment 19 | 20 | - SOFABolt version: 21 | - JVM version (e.g. `java -version`): 22 | - OS version (e.g. `uname -a`): 23 | - Maven version: 24 | - IDE version: 25 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Reporting a Vulnerability 4 | 5 | If you have apprehensions regarding SOFAStack's security or you discover vulnerability or potential threat, don’t hesitate to get in touch with us by dropping a mail at sofastack@antgroup.com. 6 | 7 | In the mail, specify the description of the issue or potential threat. You are also urged to recommend the way to reproduce and replicate the issue. The SOFAStack community will get back to you after assessing and analysing the findings. 8 | 9 | PLEASE PAY ATTENTION to report the security issue on the security email before disclosing it on public domain. 10 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | 5 | --- 6 | 7 | **Is your feature request related to a problem? Please describe.** 8 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 9 | 10 | **Describe the solution you'd like** 11 | A clear and concise description of what you want to happen. 12 | 13 | **Describe alternatives you've considered** 14 | A clear and concise description of any alternative solutions or features you've considered. 15 | 16 | **Additional context** 17 | Add any other context or screenshots about the feature request here. 18 | -------------------------------------------------------------------------------- /HEADER: -------------------------------------------------------------------------------- 1 | Licensed to the Apache Software Foundation (ASF) under one or more 2 | contributor license agreements. See the NOTICE file distributed with 3 | this work for additional information regarding copyright ownership. 4 | The ASF licenses this file to You under the Apache License, Version 2.0 5 | (the "License"); you may not use this file except in compliance with 6 | the License. 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. -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | #.travis.yml 2 | language: java 3 | 4 | dist: trusty 5 | 6 | jdk: 7 | - oraclejdk8 8 | - openjdk7 9 | 10 | dist: trusty 11 | 12 | before_install: 13 | - echo "Downloading Maven 3.2.5" 14 | && wget https://archive.apache.org/dist/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.zip 15 | && unzip -q apache-maven-3.2.5-bin.zip 16 | && export M2_HOME=$PWD/apache-maven-3.2.5 17 | && export PATH=$M2_HOME/bin:$PATH 18 | && cp ./.middleware-common/.travis.settings.xml $HOME/.m2/settings.xml 19 | && mvn -version 20 | 21 | install: 22 | - mvn clean install -DskipTests=true -Dmaven.javadoc.skip=true -B -V 23 | 24 | script: 25 | - sh ./.middleware-common/check_format.sh 26 | - mvn cobertura:cobertura 27 | 28 | after_success: 29 | - bash <(curl -s https://codecov.io/bash) 30 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one or more 3 | # contributor license agreements. See the NOTICE file distributed with 4 | # this work for additional information regarding copyright ownership. 5 | # The ASF licenses this file to You under the Apache License, Version 2.0 6 | # (the "License"); you may not use this file except in compliance with 7 | # the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip 19 | -------------------------------------------------------------------------------- /plugins/wireshark/install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # 3 | # Licensed to the Apache Software Foundation (ASF) under one or more 4 | # contributor license agreements. See the NOTICE file distributed with 5 | # this work for additional information regarding copyright ownership. 6 | # The ASF licenses this file to You under the Apache License, Version 2.0 7 | # (the "License"); you may not use this file except in compliance with 8 | # the License. You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | 20 | plugin_dir=~/.config/wireshark/plugins 21 | script_dir=`dirname $0` 22 | mkdir -p ${plugin_dir} 23 | cp $script_dir/bolt.lua ${plugin_dir} 24 | echo "Wireshark bolt protocol extension has been installed successfully, please restart Wireshark before using it." -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ClientConnectionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Connection manager in client side. 21 | * 22 | * @author chengyi (mark.lx@antfin.com) 2019-03-07 12:12 23 | */ 24 | public interface ClientConnectionManager extends ConnectionManager, LifeCycle { 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ServerConnectionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Connection manager in server side. 21 | * 22 | * @author chengyi (mark.lx@antfin.com) 2019-03-07 12:13 23 | */ 24 | public interface ServerConnectionManager extends ConnectionManager, LifeCycle { 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/Scannable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Can be scanned. 21 | * 22 | * @author jiangping 23 | * @version $Id: Scannable.java, v 0.1 Mar 4, 2016 4:46:41 PM tao Exp $ 24 | */ 25 | public interface Scannable { 26 | /** 27 | * Scan it. 28 | */ 29 | void scan(); 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/LifeCycle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * @author chengyi (mark.lx@antfin.com) 2018-11-05 14:27 21 | */ 22 | public interface LifeCycle { 23 | 24 | void startup() throws LifeCycleException; 25 | 26 | void shutdown() throws LifeCycleException; 27 | 28 | boolean isStarted(); 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ConnectionEventType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Event triggered by connection state. 21 | * 22 | * @author jiangping 23 | * @version $Id: ConnectionEventType.java, v 0.1 Mar 4, 2016 8:03:27 PM tao Exp $ 24 | */ 25 | public enum ConnectionEventType { 26 | CONNECT, CONNECT_FAILED, CLOSE, EXCEPTION; 27 | } 28 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/simpledemo/SimpleResponse.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.simpledemo; 18 | 19 | import java.io.Serializable; 20 | 21 | public class SimpleResponse implements Serializable { 22 | private int res; 23 | 24 | public SimpleResponse(int res) { 25 | this.res = res; 26 | } 27 | 28 | public int getRes() { 29 | return res; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/simpledemo/SimpleRequest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.simpledemo; 18 | 19 | import java.io.Serializable; 20 | 21 | public class SimpleRequest implements Serializable { 22 | 23 | private final int num; 24 | 25 | public SimpleRequest(int num) { 26 | this.num = num; 27 | } 28 | 29 | public int getNum() { 30 | return num; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/LifeCycleException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * @author chengyi (mark.lx@antfin.com) 2018-11-05 14:42 21 | */ 22 | public class LifeCycleException extends RuntimeException { 23 | 24 | private static final long serialVersionUID = -5581833793111988391L; 25 | 26 | public LifeCycleException(String message) { 27 | super(message); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/HeartbeatTrigger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import io.netty.channel.ChannelHandlerContext; 20 | 21 | /** 22 | * Heartbeat triggers here. 23 | * 24 | * @author jiangping 25 | * @version $Id: HeartbeatTrigger.java, v 0.1 2015-12-14 PM3:40:38 tao Exp $ 26 | */ 27 | public interface HeartbeatTrigger { 28 | void heartbeatTriggered(final ChannelHandlerContext ctx) throws Exception; 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/config/configs/ConfigType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.config.configs; 18 | 19 | /** 20 | * type of config 21 | * 22 | * @author tsui 23 | * @version $Id: ConfigType.java, v 0.1 2018-07-28 17:41 tsui Exp $$ 24 | */ 25 | @Deprecated 26 | public enum ConfigType { 27 | CLIENT_SIDE, // configs of this type can only be used in client side 28 | SERVER_SIDE // configs of this type can only be used in server side 29 | } -------------------------------------------------------------------------------- /plugins/wireshark/install.bat: -------------------------------------------------------------------------------- 1 | @REM 2 | @REM Licensed to the Apache Software Foundation (ASF) under one or more 3 | @REM contributor license agreements. See the NOTICE file distributed with 4 | @REM this work for additional information regarding copyright ownership. 5 | @REM The ASF licenses this file to You under the Apache License, Version 2.0 6 | @REM (the "License"); you may not use this file except in compliance with 7 | @REM the License. You may obtain a copy of the License at 8 | @REM 9 | @REM http://www.apache.org/licenses/LICENSE-2.0 10 | @REM 11 | @REM Unless required by applicable law or agreed to in writing, software 12 | @REM distributed under the License is distributed on an "AS IS" BASIS, 13 | @REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | @REM See the License for the specific language governing permissions and 15 | @REM limitations under the License. 16 | @REM 17 | 18 | @echo off 19 | :: Get personal plugin folder from https://www.wireshark.org/docs/wsug_html_chunked/ChPluginFolders.html 20 | SET plugin_dir=%APPDATA%\Wireshark\plugins 21 | SET script_dir=%cd% 22 | if not exist %plugin_dir% mkdir %plugin_dir% 23 | copy %script_dir%\bolt.lua %plugin_dir% 24 | echo Wireshark bolt protocol extension has been installed successfully, please restart Wireshark before using it. -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/util/ThreadTestUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | /**
 20 | * utils of thread operations 21 | * 22 | * @author tsui 
 23 | * @version $Id: ThreadTestUtils.java, v 0.1 2018-05-29 15:29 tsui Exp $$
 24 | */ 25 | public class ThreadTestUtils { 26 | public static void sleep(long duration) { 27 | try { 28 | Thread.sleep(duration); 29 | } catch (InterruptedException e) { 30 | //ignore 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/test/resources/README.md: -------------------------------------------------------------------------------- 1 | The folder contains resource files for test cases: 2 | * bolt.pfx: the server's keystore file. 3 | * cbolt.pfx: the client's keystore file. 4 | * bolt.cer: the server's exported certificate file. 5 | 6 | They are generated by following steps. 7 | 8 | First, generate server keystore and export it's certificate file: 9 | 10 | ```sh 11 | keytool -genkey -alias securebolt -keysize 2048 -validity 365 -keyalg RSA -dname "CN=localhost" -keypass sfbolt -storepass sfbolt -keystore bolt.pfx -deststoretype pkcs12 12 | 13 | keytool -export -alias securebolt -keystore bolt.pfx -storepass sfbolt -file bolt.cer 14 | ``` 15 | 16 | Then, generate client keystore and export it's certificate file: 17 | 18 | ```sh 19 | keytool -genkey -alias smcc -keysize 2048 -validity 365 -keyalg RSA -dname "CN=localhost" -keypass sfbolt -storepass sfbolt -keystore cbolt.pfx -deststoretype pkcs12 20 | 21 | keytool -export -alias smcc -keystore cbolt.pfx -storepass sfbolt -file cbolt.cer 22 | ``` 23 | 24 | Finally, import server's certificate into client's keystore, and import the client's certificate into the server's keystore: 25 | 26 | ```sh 27 | keytool -import -trustcacerts -alias securebolt -file bolt.cer -storepass sfbolt -keystore cbolt.pfx 28 | 29 | keytool -import -trustcacerts -alias smcc -file cbolt.cer -storepass sfbolt -keystore bolt.pfx 30 | ``` 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ConnectionEventProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Process connection events. 21 | * @author jiangping 22 | * @version $Id: ConnectionEventProcessor.java, v 0.1 Mar 5, 2016 11:01:07 AM tao Exp $ 23 | */ 24 | public interface ConnectionEventProcessor { 25 | /** 26 | * Process event.
27 | * 28 | * @param remoteAddress remoting connection 29 | * @param connection Connection 30 | */ 31 | void onEvent(String remoteAddress, Connection connection); 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/util/IoUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | import java.io.Closeable; 20 | import java.io.IOException; 21 | 22 | /** 23 | * IO Utilities 24 | * 25 | * @author boyan(boyan@antfin.com) 26 | * 27 | */ 28 | public class IoUtils { 29 | public static void closeQuietly(Closeable closeable) { 30 | try { 31 | if (closeable != null) { 32 | closeable.close(); 33 | } 34 | } catch (IOException e) { // NOPMD 35 | // ignore 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/benchmark/Request.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.benchmark; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * 23 | * @author jiachun.fjc 24 | */ 25 | public class Request implements Serializable { 26 | 27 | private static final long serialVersionUID = -1L; 28 | 29 | public Request(T data) { 30 | this.data = data; 31 | } 32 | 33 | private T data; 34 | 35 | public T getData() { 36 | return data; 37 | } 38 | 39 | public void setData(T data) { 40 | this.data = data; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ConnectionSelectStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | * Select strategy from connection pool 23 | * 24 | * @author xiaomin.cxm 25 | * @version $Id: ConnectionSelectStrategy.java, v 0.1 Mar 14, 2016 11:06:57 AM xiaomin.cxm Exp $ 26 | */ 27 | public interface ConnectionSelectStrategy { 28 | /** 29 | * select strategy 30 | * 31 | * @param connections source connections 32 | * @return selected connection 33 | */ 34 | Connection select(List connections); 35 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/CommandCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Remoting command code stands for a specific remoting command, and every kind of command has its own code. 21 | * 22 | * @author jiangping 23 | * @version $Id: CommandCode.java, v 0.1 2015-9-7 PM7:10:18 tao Exp $ 24 | */ 25 | public interface CommandCode { 26 | // value 0 is occupied by heartbeat, don't use value 0 for other commands 27 | short HEARTBEAT_VALUE = 0; 28 | 29 | /** 30 | * 31 | * @return the short value of the code 32 | */ 33 | short value(); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/RpcCommandType.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | /** 20 | * The type of command in the request/response model. 21 | * 22 | * @author jiangping 23 | * @version $Id: RpcCommandType.java, v 0.1 2015-9-25 AM10:58:16 tao Exp $ 24 | */ 25 | public class RpcCommandType { 26 | /** rpc response */ 27 | public static final byte RESPONSE = (byte) 0x00; 28 | /** rpc request */ 29 | public static final byte REQUEST = (byte) 0x01; 30 | /** rpc oneway request */ 31 | public static final byte REQUEST_ONEWAY = (byte) 0x02; 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/codec/Codec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.codec; 18 | 19 | import io.netty.channel.ChannelHandler; 20 | 21 | /** 22 | * Codec interface. 23 | * 24 | * @author chengyi (mark.lx@antfin.com) 2018-06-20 21:07 25 | */ 26 | public interface Codec { 27 | 28 | /** 29 | * Create an encoder instance. 30 | * 31 | * @return new encoder instance 32 | */ 33 | ChannelHandler newEncoder(); 34 | 35 | /** 36 | * Create an decoder instance. 37 | * 38 | * @return new decoder instance 39 | */ 40 | ChannelHandler newDecoder(); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/util/FutureTaskNotCompleted.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | /** 20 | * @author chengyi (mark.lx@antfin.com) 2018-10-18 16:30 21 | */ 22 | public class FutureTaskNotCompleted extends Exception { 23 | 24 | private static final long serialVersionUID = -3635466558774380138L; 25 | 26 | public FutureTaskNotCompleted() { 27 | } 28 | 29 | public FutureTaskNotCompleted(String message) { 30 | super(message); 31 | } 32 | 33 | public FutureTaskNotCompleted(String message, Throwable cause) { 34 | super(message, cause); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/InvokeCallbackListener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Listener to listen response and invoke callback. 21 | * 22 | * @author jiangping 23 | * @version $Id: InvokeCallbackListener.java, v 0.1 2015-9-21 PM5:17:08 tao Exp $ 24 | */ 25 | public interface InvokeCallbackListener { 26 | /** 27 | * Response arrived. 28 | * 29 | * @param future 30 | */ 31 | void onResponse(final InvokeFuture future); 32 | 33 | /** 34 | * Get the remote address. 35 | * 36 | * @return 37 | */ 38 | String getRemoteAddress(); 39 | } 40 | -------------------------------------------------------------------------------- /.middleware-common/pmd_rule_set.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | customize rule set priority 6 | 7 | 8 | 1 9 | 10 | 11 | 1 12 | 13 | 14 | 1 15 | 16 | 17 | 1 18 | 19 | 20 | 1 21 | 22 | 23 | 1 24 | 25 | 26 | 1 27 | 28 | 29 | 1 30 | 31 | 32 | 1 33 | 34 | 35 | 1 36 | 37 | 38 | 1 39 | 40 | 41 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/DefaultServerConnectionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Do some preparatory work in order to refactor the ConnectionManager in the next version. 21 | * 22 | * @author chengyi (mark.lx@antfin.com) 2019-03-07 14:40 23 | */ 24 | public class DefaultServerConnectionManager extends DefaultConnectionManager implements 25 | ServerConnectionManager { 26 | 27 | public DefaultServerConnectionManager(ConnectionSelectStrategy connectionSelectStrategy) { 28 | super(connectionSelectStrategy); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/RpcProtocolManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | import com.alipay.remoting.ProtocolManager; 20 | 21 | /** 22 | * Protocol manager. 23 | * 24 | * @author tsui 25 | * @version $Id: RpcProtocols.java, v 0.1 2018-03-27 19:42 tsui Exp $ 26 | */ 27 | public class RpcProtocolManager { 28 | public static final int DEFAULT_PROTOCOL_CODE_LENGTH = 1; 29 | 30 | public static void initProtocols() { 31 | ProtocolManager.registerProtocol(new RpcProtocol(), RpcProtocol.PROTOCOL_CODE); 32 | ProtocolManager.registerProtocol(new RpcProtocolV2(), RpcProtocolV2.PROTOCOL_CODE); 33 | } 34 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/constant/Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.constant; 18 | 19 | /** 20 | * Bolt Constants. 21 | * 22 | * @author chengyi (mark.lx@antfin.com) 2019-03-06 15:19 23 | */ 24 | public class Constants { 25 | 26 | /** 27 | * default expire time to remove connection pool, time unit: milliseconds 28 | */ 29 | public static final int DEFAULT_EXPIRE_TIME = 10 * 60 * 1000; 30 | 31 | /** 32 | * default retry times when failed to get result of FutureTask 33 | */ 34 | public static final int DEFAULT_RETRY_TIMES = 2; 35 | 36 | public static final String SSL_HANDLER = "sslHandler"; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/CommandDecoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.List; 20 | 21 | import io.netty.buffer.ByteBuf; 22 | import io.netty.channel.ChannelHandlerContext; 23 | 24 | /** 25 | * Decode command. 26 | * 27 | * @author jiangping 28 | * @version $Id: CommandDecoder.java, v 0.1 Mar 10, 2016 11:32:46 AM jiangping Exp $ 29 | */ 30 | public interface CommandDecoder { 31 | /** 32 | * Decode bytes into object. 33 | * 34 | * @param ctx 35 | * @param in 36 | * @param out 37 | * @throws Exception 38 | */ 39 | void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception; 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/RejectedExecutionPolicy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * RejectedExecutionPolicy determines how to deal with this situation that user executor rejected the {@link com.alipay.remoting.rpc.RpcInvokeCallbackListener.CallbackTask}. 21 | * 22 | * @author muyun 23 | * @version $Id: RejectedExecutionPolicy.java, v 0.1 2019年12月05日 7:38 PM muyun Exp $ 24 | */ 25 | public enum RejectedExecutionPolicy { 26 | /* discard the callback task */ 27 | DISCARD, 28 | /* caller runs the callback in IO-thread */ 29 | CALLER_RUNS, 30 | /* caller handle the task with exception strategy user provided */ 31 | CALLER_HANDLE_EXCEPTION 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/CommandEncoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.io.Serializable; 20 | 21 | import io.netty.buffer.ByteBuf; 22 | import io.netty.channel.ChannelHandlerContext; 23 | 24 | /** 25 | * Encode command. 26 | * 27 | * @author jiangping 28 | * @version $Id: CommandEncoder.java, v 0.1 Mar 10, 2016 11:33:02 AM jiangping Exp $ 29 | */ 30 | public interface CommandEncoder { 31 | 32 | /** 33 | * Encode object into bytes. 34 | * 35 | * @param ctx 36 | * @param msg 37 | * @param out 38 | * @throws Exception 39 | */ 40 | void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception; 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/benchmark/BenchmarkUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.benchmark; 18 | 19 | import com.alipay.remoting.AsyncContext; 20 | import com.alipay.remoting.BizContext; 21 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor; 22 | 23 | /** 24 | * 25 | * @author jiachun.fjc 26 | */ 27 | public class BenchmarkUserProcessor extends AsyncUserProcessor { 28 | 29 | @Override 30 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, Request request) { 31 | asyncCtx.sendResponse(request.getData()); 32 | } 33 | 34 | @Override 35 | public String interest() { 36 | return Request.class.getName(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/config/configs/ConfigItem.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.config.configs; 18 | 19 | /** 20 | * Items of config. 21 | * 22 | * Mainly used to define some config items managed by {@link ConfigContainer}. 23 | * You can define new config items based on this if need. 24 | * 25 | * @author tsui 26 | * @version $Id: ConfigItem.java, v 0.1 2018-07-28 17:43 tsui Exp $$ 27 | */ 28 | // fixme: remove in next version 29 | @Deprecated 30 | public enum ConfigItem { 31 | // ~~~ netty related 32 | @Deprecated 33 | NETTY_BUFFER_LOW_WATER_MARK, // netty writer buffer low water mark 34 | @Deprecated 35 | NETTY_BUFFER_HIGH_WATER_MARK // netty writer buffer high water mark 36 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/AsyncContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Async context for biz. 21 | * 22 | * @author xiaomin.cxm 23 | * @version $Id: AsyncContext.java, v 0.1 May 19, 2016 2:19:05 PM xiaomin.cxm Exp $ 24 | */ 25 | public interface AsyncContext { 26 | /** 27 | * send response back 28 | * 29 | * @param responseObject response object 30 | */ 31 | void sendResponse(Object responseObject); 32 | 33 | /** 34 | * send exception back 35 | * @param ex response exception 36 | */ 37 | default void sendException(Throwable ex) { 38 | throw new UnsupportedOperationException("default not support sendException!"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/util/IDGenerator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | import java.util.concurrent.atomic.AtomicInteger; 20 | 21 | /** 22 | * IDGenerator is used for generating request id in integer form. 23 | * 24 | * @author jiangping 25 | * @version $Id: IDGenerator.java, v 0.1 2015-9-23 PM5:28:58 tao Exp $ 26 | */ 27 | public class IDGenerator { 28 | private static final AtomicInteger id = new AtomicInteger(0); 29 | 30 | /** 31 | * generate the next id 32 | * 33 | * @return 34 | */ 35 | public static int nextId() { 36 | return id.incrementAndGet(); 37 | } 38 | 39 | public static void resetId() { 40 | id.set(0); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/simpledemo/SimpleUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.simpledemo; 18 | 19 | import com.alipay.remoting.AsyncContext; 20 | import com.alipay.remoting.BizContext; 21 | import com.alipay.remoting.rpc.protocol.AsyncUserProcessor; 22 | 23 | public class SimpleUserProcessor extends AsyncUserProcessor { 24 | 25 | @Override 26 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, SimpleRequest request) { 27 | int res = request.getNum() * request.getNum(); 28 | asyncCtx.sendResponse(new SimpleResponse(res)); 29 | } 30 | 31 | @Override 32 | public String interest() { 33 | return SimpleRequest.class.getName(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/connection/DefaultConnectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.connection; 18 | 19 | import com.alipay.remoting.codec.Codec; 20 | import com.alipay.remoting.config.Configuration; 21 | 22 | import io.netty.channel.ChannelHandler; 23 | 24 | /** 25 | * Default connection factory. 26 | * 27 | * @author chengyi (mark.lx@antfin.com) 2018-06-20 15:18 28 | */ 29 | public class DefaultConnectionFactory extends AbstractConnectionFactory { 30 | 31 | public DefaultConnectionFactory(Codec codec, ChannelHandler heartbeatHandler, 32 | ChannelHandler handler, Configuration configuration) { 33 | super(codec, heartbeatHandler, handler, configuration); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/HeartbeatCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import com.alipay.remoting.CommonCommandCode; 20 | import com.alipay.remoting.util.IDGenerator; 21 | 22 | /** 23 | * Heart beat. 24 | * 25 | * @author jiangping 26 | * @version $Id: HeartbeatCommand.java, v 0.1 2015-9-10 AM9:46:36 tao Exp $ 27 | */ 28 | public class HeartbeatCommand extends RequestCommand { 29 | 30 | /** For serialization */ 31 | private static final long serialVersionUID = 4949981019109517725L; 32 | 33 | /** 34 | * Construction. 35 | */ 36 | public HeartbeatCommand() { 37 | super(CommonCommandCode.HEARTBEAT); 38 | this.setId(IDGenerator.nextId()); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/MultiInterestUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | import java.util.List; 20 | 21 | /** 22 | * Support multi-interests feature based on UserProcessor 23 | * 24 | * The implementations of this interface don't need to implement the {@link com.alipay.remoting.rpc.protocol.UserProcessor#interest() interest()} method; 25 | * @author muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:19 AM 26 | */ 27 | public interface MultiInterestUserProcessor extends UserProcessor { 28 | 29 | /** 30 | * A list of the class names of user request. 31 | * Use String type to avoid classloader problem. 32 | */ 33 | List multiInterest(); 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/Reconnector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Reconnect manager interface. 21 | * 22 | * @author chengyi (mark.lx@antfin.com) 2018-11-05 17:43 23 | */ 24 | public interface Reconnector extends LifeCycle { 25 | 26 | /** 27 | * Do reconnecting in async mode. 28 | * 29 | * @param url target url 30 | */ 31 | void reconnect(Url url); 32 | 33 | /** 34 | * Disable reconnect to the target url. 35 | * 36 | * @param url target url 37 | */ 38 | void disableReconnect(Url url); 39 | 40 | /** 41 | * Enable reconnect to the target url. 42 | * 43 | * @param url target url 44 | */ 45 | void enableReconnect(Url url); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/InvokeCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.concurrent.Executor; 20 | 21 | /** 22 | * Invoke callback. 23 | * 24 | * @author jiangping 25 | * @version $Id: InvokeCallback.java, v 0.1 2015-9-30 AM10:24:26 tao Exp $ 26 | */ 27 | public interface InvokeCallback { 28 | 29 | /** 30 | * Response received. 31 | * 32 | * @param result 33 | */ 34 | void onResponse(final Object result); 35 | 36 | /** 37 | * Exception caught. 38 | * 39 | * @param e 40 | */ 41 | void onException(final Throwable e); 42 | 43 | /** 44 | * User defined executor. 45 | * 46 | * @return 47 | */ 48 | Executor getExecutor(); 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/HeartbeatAckCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import com.alipay.remoting.CommonCommandCode; 20 | import com.alipay.remoting.ResponseStatus; 21 | 22 | /** 23 | * Heartbeat ack. 24 | * 25 | * @author jiangping 26 | * @version $Id: HeartbeatAckCommand.java, v 0.1 2015-9-29 AM11:46:11 tao Exp $ 27 | */ 28 | public class HeartbeatAckCommand extends ResponseCommand { 29 | /** For serialization */ 30 | private static final long serialVersionUID = 2584912495844320855L; 31 | 32 | /** 33 | * Constructor. 34 | */ 35 | public HeartbeatAckCommand() { 36 | super(CommonCommandCode.HEARTBEAT); 37 | this.setResponseStatus(ResponseStatus.SUCCESS); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ExtendedNettyChannelHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import io.netty.channel.ChannelHandler; 20 | 21 | import java.util.List; 22 | 23 | /** 24 | * Leave it to external expansion and 25 | * support the addition of extended handler in the channel pipeline. 26 | */ 27 | public interface ExtendedNettyChannelHandler { 28 | 29 | /** 30 | * Netty ChannelHandlers to be added before Bolt's built-in Handler. 31 | * @return Netty ChannelHandler list 32 | */ 33 | List frontChannelHandlers(); 34 | 35 | /** 36 | * Netty ChannelHandlers to be added after Bolt's built-in Handler. 37 | * @return Netty ChannelHandler list 38 | */ 39 | List backChannelHandlers(); 40 | } 41 | -------------------------------------------------------------------------------- /plugins/wireshark/README.md: -------------------------------------------------------------------------------- 1 | # Bolt wireshark plugin 2 | 3 | How to use it: 4 | 5 | * For Mac or Linux: 6 | ``` 7 | $ sh install.sh 8 | ``` 9 | * For Windows: 10 | ``` 11 | $ .\install.bat 12 | ``` 13 | 14 | Then open wireshark and decode package as BOLT. 15 | 16 | Bolt package will be decoded like this: 17 | 18 | ``` 19 | Bolt Protocol Data 20 | Header 21 | rpc_trace_context.sofaRpcId: 0 22 | rpc_trace_context.sofaTraceId: 0bxxxx335162832343267634611586 23 | rpc_trace_context.sofaCallerIp: 1.2.3.4 24 | service: com.sofastack.demo.Service:1.0 25 | rpc_trace_context.sofaCallerApp: test-app 26 | sofa_head_method_name: hello 27 | Payload 28 | payload 29 | proto: 2 (BOLTv2) 30 | ver1: 1 31 | type: 1 (request) 32 | cmdcode: 1 (request) 33 | ver2: 1 34 | req_id: 0 35 | codec: 11 (protobuf) 36 | switch: 1 37 | timeout: 3000 38 | class_len: 44 39 | header_len: 691 40 | content_len: 65 41 | classname: com.alipay.sofa.rpc.core.request.SofaRequest 42 | rpc_id: 0 43 | trace_id: 0bxxxx335162832343267634611586 44 | ``` 45 | 46 | For advanced usage, you can search for any property under the bolt protocol, such as: 47 | ``` 48 | bolt.trace_id == 0bxxxx335162832343267634611586 49 | ``` 50 | 51 | 如果你想此 bolt 协议的解析脚本去解析更多的端口那么可以在此处添加更多的端口 52 | 53 | If you want the bolt protocol parsing script to parse more ports then you can add more ports here 54 | 55 | example: 56 | 57 | ```lua 58 | local ports = {12200, 12199, 12198} 59 | 60 | for _, port in ipairs(ports) do 61 | DissectorTable.get("tcp.port"):add(port, bolt_protocol) 62 | end 63 | ``` 64 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ConnectionHeartbeatManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Connection heart beat manager, operate heart beat whether enabled for a certain connection at runtime 21 | * 22 | * @author xiaomin.cxm 23 | * @version $Id: ConnectionHeartbeatManager.java, v 0.1 Apr 12, 2016 6:55:56 PM xiaomin.cxm Exp $ 24 | */ 25 | public interface ConnectionHeartbeatManager { 26 | 27 | /** 28 | * disable heart beat for a certain connection 29 | * 30 | * @param connection Connection 31 | */ 32 | void disableHeartbeat(Connection connection); 33 | 34 | /** 35 | * enable heart beat for a certain connection 36 | * 37 | * @param connection Connection 38 | */ 39 | void enableHeartbeat(Connection connection); 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/benchmark/BenchmarkServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.benchmark; 18 | 19 | import com.alipay.remoting.config.BoltServerOption; 20 | import com.alipay.remoting.rpc.RpcServer; 21 | 22 | /** 23 | * @author jiachun.fjc 24 | */ 25 | public class BenchmarkServer { 26 | 27 | public static void main(String[] args) { 28 | System.setProperty("bolt.netty.buffer.high.watermark", String.valueOf(64 * 1024 * 1024)); 29 | System.setProperty("bolt.netty.buffer.low.watermark", String.valueOf(32 * 1024 * 1024)); 30 | RpcServer rpcServer = new RpcServer(18090, true, true); 31 | rpcServer.option(BoltServerOption.NETTY_FLUSH_CONSOLIDATION, true); 32 | rpcServer.registerUserProcessor(new BenchmarkUserProcessor()); 33 | rpcServer.startup(); 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/exception/ConnectionClosedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.exception; 18 | 19 | /** 20 | * Exception when connection is closed. 21 | * 22 | * @author jiangping 23 | * @version $Id: ConnectionClosedException.java, v 0.1 Jan 15, 2016 3:13:12 PM tao Exp $ 24 | */ 25 | public class ConnectionClosedException extends RemotingException { 26 | 27 | /** For serialization */ 28 | private static final long serialVersionUID = -2595820033346329315L; 29 | 30 | /** 31 | * Default constructor. 32 | */ 33 | public ConnectionClosedException() { 34 | } 35 | 36 | public ConnectionClosedException(String msg) { 37 | super(msg); 38 | } 39 | 40 | public ConnectionClosedException(String msg, Throwable cause) { 41 | super(msg, cause); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/exception/InvokeException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.exception; 18 | 19 | import com.alipay.remoting.exception.RemotingException; 20 | 21 | /** 22 | * Exception when invoke failed 23 | * 24 | * @author jiangping 25 | * @version $Id: InvokeException.java, v 0.1 2015-10-5 PM8:19:36 tao Exp $ 26 | */ 27 | public class InvokeException extends RemotingException { 28 | /** For serialization */ 29 | private static final long serialVersionUID = -3974514863386363570L; 30 | 31 | /** 32 | * Default constructor. 33 | */ 34 | public InvokeException() { 35 | } 36 | 37 | public InvokeException(String msg) { 38 | super(msg); 39 | } 40 | 41 | public InvokeException(String msg, Throwable cause) { 42 | super(msg, cause); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/RpcConnectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import java.util.concurrent.ConcurrentHashMap; 20 | 21 | import com.alipay.remoting.config.Configuration; 22 | import com.alipay.remoting.connection.DefaultConnectionFactory; 23 | import com.alipay.remoting.rpc.protocol.UserProcessor; 24 | 25 | /** 26 | * Default RPC connection factory impl. 27 | * 28 | * @author chengyi (mark.lx@antfin.com) 2018-06-20 15:32 29 | */ 30 | public class RpcConnectionFactory extends DefaultConnectionFactory { 31 | 32 | public RpcConnectionFactory(ConcurrentHashMap> userProcessors, 33 | Configuration configurations) { 34 | super(new RpcCodec(), new HeartbeatHandler(), new RpcHandler(userProcessors), 35 | configurations); 36 | } 37 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/config/switches/Switch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.config.switches; 18 | 19 | /** 20 | * switch interface 21 | * 22 | * @author tsui 23 | * @version $Id: Switch.java, v 0.1 2018-04-08 11:26 tsui Exp $ 24 | */ 25 | public interface Switch { 26 | /** 27 | * api for user to turn on a feature 28 | * 29 | * @param index the switch index of feature 30 | */ 31 | void turnOn(int index); 32 | 33 | /** 34 | * api for user to turn off a feature 35 | * @param index the switch index of feature 36 | */ 37 | void turnOff(int index); 38 | 39 | /** 40 | * check switch whether on 41 | * 42 | * @param index the switch index of feature 43 | * @return true if either system setting is on or user setting is on 44 | */ 45 | boolean isOn(int index); 46 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/serialization/Serializer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.serialization; 18 | 19 | import com.alipay.remoting.exception.CodecException; 20 | 21 | /** 22 | * Serializer for serialize and deserialize. 23 | * 24 | * @author jiangping 25 | * @version $Id: Serializer.java, v 0.1 2015-10-4 PM9:37:57 tao Exp $ 26 | */ 27 | public interface Serializer { 28 | /** 29 | * Encode object into bytes. 30 | * 31 | * @param obj target object 32 | * @return serialized result 33 | */ 34 | byte[] serialize(final Object obj) throws CodecException; 35 | 36 | /** 37 | * Decode bytes into Object. 38 | * 39 | * @param data serialized data 40 | * @param classOfT class of original data 41 | */ 42 | T deserialize(final byte[] data, String classOfT) throws CodecException; 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/exception/RpcServerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.exception; 18 | 19 | import com.alipay.remoting.exception.RemotingException; 20 | 21 | /** 22 | * Rpc server exception when processing request 23 | * 24 | * @author jiangping 25 | * @version $Id: InvokeServerException.java, v 0.1 2015-10-9 PM11:16:10 tao Exp $ 26 | */ 27 | public class RpcServerException extends RemotingException { 28 | /** For serialization */ 29 | private static final long serialVersionUID = 4480283862377034355L; 30 | 31 | /** 32 | * Default constructor. 33 | */ 34 | public RpcServerException() { 35 | } 36 | 37 | public RpcServerException(String msg) { 38 | super(msg); 39 | } 40 | 41 | public RpcServerException(String msg, Throwable cause) { 42 | super(msg, cause); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/exception/InvokeServerException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.exception; 18 | 19 | import com.alipay.remoting.exception.RemotingException; 20 | 21 | /** 22 | * Server exception caught when invoking 23 | * 24 | * @author jiangping 25 | * @version $Id: InvokeServerException.java, v 0.1 2015-10-9 AM11:16:10 tao Exp $ 26 | */ 27 | public class InvokeServerException extends RemotingException { 28 | /** For serialization */ 29 | private static final long serialVersionUID = 4480283862377034355L; 30 | 31 | /** 32 | * Default constructor. 33 | */ 34 | public InvokeServerException() { 35 | } 36 | 37 | public InvokeServerException(String msg) { 38 | super(msg); 39 | } 40 | 41 | public InvokeServerException(String msg, Throwable cause) { 42 | super(msg, cause); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/RejectionProcessableInvokeCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * InvokeCallback which support {@link RejectedExecutionPolicy} is able to process the task-rejected situation. 21 | * 22 | * @author muyun 23 | * @version $Id: RejectionProcessableInvokeCallback.java, v 0.1 2019年12月05日 9:16 PM muyun Exp $ 24 | */ 25 | public interface RejectionProcessableInvokeCallback extends InvokeCallback { 26 | 27 | /** 28 | * when user executor rejected the {@link com.alipay.remoting.rpc.RpcInvokeCallbackListener.CallbackTask}, 29 | * bolt will handle the rejected task according to this {@link RejectedExecutionPolicy} 30 | * @return rejectedExecution Policy 31 | * @see com.alipay.remoting.rpc.RpcInvokeCallbackListener#onResponse(InvokeFuture) 32 | */ 33 | RejectedExecutionPolicy rejectedExecutionPolicy(); 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/exception/RemotingException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.exception; 18 | 19 | /** 20 | * Exception for default remoting problems 21 | * 22 | * @author jiangping 23 | * @version $Id: RemotingException.java, v 0.1 2015-9-21 PM 4:49:46 tao Exp $ 24 | */ 25 | public class RemotingException extends Exception { 26 | 27 | /** For serialization */ 28 | private static final long serialVersionUID = 6183635628271812505L; 29 | 30 | /** 31 | * Constructor. 32 | */ 33 | public RemotingException() { 34 | 35 | } 36 | 37 | /** 38 | * Constructor. 39 | */ 40 | public RemotingException(String message) { 41 | super(message); 42 | } 43 | 44 | /** 45 | * Constructor. 46 | */ 47 | public RemotingException(String message, Throwable cause) { 48 | super(message, cause); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/exception/InvokeSendFailedException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.exception; 18 | 19 | import com.alipay.remoting.exception.RemotingException; 20 | 21 | /** 22 | * Exception when invoke send failed 23 | * 24 | * @author jiangping 25 | * @version $Id: InvokeSendFailedException.java, v 0.1 2015-10-3 PM 12:32:45 tao Exp $ 26 | */ 27 | public class InvokeSendFailedException extends RemotingException { 28 | 29 | /** For serialization */ 30 | private static final long serialVersionUID = 4832257777758730796L; 31 | 32 | /** 33 | * Default constructor. 34 | */ 35 | public InvokeSendFailedException() { 36 | } 37 | 38 | public InvokeSendFailedException(String msg) { 39 | super(msg); 40 | } 41 | 42 | public InvokeSendFailedException(String msg, Throwable cause) { 43 | super(msg, cause); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/exception/InvokeServerBusyException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.exception; 18 | 19 | import com.alipay.remoting.exception.RemotingException; 20 | 21 | /** 22 | * Exception when thread pool busy of process server 23 | * 24 | * @author jiangping 25 | * @version $Id: InvokeServerBusyException.java, v 0.1 2015-10-9 AM11:16:10 tao Exp $ 26 | */ 27 | public class InvokeServerBusyException extends RemotingException { 28 | /** For serialization */ 29 | private static final long serialVersionUID = 4480283862377034355L; 30 | 31 | /** 32 | * Default constructor. 33 | */ 34 | public InvokeServerBusyException() { 35 | } 36 | 37 | public InvokeServerBusyException(String msg) { 38 | super(msg); 39 | } 40 | 41 | public InvokeServerBusyException(String msg, Throwable cause) { 42 | super(msg, cause); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/config/Configuration.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.config; 18 | 19 | /** 20 | * Config interface. 21 | * 22 | * @author chengyi (mark.lx@antfin.com) 2018-11-06 14:46 23 | */ 24 | public interface Configuration { 25 | 26 | /** 27 | * Get the option value. 28 | * 29 | * @param option target option 30 | * @return BoltOption 31 | */ 32 | T option(BoltOption option); 33 | 34 | /** 35 | * Allow to specify a {@link BoltOption} which is used for the {@link Configuration} instances once they got 36 | * created. Use a value of {@code null} to remove a previous set {@link BoltOption}. 37 | * 38 | * @param option target option 39 | * @param value option value, null to remove the previous option 40 | * @return Configurable instance 41 | */ 42 | Configuration option(BoltOption option, T value); 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/CommonCommandCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * The common command code, especially for heart beat command. 21 | * 22 | * @author jiangping 23 | * @version $Id: CommonCommandCode.java, v 0.1 2015-9-21 PM5:05:59 tao Exp $ 24 | */ 25 | public enum CommonCommandCode implements CommandCode { 26 | 27 | HEARTBEAT(CommandCode.HEARTBEAT_VALUE); 28 | 29 | private short value; 30 | 31 | CommonCommandCode(short value) { 32 | this.value = value; 33 | } 34 | 35 | @Override 36 | public short value() { 37 | return this.value; 38 | } 39 | 40 | public static CommonCommandCode valueOf(short value) { 41 | switch (value) { 42 | case CommandCode.HEARTBEAT_VALUE: 43 | return HEARTBEAT; 44 | } 45 | throw new IllegalArgumentException("Unknown Rpc command code value ," + value); 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/util/FutureTaskNotRunYetException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | /** 20 | * Exception to represent the run method of a future task has not been called. 21 | * 22 | * @author tsui 23 | * @version $Id: FutureTaskNotRunYetException.java, v 0.1 2017-07-31 16:29 tsui Exp $ 24 | */ 25 | public class FutureTaskNotRunYetException extends Exception { 26 | /** For serialization */ 27 | private static final long serialVersionUID = 2929126204324060632L; 28 | 29 | /** 30 | * Constructor. 31 | */ 32 | public FutureTaskNotRunYetException() { 33 | } 34 | 35 | /** 36 | * Constructor. 37 | */ 38 | public FutureTaskNotRunYetException(String message) { 39 | super(message); 40 | } 41 | 42 | /** 43 | * Constructor. 44 | */ 45 | public FutureTaskNotRunYetException(String message, Throwable cause) { 46 | super(message, cause); 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/RpcCodec.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import com.alipay.remoting.ProtocolCode; 20 | import com.alipay.remoting.codec.Codec; 21 | import com.alipay.remoting.codec.ProtocolCodeBasedEncoder; 22 | import com.alipay.remoting.rpc.protocol.RpcProtocolDecoder; 23 | import com.alipay.remoting.rpc.protocol.RpcProtocolManager; 24 | import com.alipay.remoting.rpc.protocol.RpcProtocolV2; 25 | 26 | import io.netty.channel.ChannelHandler; 27 | 28 | /** 29 | * @author muyun.cyt 30 | * @version 2018/6/26 下午3:51 31 | */ 32 | public class RpcCodec implements Codec { 33 | 34 | @Override 35 | public ChannelHandler newEncoder() { 36 | return new ProtocolCodeBasedEncoder(ProtocolCode.fromBytes(RpcProtocolV2.PROTOCOL_CODE)); 37 | } 38 | 39 | @Override 40 | public ChannelHandler newDecoder() { 41 | return new RpcProtocolDecoder(RpcProtocolManager.DEFAULT_PROTOCOL_CODE_LENGTH); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/AbstractMultiInterestUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | /** 20 | * Implements common function and provide default value. 21 | * more details in {@link com.alipay.remoting.rpc.protocol.AbstractUserProcessor} 22 | * @author muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:17 AM 23 | */ 24 | public abstract class AbstractMultiInterestUserProcessor extends AbstractUserProcessor 25 | implements 26 | MultiInterestUserProcessor { 27 | 28 | /** 29 | * do not need to implement this method because of the multiple interests 30 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#interest() 31 | * */ 32 | @Override 33 | public String interest() { 34 | return null; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/RemotingProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.concurrent.ExecutorService; 20 | 21 | /** 22 | * Remoting processor processes remoting commands. 23 | * 24 | * @author jiangping 25 | * @version $Id: RemotingProcessor.java, v 0.1 Dec 22, 2015 11:48:43 AM tao Exp $ 26 | */ 27 | public interface RemotingProcessor { 28 | 29 | /** 30 | * Process the remoting command. 31 | * 32 | * @param ctx 33 | * @param msg 34 | * @param defaultExecutor 35 | * @throws Exception 36 | */ 37 | void process(RemotingContext ctx, T msg, ExecutorService defaultExecutor) throws Exception; 38 | 39 | /** 40 | * Get the executor. 41 | * 42 | * @return 43 | */ 44 | ExecutorService getExecutor(); 45 | 46 | /** 47 | * Set executor. 48 | * 49 | * @param executor 50 | */ 51 | void setExecutor(ExecutorService executor); 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/RpcCommandCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | import com.alipay.remoting.CommandCode; 20 | 21 | /** 22 | * Command code for rpc remoting command. 23 | * @author jiangping 24 | * @version $Id: RpcCommandCode.java, v 0.1 2015-9-21 PM5:05:59 tao Exp $ 25 | */ 26 | public enum RpcCommandCode implements CommandCode { 27 | 28 | RPC_REQUEST((short) 1), RPC_RESPONSE((short) 2); 29 | 30 | private short value; 31 | 32 | RpcCommandCode(short value) { 33 | this.value = value; 34 | } 35 | 36 | @Override 37 | public short value() { 38 | return this.value; 39 | } 40 | 41 | public static RpcCommandCode valueOf(short value) { 42 | switch (value) { 43 | case 1: 44 | return RPC_REQUEST; 45 | case 2: 46 | return RPC_RESPONSE; 47 | } 48 | throw new IllegalArgumentException("Unknown Rpc command code value: " + value); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/exception/CodecException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.exception; 18 | 19 | /** 20 | * Exception when codec problems occur 21 | * 22 | * @author xiaomin.cxm 23 | * @version $Id: CodecException.java, v 0.1 2016-1-3 PM 6:26:12 xiaomin.cxm Exp $ 24 | */ 25 | public class CodecException extends RemotingException { 26 | 27 | /** For serialization */ 28 | private static final long serialVersionUID = -7513762648815278960L; 29 | 30 | /** 31 | * Constructor. 32 | */ 33 | public CodecException() { 34 | } 35 | 36 | /** 37 | * Constructor. 38 | * 39 | * @param message the detail message. 40 | */ 41 | public CodecException(String message) { 42 | super(message); 43 | } 44 | 45 | /** 46 | * Constructor. 47 | * 48 | * @param message the detail message 49 | * @param cause the cause 50 | */ 51 | public CodecException(String message, Throwable cause) { 52 | super(message, cause); 53 | } 54 | 55 | } -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one or more 3 | # contributor license agreements. See the NOTICE file distributed with 4 | # this work for additional information regarding copyright ownership. 5 | # The ASF licenses this file to You under the Apache License, Version 2.0 6 | # (the "License"); you may not use this file except in compliance with 7 | # the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | # This workflow will build a Java project with Maven 19 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 20 | 21 | name: build 22 | on: 23 | push: 24 | branches: [ master ] 25 | pull_request: 26 | types: [opened, synchronize, reopened] 27 | 28 | jobs: 29 | build: 30 | 31 | runs-on: ubuntu-latest 32 | 33 | strategy: 34 | matrix: 35 | java-version: [8, 11, 17, 21] 36 | fail-fast: false 37 | steps: 38 | - uses: actions/checkout@v2 39 | - name: Set up JDK ${{ matrix.java-version }} 40 | uses: actions/setup-java@v3 41 | with: 42 | java-version: ${{ matrix.java-version }} 43 | distribution: 'temurin' 44 | - name: Build with Maven 45 | run: ./mvnw clean install -Dmaven.test.skip=true -B -U -e && bash ./.middleware-common/check_format.sh 46 | - name: Test with Maven 47 | run: ./mvnw package 48 | - name: Codecov 49 | uses: codecov/codecov-action@v1 50 | 51 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/Protocol.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * A protocol contains a group of commands. 21 | * 22 | * @author jiangping 23 | * @version $Id: Protocol.java, v 0.1 2015-12-11 PM5:02:48 tao Exp $ 24 | */ 25 | public interface Protocol { 26 | /** 27 | * Get the newEncoder for the protocol. 28 | * 29 | * @return 30 | */ 31 | CommandEncoder getEncoder(); 32 | 33 | /** 34 | * Get the decoder for the protocol. 35 | * 36 | * @return 37 | */ 38 | CommandDecoder getDecoder(); 39 | 40 | /** 41 | * Get the heartbeat trigger for the protocol. 42 | * 43 | * @return 44 | */ 45 | HeartbeatTrigger getHeartbeatTrigger(); 46 | 47 | /** 48 | * Get the command handler for the protocol. 49 | * 50 | * @return 51 | */ 52 | CommandHandler getCommandHandler(); 53 | 54 | /** 55 | * Get the command factory for the protocol. 56 | * @return 57 | */ 58 | CommandFactory getCommandFactory(); 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/userprocessor/multiinterestprocessor/MultiInterestBaseRequestBody.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.userprocessor.multiinterestprocessor; 18 | 19 | import java.io.Serializable; 20 | 21 | /** 22 | * @antuor muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:20 AM 23 | */ 24 | public interface MultiInterestBaseRequestBody extends Serializable { 25 | /** 26 | * Getter method for property id. 27 | * 28 | * @return property value of id 29 | */ 30 | int getId(); 31 | 32 | /** 33 | * Setter method for property id. 34 | * 35 | * @param id value to be assigned to property id 36 | */ 37 | void setId(int id); 38 | 39 | /** 40 | * Getter method for property msg. 41 | * 42 | * @return property value of msg 43 | */ 44 | String getMsg(); 45 | 46 | /** 47 | * Setter method for property msg. 48 | * 49 | * @param msg value to be assigned to property msg 50 | */ 51 | void setMsg(String msg); 52 | } 53 | -------------------------------------------------------------------------------- /assembly/assembly.xml: -------------------------------------------------------------------------------- 1 | 19 | 23 | release 24 | 25 | dir 26 | 27 | 28 | 29 | src\main\java 30 | 31 | *.xml 32 | 33 | \ 34 | 35 | 36 | 37 | 38 | 39 | true 40 | lib 41 | runtime 42 | 43 | 44 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/simpledemo/QuickStartServerAndClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.simpledemo; 18 | 19 | import com.alipay.remoting.exception.RemotingException; 20 | import com.alipay.remoting.rpc.RpcClient; 21 | import com.alipay.remoting.rpc.RpcServer; 22 | 23 | public class QuickStartServerAndClient { 24 | public static void main(String[] args) throws RemotingException, InterruptedException { 25 | RpcServer rpcServer = new RpcServer(9876); 26 | rpcServer.registerUserProcessor(new SimpleUserProcessor()); 27 | rpcServer.startup(); 28 | 29 | RpcClient rpcClient = new RpcClient(); 30 | rpcClient.startup(); 31 | for (int i = 0; i < 10; i++) { 32 | SimpleResponse response = (SimpleResponse) rpcClient.invokeSync("127.0.0.1:9876", 33 | new SimpleRequest(i), 1000); 34 | System.out.println("i=" + i + " res=" + response.getRes()); 35 | Thread.sleep(1000); 36 | } 37 | 38 | rpcClient.shutdown(); 39 | rpcServer.shutdown(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing to SOFABolt 2 | SOFABolt is released under the Apache 2.0 license, and follows a very 3 | standard Github development process, using Github tracker for issues and 4 | merging pull requests into master . If you would like to contribute something, 5 | or simply want to hack on the code this document should help you get started. 6 | 7 | ### Sign the Contributor License Agreement 8 | Before we accept a non-trivial patch or pull request we will need you to 9 | sign the Contributor License Agreement. Signing the contributor’s agreement 10 | does not grant anyone commit rights to the main repository, but it does mean 11 | that we can accept your contributions, and you will get an author credit if 12 | we do. Active contributors might be asked to join the core team, and given 13 | the ability to merge pull requests. 14 | 15 | ### Code Conventions 16 | None of these is essential for a pull request, but they will all help. 17 | 18 | 1. we provided a [code formatter file](./.middleware-common/AlipayFormatter.xml), it will formatting 19 | automatically your project when during process of building. 20 | 21 | 2. Make sure all new `.java` files to have a simple Javadoc class comment 22 | with at least an `@author` tag identifying you, and preferably at least a 23 | paragraph on what the class is for. 24 | 25 | 3. Add the ASF license header comment to all new `.java` files (copy from existing files in the project) 26 | 27 | 4. Add yourself as an `@author` to the `.java` files that you modify substantially (more than cosmetic changes). 28 | 29 | 5. Add some Javadocs. 30 | 31 | 6. A few unit tests would help a lot as well — someone has to do it. 32 | 33 | 7. When writing a commit message please follow [these conventions](https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html), if 34 | you are fixing an existing issue please add Fixes gh-XXXX at the end 35 | of the commit message (where XXXX is the issue number). -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/simpledemo/QuickStartServerAndClientWithIpv6.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.simpledemo; 18 | 19 | import com.alipay.remoting.exception.RemotingException; 20 | import com.alipay.remoting.rpc.RpcClient; 21 | import com.alipay.remoting.rpc.RpcServer; 22 | 23 | public class QuickStartServerAndClientWithIpv6 { 24 | public static void main(String[] args) throws RemotingException, InterruptedException { 25 | RpcServer rpcServer = new RpcServer("0:0:0:0:0:0:0:1", 9875); 26 | rpcServer.registerUserProcessor(new SimpleUserProcessor()); 27 | rpcServer.startup(); 28 | 29 | RpcClient rpcClient = new RpcClient(); 30 | rpcClient.startup(); 31 | for (int i = 0; i < 10; i++) { 32 | SimpleResponse response = (SimpleResponse) rpcClient.invokeSync("0:0:0:0:0:0:0:1:9875", 33 | new SimpleRequest(i), 1000); 34 | System.out.println("i=" + i + " res=" + response.getRes()); 35 | Thread.sleep(1000); 36 | } 37 | 38 | rpcClient.shutdown(); 39 | rpcServer.shutdown(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/CommandHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.concurrent.ExecutorService; 20 | 21 | /** 22 | * Command handler. 23 | * 24 | * @author jiangping 25 | * @version $Id: CommandHandler.java, v 0.1 2015-12-14 PM4:03:55 tao Exp $ 26 | */ 27 | public interface CommandHandler { 28 | /** 29 | * Handle the command. 30 | * 31 | * @param ctx 32 | * @param msg 33 | * @throws Exception 34 | */ 35 | void handleCommand(RemotingContext ctx, Object msg) throws Exception; 36 | 37 | /** 38 | * Register processor for command with specified code. 39 | * 40 | * @param cmd 41 | * @param processor 42 | */ 43 | void registerProcessor(CommandCode cmd, RemotingProcessor processor); 44 | 45 | /** 46 | * Register default executor for the handler. 47 | * 48 | * @param executor 49 | */ 50 | void registerDefaultExecutor(ExecutorService executor); 51 | 52 | /** 53 | * Get default executor for the handler. 54 | */ 55 | ExecutorService getDefaultExecutor(); 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/exception/InvokeTimeoutException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.exception; 18 | 19 | import com.alipay.remoting.exception.RemotingException; 20 | 21 | /** 22 | * Exception when invoke timeout 23 | * 24 | * @author jiangping 25 | * @version $Id: InvokeTimeoutException.java, v 0.1 2015-9-28 PM3:35:53 tao Exp $ 26 | */ 27 | public class InvokeTimeoutException extends RemotingException { 28 | 29 | /** For serialization */ 30 | private static final long serialVersionUID = -7772633244795043476L; 31 | 32 | /** 33 | * Default constructor. 34 | */ 35 | public InvokeTimeoutException() { 36 | } 37 | 38 | /** 39 | * Constructor. 40 | * 41 | * @param msg the detail message 42 | */ 43 | public InvokeTimeoutException(String msg) { 44 | super(msg); 45 | } 46 | 47 | /** 48 | * Constructor. 49 | * 50 | * @param msg the detail message 51 | * @param cause the cause 52 | */ 53 | public InvokeTimeoutException(String msg, Throwable cause) { 54 | super(msg, cause); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/RpcProtocolDecoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | import com.alipay.remoting.codec.ProtocolCodeBasedDecoder; 20 | 21 | import io.netty.buffer.ByteBuf; 22 | 23 | /** 24 | * Rpc protocol decoder. 25 | * 26 | * @author tsui 27 | * @version $Id: RpcProtocolDecoder.java, v 0.1 2018-03-27 19:28 tsui Exp $ 28 | */ 29 | public class RpcProtocolDecoder extends ProtocolCodeBasedDecoder { 30 | public static final int MIN_PROTOCOL_CODE_WITH_VERSION = 2; 31 | 32 | public RpcProtocolDecoder(int protocolCodeLength) { 33 | super(protocolCodeLength); 34 | } 35 | 36 | @Override 37 | protected byte decodeProtocolVersion(ByteBuf in) { 38 | in.resetReaderIndex(); 39 | if (in.readableBytes() >= protocolCodeLength + DEFAULT_PROTOCOL_VERSION_LENGTH) { 40 | byte rpcProtocolCodeByte = in.readByte(); 41 | if (rpcProtocolCodeByte >= MIN_PROTOCOL_CODE_WITH_VERSION) { 42 | return in.readByte(); 43 | } 44 | } 45 | return DEFAULT_ILLEGAL_PROTOCOL_VERSION_LENGTH; 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ConnectionMonitorStrategy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.List; 20 | import java.util.Map; 21 | 22 | import com.alipay.remoting.util.RunStateRecordedFutureTask; 23 | 24 | /** 25 | * The strategy of connection monitor 26 | * 27 | * @author tsui 28 | * @version $Id: ConnectionMonitorStrategy.java, v 0.1 2017-02-21 12:06 tsui Exp $ 29 | */ 30 | public interface ConnectionMonitorStrategy { 31 | 32 | /** 33 | * Filter connections to monitor 34 | * 35 | * Deprecated this method, this should be a private method. 36 | * 37 | * @param connections connections from a connection pool 38 | */ 39 | @Deprecated 40 | Map> filter(List connections); 41 | 42 | /** 43 | * Add a set of connections to monitor. 44 | *

45 | * The previous connections in monitor of this protocol, 46 | * will be dropped by monitor automatically. 47 | * 48 | * @param connPools connection pools 49 | */ 50 | void monitor(Map> connPools); 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/TimerHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.concurrent.TimeUnit; 20 | 21 | import io.netty.util.HashedWheelTimer; 22 | import io.netty.util.Timer; 23 | 24 | /** 25 | * A singleton holder of the timer for timeout. 26 | * 27 | * @author jiangping 28 | * @version $Id: TimerHolder.java, v 0.1 2015-09-28 2:02:20 tao Exp $ 29 | */ 30 | public class TimerHolder { 31 | 32 | private final static long defaultTickDuration = 10; 33 | 34 | private static class DefaultInstance { 35 | static final Timer INSTANCE = new HashedWheelTimer(new NamedThreadFactory( 36 | "DefaultTimer" + defaultTickDuration, true), 37 | defaultTickDuration, TimeUnit.MILLISECONDS); 38 | } 39 | 40 | private TimerHolder() { 41 | } 42 | 43 | /** 44 | * Get a singleton instance of {@link Timer}.
45 | * The tick duration is {@link #defaultTickDuration}. 46 | * 47 | * @return Timer 48 | */ 49 | public static Timer getTimer() { 50 | return DefaultInstance.INSTANCE; 51 | } 52 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/RpcDeserializeLevel.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | /** 20 | * Rpc deserialize level. 21 | * 22 | * @author tsui 23 | * @version $Id: RpcDeserializeLevel.java, v 0.1 2017-04-24 15:12 tsui Exp $ 24 | */ 25 | public class RpcDeserializeLevel { 26 | /** deserialize clazz, header, contents all three parts of rpc command */ 27 | public final static int DESERIALIZE_ALL = 0x02; 28 | /** deserialize both header and clazz parts of rpc command */ 29 | public final static int DESERIALIZE_HEADER = 0x01; 30 | /** deserialize only the clazz part of rpc command */ 31 | public final static int DESERIALIZE_CLAZZ = 0x00; 32 | 33 | /** 34 | * Convert to String. 35 | */ 36 | public static String valueOf(int value) { 37 | switch (value) { 38 | case 0x00: 39 | return "DESERIALIZE_CLAZZ"; 40 | case 0x01: 41 | return "DESERIALIZE_HEADER"; 42 | case 0x02: 43 | return "DESERIALIZE_ALL"; 44 | } 45 | throw new IllegalArgumentException("Unknown deserialize level value ," + value); 46 | } 47 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/RpcConnectionEventHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import com.alipay.remoting.Connection; 20 | import com.alipay.remoting.ConnectionEventHandler; 21 | import com.alipay.remoting.config.Configuration; 22 | 23 | import io.netty.channel.ChannelHandlerContext; 24 | 25 | /** 26 | * ConnectionEventHandler for Rpc. 27 | * 28 | * @author jiangping 29 | * @version $Id: RpcConnectionEventHandler.java, v 0.1 2015-10-16 PM4:41:29 tao Exp $ 30 | */ 31 | public class RpcConnectionEventHandler extends ConnectionEventHandler { 32 | 33 | public RpcConnectionEventHandler() { 34 | super(); 35 | } 36 | 37 | public RpcConnectionEventHandler(Configuration configuration) { 38 | super(configuration); 39 | } 40 | 41 | /** 42 | * @see com.alipay.remoting.ConnectionEventHandler#channelInactive(io.netty.channel.ChannelHandlerContext) 43 | */ 44 | @Override 45 | public void channelInactive(ChannelHandlerContext ctx) throws Exception { 46 | Connection conn = ctx.channel().attr(Connection.CONNECTION).get(); 47 | if (conn != null) { 48 | this.getConnectionManager().remove(conn); 49 | } 50 | super.channelInactive(ctx); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/util/RunStateRecordedFutureTask.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | import java.util.concurrent.Callable; 20 | import java.util.concurrent.ExecutionException; 21 | import java.util.concurrent.FutureTask; 22 | import java.util.concurrent.atomic.AtomicBoolean; 23 | 24 | /** 25 | * A customized FutureTask which can record whether the run method has been called. 26 | * @author tsui 27 | * @version $Id: RunStateRecordedFutureTask.java, v 0.1 2017-07-31 16:28 tsui Exp $ 28 | */ 29 | public class RunStateRecordedFutureTask extends FutureTask { 30 | private AtomicBoolean hasRun = new AtomicBoolean(); 31 | 32 | public RunStateRecordedFutureTask(Callable callable) { 33 | super(callable); 34 | } 35 | 36 | @Override 37 | public void run() { 38 | this.hasRun.set(true); 39 | super.run(); 40 | } 41 | 42 | public V getAfterRun() throws InterruptedException, ExecutionException, 43 | FutureTaskNotRunYetException, FutureTaskNotCompleted { 44 | if (!hasRun.get()) { 45 | throw new FutureTaskNotRunYetException(); 46 | } 47 | 48 | if (!isDone()) { 49 | throw new FutureTaskNotCompleted(); 50 | } 51 | 52 | return super.get(); 53 | } 54 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/ProcessorManagerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | import com.alipay.remoting.rpc.protocol.RpcCommandCode; 23 | import com.alipay.remoting.rpc.protocol.RpcRequestProcessor; 24 | 25 | /** 26 | * test processor manager 27 | * 28 | * @author tsui 29 | * @version $Id: ProcessorManagerTest.java, v 0.1 2018-07-06 12:19 tsui Exp $$ 30 | */ 31 | public class ProcessorManagerTest { 32 | 33 | /** 34 | * test it should be override if register twice for the same command code 35 | */ 36 | @Test 37 | public void testRegisterProcessor() { 38 | ProcessorManager processorManager = new ProcessorManager(); 39 | CommandCode cmd1 = RpcCommandCode.RPC_REQUEST; 40 | CommandCode cmd2 = RpcCommandCode.RPC_REQUEST; 41 | RpcRequestProcessor rpcRequestProcessor1 = new RpcRequestProcessor(); 42 | RpcRequestProcessor rpcRequestProcessor2 = new RpcRequestProcessor(); 43 | processorManager.registerProcessor(cmd1, rpcRequestProcessor1); 44 | processorManager.registerProcessor(cmd2, rpcRequestProcessor2); 45 | Assert.assertEquals(processorManager.getProcessor(cmd1), rpcRequestProcessor2); 46 | Assert.assertEquals(processorManager.getProcessor(cmd2), rpcRequestProcessor2); 47 | } 48 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/util/TraceLogUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | import org.slf4j.Logger; 20 | 21 | import com.alipay.remoting.InvokeContext; 22 | 23 | /**
 24 | * Trace log util 25 | * 26 | * @author tsui
 27 | * @version $Id: TraceLogUtil.java, v 0.1 2016-08-02 17:31 tsui Exp $ 28 | */ 29 | public class TraceLogUtil { 30 | /** 31 | * print trace log 32 | * @param traceId 33 | * @param invokeContext 34 | */ 35 | public static void printConnectionTraceLog(Logger logger, String traceId, 36 | InvokeContext invokeContext) { 37 | String sourceIp = invokeContext.get(InvokeContext.CLIENT_LOCAL_IP); 38 | Integer sourcePort = invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT); 39 | String targetIp = invokeContext.get(InvokeContext.CLIENT_REMOTE_IP); 40 | Integer targetPort = invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT); 41 | StringBuilder logMsg = new StringBuilder(); 42 | logMsg.append(traceId).append(","); 43 | logMsg.append(sourceIp).append(","); 44 | logMsg.append(sourcePort).append(","); 45 | logMsg.append(targetIp).append(","); 46 | logMsg.append(targetPort); 47 | if (logger.isInfoEnabled()) { 48 | logger.info(logMsg.toString()); 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/common/DISCONNECTEventProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.common; 18 | 19 | import java.util.concurrent.atomic.AtomicBoolean; 20 | import java.util.concurrent.atomic.AtomicInteger; 21 | 22 | import org.junit.Assert; 23 | 24 | import com.alipay.remoting.Connection; 25 | import com.alipay.remoting.ConnectionEventProcessor; 26 | 27 | /** 28 | * ConnectionEventProcessor for ConnectionEventType.CLOSE 29 | * 30 | * @author xiaomin.cxm 31 | * @version $Id: DISCONNECTEventProcessor.java, v 0.1 Apr 8, 2016 10:58:48 AM xiaomin.cxm Exp $ 32 | */ 33 | public class DISCONNECTEventProcessor implements ConnectionEventProcessor { 34 | 35 | private AtomicBoolean dicConnected = new AtomicBoolean(); 36 | private AtomicInteger disConnectTimes = new AtomicInteger(); 37 | 38 | @Override 39 | public void onEvent(String remoteAddr, Connection conn) { 40 | Assert.assertNotNull(conn); 41 | dicConnected.set(true); 42 | disConnectTimes.incrementAndGet(); 43 | } 44 | 45 | public boolean isDisConnected() { 46 | return this.dicConnected.get(); 47 | } 48 | 49 | public int getDisConnectTimes() { 50 | return this.disConnectTimes.get(); 51 | } 52 | 53 | public void reset() { 54 | this.disConnectTimes.set(0); 55 | this.dicConnected.set(false); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/AbstractLifeCycle.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.concurrent.atomic.AtomicBoolean; 20 | 21 | /** 22 | * @author chengyi (mark.lx@antfin.com) 2018-11-05 14:43 23 | */ 24 | public abstract class AbstractLifeCycle implements LifeCycle { 25 | 26 | private final AtomicBoolean isStarted = new AtomicBoolean(false); 27 | 28 | @Override 29 | public void startup() throws LifeCycleException { 30 | if (isStarted.compareAndSet(false, true)) { 31 | return; 32 | } 33 | throw new LifeCycleException("this component has started"); 34 | } 35 | 36 | @Override 37 | public void shutdown() throws LifeCycleException { 38 | if (isStarted.compareAndSet(true, false)) { 39 | return; 40 | } 41 | throw new LifeCycleException("this component has closed"); 42 | } 43 | 44 | @Override 45 | public boolean isStarted() { 46 | return isStarted.get(); 47 | } 48 | 49 | /** 50 | * ensure the component has been startup before providing service. 51 | */ 52 | protected void ensureStarted() { 53 | if (!isStarted()) { 54 | throw new LifeCycleException(String.format( 55 | "Component(%s) has not been started yet, please startup first!", getClass() 56 | .getSimpleName())); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/DefaultClientConnectionManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import com.alipay.remoting.connection.ConnectionFactory; 20 | 21 | /** 22 | * Do some preparatory work in order to refactor the ConnectionManager in the next version. 23 | * 24 | * @author chengyi (mark.lx@antfin.com) 2019-03-07 14:27 25 | */ 26 | public class DefaultClientConnectionManager extends DefaultConnectionManager implements 27 | ClientConnectionManager { 28 | 29 | public DefaultClientConnectionManager(ConnectionSelectStrategy connectionSelectStrategy, 30 | ConnectionFactory connectionFactory, 31 | ConnectionEventHandler connectionEventHandler, 32 | ConnectionEventListener connectionEventListener) { 33 | super(connectionSelectStrategy, connectionFactory, connectionEventHandler, 34 | connectionEventListener); 35 | } 36 | 37 | @Override 38 | public void startup() throws LifeCycleException { 39 | super.startup(); 40 | 41 | this.connectionEventHandler.setConnectionManager(this); 42 | this.connectionEventHandler.setConnectionEventListener(connectionEventListener); 43 | this.connectionFactory.init(connectionEventHandler); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/HeartbeatHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import com.alipay.remoting.Connection; 20 | import com.alipay.remoting.Protocol; 21 | import com.alipay.remoting.ProtocolCode; 22 | import com.alipay.remoting.ProtocolManager; 23 | 24 | import io.netty.channel.ChannelDuplexHandler; 25 | import io.netty.channel.ChannelHandler.Sharable; 26 | import io.netty.channel.ChannelHandlerContext; 27 | import io.netty.handler.timeout.IdleStateEvent; 28 | 29 | /** 30 | * Heart beat triggerd. 31 | * 32 | * @author jiangping 33 | * @version $Id: SharableHandler.java, v 0.1 2015-12-14 PM3:16:00 tao Exp $ 34 | */ 35 | @Sharable 36 | public class HeartbeatHandler extends ChannelDuplexHandler { 37 | 38 | /** 39 | * 40 | * @see io.netty.channel.ChannelInboundHandlerAdapter#userEventTriggered(io.netty.channel.ChannelHandlerContext, java.lang.Object) 41 | */ 42 | @Override 43 | public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception { 44 | if (evt instanceof IdleStateEvent) { 45 | ProtocolCode protocolCode = ctx.channel().attr(Connection.PROTOCOL).get(); 46 | Protocol protocol = ProtocolManager.getProtocol(protocolCode); 47 | protocol.getHeartbeatTrigger().heartbeatTriggered(ctx); 48 | } else { 49 | super.userEventTriggered(ctx, evt); 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/RemotingAddressParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * Remoting address parser 21 | * 22 | * Implement this to generate a {@link Url} 23 | * 24 | * @author xiaomin.cxm 25 | * @version $Id: RemotingAddressParser.java, v 0.1 Mar 11, 2016 5:56:55 PM xiaomin.cxm Exp $ 26 | */ 27 | public interface RemotingAddressParser { 28 | /** 29 | * Parse a simple string url to get {@link Url} 30 | * 31 | * @param url url 32 | * @return parsed {@link Url} 33 | */ 34 | Url parse(String url); 35 | 36 | /** 37 | * Parse a simple string url to get a unique key of a certain address 38 | * 39 | * @param url url 40 | * @return unique key 41 | */ 42 | String parseUniqueKey(String url); 43 | 44 | /** 45 | * Parse to get property value according to specified property key 46 | * 47 | * @param url url 48 | * @param propKey property key 49 | * @return propValue 50 | */ 51 | String parseProperty(String url, String propKey); 52 | 53 | /** 54 | * Initialize {@link Url} arguments 55 | * 56 | * @param url url 57 | */ 58 | void initUrlArgs(Url url); 59 | 60 | /** symbol : */ 61 | char COLON = ':'; 62 | 63 | /** symbol = */ 64 | char EQUAL = '='; 65 | 66 | /** symbol & */ 67 | char AND = '&'; 68 | 69 | /** symbol ? */ 70 | char QUES = '?'; 71 | } 72 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/heartbeat/CustomHeartBeatProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.heartbeat; 18 | 19 | import java.util.Date; 20 | import java.util.concurrent.atomic.AtomicInteger; 21 | 22 | import org.slf4j.Logger; 23 | import org.slf4j.LoggerFactory; 24 | 25 | import com.alipay.remoting.AbstractRemotingProcessor; 26 | import com.alipay.remoting.RemotingCommand; 27 | import com.alipay.remoting.RemotingContext; 28 | 29 | /** 30 | * CustomHeartBeatProcessor 31 | * 32 | * @author xiaomin.cxm 33 | * @version $Id: CustomHeartBeatProcessor.java, v 0.1 Apr 12, 2016 12:05:19 PM xiaomin.cxm Exp $ 34 | */ 35 | public class CustomHeartBeatProcessor extends AbstractRemotingProcessor { 36 | static Logger logger = LoggerFactory.getLogger(CustomHeartBeatProcessor.class); 37 | 38 | private AtomicInteger heartBeatTimes = new AtomicInteger(); 39 | 40 | public int getHeartBeatTimes() { 41 | return heartBeatTimes.get(); 42 | } 43 | 44 | public void reset() { 45 | this.heartBeatTimes.set(0); 46 | } 47 | 48 | @Override 49 | public void doProcess(RemotingContext ctx, RemotingCommand msg) throws Exception { 50 | heartBeatTimes.incrementAndGet(); 51 | java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 52 | logger.warn("heart beat received:" + format1.format(new Date())); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/RemotingServerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | import com.alipay.remoting.rpc.common.PortScan; 23 | import com.alipay.remoting.rpc.RpcServer; 24 | 25 | /** 26 | * test {@link AbstractRemotingServer} apis 27 | * 28 | * @author tsui 29 | * @version $Id: RemotingServerTest.java, v 0.1 May 16, 2018 10:00:48 AM tsui Exp $ 30 | */ 31 | public class RemotingServerTest { 32 | @Test 33 | public void testStartRepeatedly() { 34 | RpcServer rpcServer = new RpcServer(PortScan.select()); 35 | rpcServer.start(); 36 | 37 | try { 38 | rpcServer.start(); 39 | Assert.fail("Should not reach here!"); 40 | } catch (Exception e) { 41 | // expect IllegalStateException 42 | } 43 | rpcServer.stop(); 44 | } 45 | 46 | @Test 47 | public void testStopRepeatedly() { 48 | RpcServer rpcServer = new RpcServer(PortScan.select()); 49 | try { 50 | rpcServer.start(); 51 | } catch (Exception e) { 52 | Assert.fail("Should not reach here!"); 53 | e.printStackTrace(); 54 | } 55 | rpcServer.stop(); 56 | try { 57 | rpcServer.stop(); 58 | Assert.fail("Should not reach here!"); 59 | } catch (Exception e) { 60 | // expect IllegalStateException 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/config/BoltOption.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.config; 18 | 19 | /** 20 | * The base implementation class of the configuration item. 21 | * 22 | * @author chengyi (mark.lx@antfin.com) 2018-11-06 17:25 23 | */ 24 | public class BoltOption { 25 | 26 | private final String name; 27 | private T defaultValue; 28 | 29 | protected BoltOption(String name, T defaultValue) { 30 | this.name = name; 31 | this.defaultValue = defaultValue; 32 | } 33 | 34 | public String name() { 35 | return name; 36 | } 37 | 38 | public T defaultValue() { 39 | return defaultValue; 40 | } 41 | 42 | public static BoltOption valueOf(String name) { 43 | return new BoltOption(name, null); 44 | } 45 | 46 | public static BoltOption valueOf(String name, T defaultValue) { 47 | return new BoltOption(name, defaultValue); 48 | } 49 | 50 | @Override 51 | public boolean equals(Object o) { 52 | if (this == o) { 53 | return true; 54 | } 55 | 56 | if (o == null || getClass() != o.getClass()) { 57 | return false; 58 | } 59 | 60 | BoltOption that = (BoltOption) o; 61 | 62 | return name != null ? name.equals(that.name) : that.name == null; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | return name != null ? name.hashCode() : 0; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/common/FifoServerUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.common; 18 | 19 | import com.alipay.remoting.BizContext; 20 | import com.alipay.remoting.rpc.protocol.SyncUserProcessor; 21 | 22 | import java.util.concurrent.Executor; 23 | import java.util.concurrent.Executors; 24 | 25 | public class FifoServerUserProcessor extends SyncUserProcessor { 26 | //key point: create a single thread pool to fifo process request 27 | private Executor executor = Executors.newSingleThreadExecutor(r -> new Thread(r, "FifoThread")); 28 | private Integer previousOrder = null; 29 | 30 | @Override 31 | public Object handleRequest(BizContext bizCtx, RequestBody request) throws Exception { 32 | System.out.println("thread[" + Thread.currentThread().getName() + "] Request received:" + request + ", arriveTimestamp:" + bizCtx.getArriveTimestamp()); 33 | Integer currentOrder = request.getId(); 34 | if (previousOrder != null) { 35 | if (currentOrder - previousOrder != 1) { 36 | System.out.println("error: not in fifo"); 37 | } 38 | } 39 | previousOrder = currentOrder; 40 | return RequestBody.DEFAULT_SERVER_RETURN_STR; 41 | } 42 | 43 | @Override 44 | public Executor getExecutor() { 45 | return executor; 46 | } 47 | 48 | @Override 49 | public String interest() { 50 | return RequestBody.class.getName(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/config/switches/GlobalSwitch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.config.switches; 18 | 19 | import java.util.BitSet; 20 | 21 | /** 22 | * Global switches used in client or server 23 | *

24 | * NOTICE:
25 | * 1. system settings will take effect in all bolt client or server instances in one process
26 | * 2. user settings will only take effect in the current instance of bolt client or server. 27 | *

28 | * 29 | * @author tsui 30 | * @version $Id: GlobalSwitch.java, v 0.1 2017-08-03 15:50 tsui Exp $ 31 | */ 32 | @Deprecated 33 | public class GlobalSwitch implements Switch { 34 | 35 | /** user settings */ 36 | private BitSet userSettings = new BitSet(); 37 | 38 | /** 39 | * Init with system default value 40 | * if settings exist by system property then use system property at first; 41 | * if no settings exist by system property then use default value in {@link com.alipay.remoting.config.Configs} 42 | * All these settings can be overwrite by user api settings. 43 | */ 44 | public GlobalSwitch() { 45 | 46 | } 47 | 48 | // ~~~ public methods 49 | @Override 50 | public void turnOn(int index) { 51 | this.userSettings.set(index); 52 | } 53 | 54 | @Override 55 | public void turnOff(int index) { 56 | this.userSettings.clear(index); 57 | } 58 | 59 | @Override 60 | public boolean isOn(int index) { 61 | return this.userSettings.get(index); 62 | } 63 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/SyncUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | import com.alipay.remoting.AsyncContext; 20 | import com.alipay.remoting.BizContext; 21 | 22 | /** 23 | * Extends this to process user defined request in SYNC way.
24 | * If you want process request in ASYNC way, please extends {@link AsyncUserProcessor}. 25 | * 26 | * @author xiaomin.cxm 27 | * @version $Id: SyncUserProcessor.java, v 0.1 May 19, 2016 2:47:21 PM xiaomin.cxm Exp $ 28 | */ 29 | public abstract class SyncUserProcessor extends AbstractUserProcessor { 30 | /** 31 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(com.alipay.remoting.BizContext, java.lang.Object) 32 | */ 33 | @Override 34 | public abstract Object handleRequest(BizContext bizCtx, T request) throws Exception; 35 | 36 | /** 37 | * unsupported here! 38 | * 39 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(com.alipay.remoting.BizContext, com.alipay.remoting.AsyncContext, java.lang.Object) 40 | */ 41 | @Override 42 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, T request) { 43 | throw new UnsupportedOperationException( 44 | "ASYNC handle request is unsupported in SyncUserProcessor!"); 45 | } 46 | 47 | /** 48 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#interest() 49 | */ 50 | @Override 51 | public abstract String interest(); 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/AsyncUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | import com.alipay.remoting.AsyncContext; 20 | import com.alipay.remoting.BizContext; 21 | 22 | /** 23 | * Extends this to process user defined request in ASYNC way.
24 | * If you want process request in SYNC way, please extends {@link SyncUserProcessor}. 25 | * 26 | * @author xiaomin.cxm 27 | * @version $Id: AsyncUserProcessor.java, v 0.1 May 16, 2016 8:18:03 PM xiaomin.cxm Exp $ 28 | */ 29 | public abstract class AsyncUserProcessor extends AbstractUserProcessor { 30 | /** 31 | * unsupported here! 32 | * 33 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(com.alipay.remoting.BizContext, java.lang.Object) 34 | */ 35 | @Override 36 | public Object handleRequest(BizContext bizCtx, T request) throws Exception { 37 | throw new UnsupportedOperationException( 38 | "SYNC handle request is unsupported in AsyncUserProcessor!"); 39 | } 40 | 41 | /** 42 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(com.alipay.remoting.BizContext, com.alipay.remoting.AsyncContext, java.lang.Object) 43 | */ 44 | @Override 45 | public abstract void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, T request); 46 | 47 | /** 48 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#interest() 49 | */ 50 | @Override 51 | public abstract String interest(); 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/RequestCommand.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import com.alipay.remoting.CommandCode; 20 | 21 | /** 22 | * Command of request. 23 | * 24 | * @author jiangping 25 | * @version $Id: RequestCommand.java, v 0.1 2015-9-10 AM10:27:59 tao Exp $ 26 | */ 27 | public abstract class RequestCommand extends RpcCommand { 28 | 29 | /** For serialization */ 30 | private static final long serialVersionUID = -3457717009326601317L; 31 | /** timeout, -1 stands for no timeout */ 32 | private int timeout = -1; 33 | 34 | public RequestCommand() { 35 | super(RpcCommandType.REQUEST); 36 | } 37 | 38 | public RequestCommand(CommandCode code) { 39 | super(RpcCommandType.REQUEST, code); 40 | } 41 | 42 | public RequestCommand(byte type, CommandCode code) { 43 | super(type, code); 44 | } 45 | 46 | public RequestCommand(byte version, byte type, CommandCode code) { 47 | super(version, type, code); 48 | } 49 | 50 | /** 51 | * Getter method for property timeout. 52 | * 53 | * @return property value of timeout 54 | */ 55 | public int getTimeout() { 56 | return timeout; 57 | } 58 | 59 | /** 60 | * Setter method for property timeout. 61 | * 62 | * @param timeout value to be assigned to property timeout 63 | */ 64 | public void setTimeout(int timeout) { 65 | this.timeout = timeout; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/config/BoltOptions.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.config; 18 | 19 | import java.util.concurrent.ConcurrentHashMap; 20 | 21 | /** 22 | * Option carrier. 23 | * 24 | * @author chengyi (mark.lx@antfin.com) 2018-11-06 17:42 25 | */ 26 | public class BoltOptions { 27 | 28 | private ConcurrentHashMap, Object> options = new ConcurrentHashMap, Object>(); 29 | 30 | /** 31 | * Get the optioned value. 32 | * Return default value if option does not exist. 33 | * 34 | * @param option target option 35 | * @return the optioned value of default value if option does not exist. 36 | */ 37 | @SuppressWarnings("unchecked") 38 | public T option(BoltOption option) { 39 | Object value = options.get(option); 40 | if (value == null) { 41 | value = option.defaultValue(); 42 | } 43 | 44 | return value == null ? null : (T) value; 45 | } 46 | 47 | /** 48 | * Set up an new option with specific value. 49 | * Use a value of {@code null} to remove a previous set {@link BoltOption}. 50 | * 51 | * @param option target option 52 | * @param value option value, null for remove a previous set {@link BoltOption}. 53 | * @return this BoltOptions instance 54 | */ 55 | public BoltOptions option(BoltOption option, T value) { 56 | if (value == null) { 57 | options.remove(option); 58 | return this; 59 | } 60 | 61 | options.put(option, value); 62 | return this; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/util/CrcUtil.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | import java.util.zip.CRC32; 20 | 21 | /** 22 | * CRC32 utility. 23 | * @author jiangping 24 | * @version $Id: CrcUtil2, v 0.1 2017-06-05 11:29 Timo Exp $ 25 | */ 26 | public class CrcUtil { 27 | 28 | private static final ThreadLocal CRC_32_THREAD_LOCAL = new ThreadLocal() { 29 | @Override 30 | protected CRC32 initialValue() { 31 | return new CRC32(); 32 | } 33 | }; 34 | 35 | /** 36 | * Compute CRC32 code for byte[]. 37 | * 38 | * @param array 39 | * @return 40 | */ 41 | public static final int crc32(byte[] array) { 42 | if (array != null) { 43 | return crc32(array, 0, array.length); 44 | } 45 | 46 | return 0; 47 | } 48 | 49 | /** 50 | * Compute CRC32 code for byte[]. 51 | * 52 | * @param array 53 | * @param offset 54 | * @param length 55 | * @return 56 | */ 57 | public static final int crc32(byte[] array, int offset, int length) { 58 | CRC32 crc32 = CRC_32_THREAD_LOCAL.get(); 59 | crc32.update(array, offset, length); 60 | int ret = (int) crc32.getValue(); 61 | crc32.reset(); 62 | return ret; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ProtocolManager.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.concurrent.ConcurrentHashMap; 20 | import java.util.concurrent.ConcurrentMap; 21 | 22 | /** 23 | * Manager of all protocols 24 | * 25 | * @author tsui 26 | * @version $Id: ProtocolManager.java, v 0.1 2018-03-27 15:18 tsui Exp $ 27 | */ 28 | public class ProtocolManager { 29 | 30 | private static final ConcurrentMap protocols = new ConcurrentHashMap(); 31 | 32 | public static Protocol getProtocol(ProtocolCode protocolCode) { 33 | return protocols.get(protocolCode); 34 | } 35 | 36 | public static void registerProtocol(Protocol protocol, byte... protocolCodeBytes) { 37 | registerProtocol(protocol, ProtocolCode.fromBytes(protocolCodeBytes)); 38 | } 39 | 40 | public static void registerProtocol(Protocol protocol, ProtocolCode protocolCode) { 41 | if (null == protocolCode || null == protocol) { 42 | throw new RuntimeException("Protocol: " + protocol + " and protocol code:" 43 | + protocolCode + " should not be null!"); 44 | } 45 | Protocol exists = ProtocolManager.protocols.putIfAbsent(protocolCode, protocol); 46 | if (exists != null) { 47 | throw new RuntimeException("Protocol for code: " + protocolCode + " already exists!"); 48 | } 49 | } 50 | 51 | public static Protocol unRegisterProtocol(byte protocolCode) { 52 | return ProtocolManager.protocols.remove(ProtocolCode.fromBytes(protocolCode)); 53 | } 54 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/AsyncMultiInterestUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | import java.util.List; 20 | 21 | import com.alipay.remoting.AsyncContext; 22 | import com.alipay.remoting.BizContext; 23 | 24 | /** 25 | * Extends this to process user defined request in ASYNC way.
26 | * If you want process request in SYNC way, please extends {@link SyncMultiInterestUserProcessor}. 27 | * @author muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:19 AM 28 | */ 29 | public abstract class AsyncMultiInterestUserProcessor extends 30 | AbstractMultiInterestUserProcessor { 31 | /** 32 | * unsupported here! 33 | * 34 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(com.alipay.remoting.BizContext, java.lang.Object) 35 | */ 36 | @Override 37 | public Object handleRequest(BizContext bizCtx, T request) throws Exception { 38 | throw new UnsupportedOperationException( 39 | "SYNC handle request is unsupported in AsyncMultiInterestUserProcessor!"); 40 | } 41 | 42 | /** 43 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(com.alipay.remoting.BizContext, com.alipay.remoting.AsyncContext, java.lang.Object) 44 | */ 45 | @Override 46 | public abstract void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, T request); 47 | 48 | /** 49 | * @see com.alipay.remoting.rpc.protocol.MultiInterestUserProcessor#multiInterest() 50 | */ 51 | @Override 52 | public abstract List multiInterest(); 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/rpc/protocol/SyncMultiInterestUserProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.protocol; 18 | 19 | import java.util.List; 20 | 21 | import com.alipay.remoting.AsyncContext; 22 | import com.alipay.remoting.BizContext; 23 | 24 | /** 25 | * Extends this to process user defined request in SYNC way.
26 | * If you want process request in ASYNC way, please extends {@link AsyncMultiInterestUserProcessor}. 27 | * 28 | * @author muyun.cyt (muyun.cyt@antfin.com) 2018/7/5 11:19 AM 29 | */ 30 | public abstract class SyncMultiInterestUserProcessor extends 31 | AbstractMultiInterestUserProcessor { 32 | 33 | /** 34 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(com.alipay.remoting.BizContext, java.lang.Object) 35 | */ 36 | @Override 37 | public abstract Object handleRequest(BizContext bizCtx, T request) throws Exception; 38 | 39 | /** 40 | * unsupported here! 41 | * 42 | * @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(com.alipay.remoting.BizContext, com.alipay.remoting.AsyncContext, java.lang.Object) 43 | */ 44 | @Override 45 | public void handleRequest(BizContext bizCtx, AsyncContext asyncCtx, T request) { 46 | throw new UnsupportedOperationException( 47 | "ASYNC handle request is unsupported in SyncMultiInterestUserProcessor!"); 48 | } 49 | 50 | /** 51 | * @see com.alipay.remoting.rpc.protocol.MultiInterestUserProcessor#multiInterest() 52 | */ 53 | @Override 54 | public abstract List multiInterest(); 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/exception/DeserializationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.exception; 18 | 19 | /** 20 | * Exception when deserialize failed 21 | * 22 | * @author tsui 23 | * @version $Id: DeserializationException.java, v 0.1 2017-07-26 16:13 tsui Exp $ 24 | */ 25 | public class DeserializationException extends CodecException { 26 | /** For serialization */ 27 | private static final long serialVersionUID = 310446237157256052L; 28 | 29 | private boolean serverSide = false; 30 | 31 | /** 32 | * Constructor. 33 | */ 34 | public DeserializationException() { 35 | 36 | } 37 | 38 | /** 39 | * Constructor. 40 | */ 41 | public DeserializationException(String message) { 42 | super(message); 43 | } 44 | 45 | /** 46 | * Constructor. 47 | */ 48 | public DeserializationException(String message, boolean serverSide) { 49 | this(message); 50 | this.serverSide = serverSide; 51 | } 52 | 53 | /** 54 | * Constructor. 55 | */ 56 | public DeserializationException(String message, Throwable cause) { 57 | super(message, cause); 58 | } 59 | 60 | /** 61 | * Constructor. 62 | */ 63 | public DeserializationException(String message, Throwable cause, boolean serverSide) { 64 | this(message, cause); 65 | this.serverSide = serverSide; 66 | } 67 | 68 | /** 69 | * Getter method for property serverSide. 70 | * 71 | * @return property value of serverSide 72 | */ 73 | public boolean isServerSide() { 74 | return serverSide; 75 | } 76 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/exception/SerializationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.exception; 18 | 19 | /** 20 | * Exception when serialize failed 21 | * 22 | * @author tsui 23 | * @version $Id: SerializationException.java, v 0.1 2017-07-26 16:12 tsui Exp $ 24 | */ 25 | public class SerializationException extends CodecException { 26 | /** 27 | * For serialization 28 | */ 29 | private static final long serialVersionUID = 5668965722686668067L; 30 | 31 | private boolean serverSide = false; 32 | 33 | /** 34 | * Constructor. 35 | */ 36 | public SerializationException() { 37 | 38 | } 39 | 40 | /** 41 | * Constructor. 42 | */ 43 | public SerializationException(String message) { 44 | super(message); 45 | } 46 | 47 | /** 48 | * Constructor. 49 | */ 50 | public SerializationException(String message, boolean serverSide) { 51 | this(message); 52 | this.serverSide = serverSide; 53 | } 54 | 55 | /** 56 | * Constructor. 57 | */ 58 | public SerializationException(String message, Throwable cause) { 59 | super(message, cause); 60 | } 61 | 62 | /** 63 | * Constructor. 64 | */ 65 | public SerializationException(String message, Throwable cause, boolean serverSide) { 66 | this(message, cause); 67 | this.serverSide = serverSide; 68 | } 69 | 70 | /** 71 | * Getter method for property serverSide. 72 | * 73 | * @return property value of serverSide 74 | */ 75 | public boolean isServerSide() { 76 | return serverSide; 77 | } 78 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ProtocolCode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import java.util.Arrays; 20 | 21 | /** 22 | * Protocol code definition, you can define your own protocol code in byte array {@link ProtocolCode#version} 23 | * We suggest to use just one byte for simplicity. 24 | * 25 | * @author tsui 26 | * @version $Id: ProtocolCode.java, v 0.1 2018-03-27 17:23 tsui Exp $ 27 | */ 28 | public class ProtocolCode { 29 | /** bytes to represent protocol code */ 30 | byte[] version; 31 | 32 | private ProtocolCode(byte... version) { 33 | this.version = version; 34 | } 35 | 36 | public static ProtocolCode fromBytes(byte... version) { 37 | return new ProtocolCode(version); 38 | } 39 | 40 | /** 41 | * get the first single byte if your protocol code is single code. 42 | * @return 43 | */ 44 | public byte getFirstByte() { 45 | return this.version[0]; 46 | } 47 | 48 | public int length() { 49 | return this.version.length; 50 | } 51 | 52 | @Override 53 | public boolean equals(Object o) { 54 | if (this == o) { 55 | return true; 56 | } 57 | if (o == null || getClass() != o.getClass()) { 58 | return false; 59 | } 60 | ProtocolCode that = (ProtocolCode) o; 61 | return Arrays.equals(version, that.version); 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | return Arrays.hashCode(version); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "ProtocolVersion{" + "version=" + Arrays.toString(version) + '}'; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/ConnectionEventListenerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import com.alipay.remoting.rpc.RpcClient; 20 | import org.junit.Assert; 21 | import org.junit.Test; 22 | 23 | import java.util.concurrent.CountDownLatch; 24 | import java.util.concurrent.TimeUnit; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class ConnectionEventListenerTest { 29 | 30 | @Test 31 | public void addConnectionEventProcessorConcurrentTest() throws InterruptedException { 32 | int concurrentNum = 100; 33 | CountDownLatch countDownLatch = new CountDownLatch(concurrentNum); 34 | RpcClient rpcClient = new RpcClient(); 35 | for (int i = 0; i < concurrentNum; ++i) { 36 | MyThread thread = new MyThread(countDownLatch, rpcClient); 37 | new Thread(thread).start(); 38 | } 39 | Assert.assertTrue(countDownLatch.await(2, TimeUnit.SECONDS)); 40 | } 41 | 42 | static class MyThread implements Runnable { 43 | CountDownLatch countDownLatch; 44 | RpcClient rpcClient; 45 | 46 | public MyThread(CountDownLatch countDownLatch, RpcClient rpcClient) { 47 | this.countDownLatch = countDownLatch; 48 | this.rpcClient = rpcClient; 49 | } 50 | 51 | @Override 52 | public void run() { 53 | try { 54 | rpcClient.addConnectionEventProcessor(ConnectionEventType.CONNECT, (remoteAddress, connection) -> {}); 55 | } catch (Exception e) { 56 | fail(); 57 | } finally { 58 | countDownLatch.countDown(); 59 | } 60 | } 61 | } 62 | } -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/ServerIdleHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | import org.slf4j.Logger; 20 | 21 | import com.alipay.remoting.log.BoltLoggerFactory; 22 | import com.alipay.remoting.util.RemotingUtil; 23 | 24 | import io.netty.channel.ChannelDuplexHandler; 25 | import io.netty.channel.ChannelHandler.Sharable; 26 | import io.netty.channel.ChannelHandlerContext; 27 | import io.netty.handler.timeout.IdleStateEvent; 28 | 29 | /** 30 | * Server Idle handler. 31 | * 32 | * In the server side, the connection will be closed if it is idle for a certain period of time. 33 | * 34 | * @author jiangping 35 | * @version $Id: ServerIdleHandler.java, v 0.1 Nov 3, 2015 05:23:19 PM tao Exp $ 36 | */ 37 | @Sharable 38 | public class ServerIdleHandler extends ChannelDuplexHandler { 39 | 40 | private static final Logger logger = BoltLoggerFactory.getLogger("CommonDefault"); 41 | 42 | /** 43 | * @see io.netty.channel.ChannelInboundHandlerAdapter#userEventTriggered(io.netty.channel.ChannelHandlerContext, java.lang.Object) 44 | */ 45 | @Override 46 | public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception { 47 | if (evt instanceof IdleStateEvent) { 48 | try { 49 | logger.warn("Connection idle, close it from server side: {}", 50 | RemotingUtil.parseRemoteAddress(ctx.channel())); 51 | ctx.close(); 52 | } catch (Exception e) { 53 | logger.warn("Exception caught when closing connection in ServerIdleHandler.", e); 54 | } 55 | } else { 56 | super.userEventTriggered(ctx, evt); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/RpcCommandTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import com.alipay.remoting.rpc.protocol.RpcRequestCommand; 20 | import org.junit.Assert; 21 | import org.junit.Rule; 22 | import org.junit.Test; 23 | import org.junit.rules.ExpectedException; 24 | 25 | public class RpcCommandTest { 26 | @Rule 27 | public ExpectedException thrown = ExpectedException.none(); 28 | 29 | @Test 30 | public void setClazz_normal_len() { 31 | RpcCommand rpcCommand = new RpcRequestCommand(); 32 | byte[] clazz = new byte[100]; 33 | rpcCommand.setClazz(clazz); 34 | Assert.assertNotNull(rpcCommand.getClazz()); 35 | } 36 | 37 | @Test 38 | public void setClazz_exceed_maximum() { 39 | RpcCommand rpcCommand = new RpcRequestCommand(); 40 | byte[] clazz = new byte[Short.MAX_VALUE + 1]; 41 | thrown.expect(RuntimeException.class); 42 | thrown.expectMessage("class length exceed maximum, len=" + clazz.length); 43 | rpcCommand.setClazz(clazz); 44 | } 45 | 46 | @Test 47 | public void setHeader_normal_len() { 48 | RpcCommand rpcCommand = new RpcRequestCommand(); 49 | byte[] header = new byte[100]; 50 | rpcCommand.setHeader(header); 51 | Assert.assertNotNull(rpcCommand.getHeader()); 52 | } 53 | 54 | @Test 55 | public void setHeader_exceed_maximum() { 56 | RpcCommand rpcCommand = new RpcRequestCommand(); 57 | byte[] header = new byte[Short.MAX_VALUE + 1]; 58 | thrown.expect(RuntimeException.class); 59 | thrown.expectMessage("header length exceed maximum, len=" + header.length); 60 | rpcCommand.setHeader(header); 61 | } 62 | } -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Licensed to the Apache Software Foundation (ASF) under one or more 3 | # contributor license agreements. See the NOTICE file distributed with 4 | # this work for additional information regarding copyright ownership. 5 | # The ASF licenses this file to You under the Apache License, Version 2.0 6 | # (the "License"); you may not use this file except in compliance with 7 | # the License. You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | # 17 | 18 | # This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time 19 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 20 | 21 | name: Release 22 | 23 | on: 24 | workflow_dispatch: 25 | 26 | 27 | jobs: 28 | build: 29 | runs-on: ubuntu-latest 30 | 31 | steps: 32 | - uses: actions/checkout@v3 33 | - name: Set up JDK 8 34 | uses: actions/setup-java@v3 35 | with: 36 | java-version: '8' 37 | distribution: 'temurin' 38 | cache: maven 39 | - name: Build with Maven 40 | run: mvn clean install -Pci-install -B -U -e && bash ./.middleware-common/check_format.sh 41 | release: 42 | needs: build 43 | runs-on: ubuntu-latest 44 | steps: 45 | - uses: actions/checkout@v3 46 | - name: Set up JDK 8 47 | uses: actions/setup-java@v3 48 | with: 49 | java-version: '8' 50 | distribution: 'temurin' 51 | cache: maven 52 | server-id: ossrh 53 | server-username: MAVEN_USERNAME 54 | server-password: MAVEN_PASSWORD 55 | gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import 56 | gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase 57 | - name: Build with Maven 58 | run: mvn --batch-mode deploy -DskipTests -Prelease 59 | env: 60 | MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }} 61 | MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }} 62 | MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} 63 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/addressargs/RpcAddressParser_SOFTREF_Test.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.addressargs; 18 | 19 | import org.junit.Assert; 20 | 21 | import com.alipay.remoting.Url; 22 | import com.alipay.remoting.exception.RemotingException; 23 | import com.alipay.remoting.rpc.RpcAddressParser; 24 | 25 | /** 26 | * rpc address parser 27 | * test soft reference 28 | * 29 | * @author xiaomin.cxm 30 | * @version $Id: RpcAddressParser_SOFTREF_Test.java, v 0.1 Apr 6, 2016 10:45:13 AM xiaomin.cxm Exp $ 31 | */ 32 | public class RpcAddressParser_SOFTREF_Test { 33 | 34 | // @Test 35 | public void testParserNonProtocol() throws RemotingException { 36 | String url = "127.0.0.1:1111?_TIMEOUT=3000&_SERIALIZETYPE=hessian2"; 37 | RpcAddressParser parser = new RpcAddressParser(); 38 | int MAX = 1000000; 39 | 40 | printMemory(); 41 | long start1 = System.currentTimeMillis(); 42 | for (int i = 0; i < MAX; ++i) { 43 | Url btUrl = parser.parse(url); 44 | Assert.assertEquals(btUrl.getUniqueKey(), "127.0.0.1:1111"); 45 | } 46 | long end1 = System.currentTimeMillis(); 47 | long time1 = end1 - start1; 48 | System.out.println("time1:" + time1); 49 | printMemory(); 50 | } 51 | 52 | private void printMemory() { 53 | int mb = 1024 * 1024; 54 | Runtime rt = Runtime.getRuntime(); 55 | long total = rt.totalMemory(); 56 | long max = rt.maxMemory(); 57 | long free = rt.freeMemory(); 58 | System.out.print("total[" + total / mb + "mb] "); 59 | System.out.print("max[" + max / mb + "mb] "); 60 | System.out.println("free[" + free / mb + "mb]"); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/util/ThreadLocalArriveTimeHolder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.util; 18 | 19 | import io.netty.channel.Channel; 20 | import io.netty.util.concurrent.FastThreadLocal; 21 | 22 | import java.util.HashMap; 23 | import java.util.Map; 24 | import java.util.WeakHashMap; 25 | 26 | /** 27 | * @author zhaowang 28 | * @version : ThreadLocalTimeHolder.java, v 0.1 2021年07月01日 3:05 下午 zhaowang 29 | */ 30 | public class ThreadLocalArriveTimeHolder { 31 | 32 | private static FastThreadLocal>> arriveTimeInNano = new FastThreadLocal>>(); 33 | 34 | public static void arrive(Channel channel, Integer key) { 35 | Map map = getArriveTimeMap(channel); 36 | if (map.get(key) == null) { 37 | map.put(key, System.nanoTime()); 38 | } 39 | } 40 | 41 | public static long getAndClear(Channel channel, Integer key) { 42 | Map map = getArriveTimeMap(channel); 43 | Long result = map.remove(key); 44 | if (result == null) { 45 | return -1; 46 | } 47 | return result; 48 | } 49 | 50 | private static Map getArriveTimeMap(Channel channel) { 51 | WeakHashMap> map = arriveTimeInNano.get(); 52 | if (map == null) { 53 | arriveTimeInNano.set(new WeakHashMap>(256)); 54 | map = arriveTimeInNano.get(); 55 | } 56 | Map subMap = map.get(channel); 57 | if (subMap == null) { 58 | map.put(channel, new HashMap()); 59 | } 60 | return map.get(channel); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/serialization/HessianSerializerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.serialization; 18 | 19 | import org.junit.Test; 20 | 21 | import java.util.UUID; 22 | import java.util.concurrent.CountDownLatch; 23 | import java.util.concurrent.TimeUnit; 24 | 25 | import static org.junit.Assert.*; 26 | 27 | public class HessianSerializerTest { 28 | 29 | private static HessianSerializer serializer = new HessianSerializer(); 30 | 31 | @Test 32 | public void concurrentSerializeTest() throws InterruptedException { 33 | int concurrentNum = 10; 34 | CountDownLatch countDownLatch = new CountDownLatch(concurrentNum); 35 | for (int i = 0; i < concurrentNum; ++i) { 36 | MyThread thread = new MyThread(countDownLatch); 37 | new Thread(thread).start(); 38 | } 39 | countDownLatch.await(2, TimeUnit.SECONDS); 40 | 41 | } 42 | 43 | static class MyThread implements Runnable { 44 | CountDownLatch countDownLatch; 45 | 46 | public MyThread(CountDownLatch countDownLatch) { 47 | this.countDownLatch = countDownLatch; 48 | } 49 | 50 | @Override 51 | public void run() { 52 | try { 53 | for (int i = 0; i < 100; i++) { 54 | String randomStr = UUID.randomUUID().toString(); 55 | byte[] bytes = serializer.serialize(randomStr); 56 | String o = serializer.deserialize(bytes, null); 57 | assertEquals(o, randomStr); 58 | } 59 | } catch (Exception e) { 60 | fail(); 61 | } finally { 62 | countDownLatch.countDown(); 63 | } 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/LifeCycleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import com.alipay.remoting.LifeCycleException; 20 | import org.junit.Assert; 21 | import org.junit.Test; 22 | 23 | /** 24 | * 25 | * @author muyun 26 | * @version $Id: LifeCycleTest.java, v 0.1 2019年12月02日 10:25 AM muyun Exp $ 27 | */ 28 | public class LifeCycleTest { 29 | 30 | private RpcServer server = new RpcServer(9999, true); 31 | private RpcClient client = new RpcClient(); 32 | 33 | @Test 34 | public void testAvailabilityCheck() { 35 | Assert.assertTrue(testFunctionAvailable(false)); 36 | server.startup(); 37 | client.startup(); 38 | Assert.assertTrue(testFunctionAvailable(true)); 39 | server.shutdown(); 40 | client.shutdown(); 41 | Assert.assertTrue(testFunctionAvailable(false)); 42 | } 43 | 44 | private boolean testFunctionAvailable(boolean expectedResult) { 45 | try { 46 | server.isConnected("127.0.0.1:9999"); 47 | if (!expectedResult) { 48 | return false; 49 | } 50 | } catch (LifeCycleException e) { 51 | if (expectedResult) { 52 | return false; 53 | } 54 | } 55 | 56 | try { 57 | client.getConnection("127.0.0.1:9999", 1000); 58 | if (!expectedResult) { 59 | return false; 60 | } 61 | } catch (LifeCycleException e) { 62 | if (expectedResult) { 63 | return false; 64 | } 65 | } catch (Exception e) { 66 | if (expectedResult) { 67 | return false; 68 | } 69 | } 70 | 71 | return true; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/common/PortScan.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc.common; 18 | 19 | import java.io.IOException; 20 | import java.net.InetSocketAddress; 21 | import java.net.ServerSocket; 22 | import java.net.Socket; 23 | 24 | import org.slf4j.Logger; 25 | import org.slf4j.LoggerFactory; 26 | 27 | public class PortScan { 28 | private static final Logger logger = LoggerFactory.getLogger(PortScan.class); 29 | 30 | static public int select() { 31 | int port = -1; 32 | ServerSocket ss = null; 33 | try { 34 | ss = new ServerSocket(); 35 | ss.bind(null); 36 | port = ss.getLocalPort(); 37 | } catch (IOException ioe) { 38 | ioe.printStackTrace(); 39 | } finally { 40 | try { 41 | if (ss != null) { 42 | ss.close(); 43 | logger.warn("Server socket close status: {}", ss.isClosed()); 44 | } 45 | } catch (IOException e) { 46 | } 47 | } 48 | return port; 49 | } 50 | 51 | public static void main(String[] args) throws Exception { 52 | int port = PortScan.select(); 53 | ServerSocket ss = new ServerSocket(); 54 | ss.bind(new InetSocketAddress(port)); 55 | logger.warn("listening on port:{}", port); 56 | 57 | Thread.sleep(100); 58 | Socket s = new Socket("localhost", port); 59 | System.out.println(s.isConnected()); 60 | System.out.println("local port: " + s.getLocalPort()); 61 | System.out.println("remote port: " + s.getPort()); 62 | Object lock = new Object(); 63 | 64 | synchronized (lock) { 65 | lock.wait(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/com/alipay/remoting/BizContext.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting; 18 | 19 | /** 20 | * basic info for biz 21 | * 22 | * @author xiaomin.cxm 23 | * @version $Id: BizContext.java, v 0.1 Jan 6, 2016 10:35:04 PM xiaomin.cxm Exp $ 24 | */ 25 | public interface BizContext { 26 | /** 27 | * get remote address 28 | * 29 | * @return remote address 30 | */ 31 | String getRemoteAddress(); 32 | 33 | /** 34 | * get remote host ip 35 | * 36 | * @return remote host 37 | */ 38 | String getRemoteHost(); 39 | 40 | /** 41 | * get remote port 42 | * 43 | * @return remote port 44 | */ 45 | int getRemotePort(); 46 | 47 | /** 48 | * get the connection of this request 49 | * 50 | * @return connection 51 | */ 52 | Connection getConnection(); 53 | 54 | /** 55 | * check whether request already timeout 56 | * 57 | * @return true if already timeout, you can log some useful info and then discard this request. 58 | */ 59 | boolean isRequestTimeout(); 60 | 61 | /** 62 | * get the timeout value from rpc client. 63 | * 64 | * @return client timeout 65 | */ 66 | int getClientTimeout(); 67 | 68 | /** 69 | * get the arrive time stamp 70 | * 71 | * @return the arrive time stamp 72 | */ 73 | long getArriveTimestamp(); 74 | 75 | /** 76 | * put a key and value 77 | */ 78 | void put(String key, String value); 79 | 80 | /** 81 | * get value 82 | * 83 | * @param key target key 84 | * @return value 85 | */ 86 | String get(String key); 87 | 88 | /** 89 | * get invoke context. 90 | * 91 | * @return InvokeContext 92 | */ 93 | InvokeContext getInvokeContext(); 94 | } -------------------------------------------------------------------------------- /src/test/java/com/alipay/remoting/rpc/RpcConfigManagerTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | package com.alipay.remoting.rpc; 18 | 19 | import javax.net.ssl.KeyManagerFactory; 20 | import javax.net.ssl.TrustManagerFactory; 21 | import org.junit.After; 22 | import org.junit.AfterClass; 23 | import org.junit.Assert; 24 | import org.junit.Before; 25 | import org.junit.BeforeClass; 26 | import org.junit.Test; 27 | 28 | public class RpcConfigManagerTest { 29 | @BeforeClass 30 | public static void initClass() { 31 | } 32 | 33 | @Before 34 | public void init() { 35 | } 36 | 37 | @After 38 | public void stop() { 39 | } 40 | 41 | @AfterClass 42 | public static void afterClass() { 43 | } 44 | 45 | @Test 46 | public void testSystemSettings() { 47 | Assert.assertTrue(RpcConfigManager.dispatch_msg_list_in_default_executor()); 48 | 49 | Assert.assertFalse(RpcConfigManager.server_ssl_enable()); 50 | Assert.assertFalse(RpcConfigManager.server_ssl_need_client_auth()); 51 | Assert.assertNull(RpcConfigManager.server_ssl_keystore_pass()); 52 | Assert.assertNull(RpcConfigManager.server_ssl_keystore()); 53 | Assert.assertNull(RpcConfigManager.server_ssl_keystore_type()); 54 | Assert.assertEquals(KeyManagerFactory.getDefaultAlgorithm(), 55 | RpcConfigManager.server_ssl_kmf_algorithm()); 56 | 57 | Assert.assertFalse(RpcConfigManager.client_ssl_enable()); 58 | Assert.assertNull(RpcConfigManager.client_ssl_keystore_pass()); 59 | Assert.assertNull(RpcConfigManager.client_ssl_keystore()); 60 | Assert.assertNull(RpcConfigManager.client_ssl_keystore_type()); 61 | Assert.assertEquals(TrustManagerFactory.getDefaultAlgorithm(), 62 | RpcConfigManager.client_ssl_tmf_algorithm()); 63 | } 64 | } 65 | --------------------------------------------------------------------------------