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 |
--------------------------------------------------------------------------------