From fc61f65efbcecf5de51c565035f688adeafe3f0a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=94=90=E6=B4=81?= <13799369482@139.com>
Date: Tue, 26 Sep 2017 11:28:56 +0800
Subject: [PATCH] JDBC operation example in NettyRPC by tangjie
JDBC operation example in NettyRPC by tangjie
---
.../rpc/core/MessageCallBack.java | 7 +-
.../rpc/core/RpcSystemConfig.java | 4 +
.../rpc/exception/InvokeModuleException.java | 42 +++++++
.../rpc/netty/MessageRecvHandler.java | 1 +
.../rpc/netty/MessageRecvInitializeTask.java | 11 +-
.../hessian/HessianSerializePool.java | 7 +-
.../ProtostuffSerializeFactory.java | 4 +-
.../protostuff/ProtostuffSerializePool.java | 8 +-
.../rpc/services/JdbcPersonManage.java | 37 ++++++
.../rpc/services/PersonManage.java | 4 +
.../services/impl/JdbcPersonManageImpl.java | 112 ++++++++++++++++++
.../rpc/services/impl/PersonManageImpl.java | 17 +++
.../rpc/services/pojo/Person.java | 13 +-
.../test/AsyncRpcCallErrorTest.java | 58 +++++++++
.../test/PojoCallErrorTest.java | 66 +++++++++++
.../jdbc/NettyRpcJdbcClientErrorTest.java | 54 +++++++++
.../test/jdbc/NettyRpcJdbcClientTest.java | 63 ++++++++++
.../test/jdbc/NettyRpcJdbcServerTest.java | 33 ++++++
.../test/jdbc/create_table(oracle).sql | 7 ++
.../rpc-invoke-config-jdbc-client.xml | 27 +++++
.../rpc-invoke-config-jdbc-server.xml | 58 +++++++++
src/test/resources/rpc-server.properties | 5 +-
22 files changed, 629 insertions(+), 9 deletions(-)
create mode 100644 src/main/java/com/newlandframework/rpc/exception/InvokeModuleException.java
create mode 100644 src/main/java/com/newlandframework/rpc/services/JdbcPersonManage.java
create mode 100644 src/main/java/com/newlandframework/rpc/services/impl/JdbcPersonManageImpl.java
create mode 100644 src/test/java/com/newlandframework/test/AsyncRpcCallErrorTest.java
create mode 100644 src/test/java/com/newlandframework/test/PojoCallErrorTest.java
create mode 100644 src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcClientErrorTest.java
create mode 100644 src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcClientTest.java
create mode 100644 src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcServerTest.java
create mode 100644 src/test/java/com/newlandframework/test/jdbc/create_table(oracle).sql
create mode 100644 src/test/resources/rpc-invoke-config-jdbc-client.xml
create mode 100644 src/test/resources/rpc-invoke-config-jdbc-server.xml
diff --git a/src/main/java/com/newlandframework/rpc/core/MessageCallBack.java b/src/main/java/com/newlandframework/rpc/core/MessageCallBack.java
index e15e71c..780552f 100644
--- a/src/main/java/com/newlandframework/rpc/core/MessageCallBack.java
+++ b/src/main/java/com/newlandframework/rpc/core/MessageCallBack.java
@@ -20,6 +20,7 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
+import com.newlandframework.rpc.exception.InvokeModuleException;
import com.newlandframework.rpc.exception.RejectResponeException;
import com.newlandframework.rpc.model.MessageRequest;
import com.newlandframework.rpc.model.MessageResponse;
@@ -48,7 +49,11 @@ public Object start() throws InterruptedException, RejectResponeException {
finish.await(RpcSystemConfig.SYSTEM_PROPERTY_MESSAGE_CALLBACK_TIMEOUT, TimeUnit.MILLISECONDS);
if (this.response != null) {
if (!this.response.getError().equals(RpcSystemConfig.FILTER_RESPONSE_MSG) && (!this.response.isReturnNotNull() || (this.response.isReturnNotNull() && this.response.getResult() != null))) {
- return this.response.getResult();
+ if (this.response.getError().isEmpty()) {
+ return this.response.getResult();
+ } else {
+ throw new InvokeModuleException(this.response.getError());
+ }
} else {
throw new RejectResponeException(RpcSystemConfig.FILTER_RESPONSE_MSG);
}
diff --git a/src/main/java/com/newlandframework/rpc/core/RpcSystemConfig.java b/src/main/java/com/newlandframework/rpc/core/RpcSystemConfig.java
index a600078..2a8ae74 100644
--- a/src/main/java/com/newlandframework/rpc/core/RpcSystemConfig.java
+++ b/src/main/java/com/newlandframework/rpc/core/RpcSystemConfig.java
@@ -35,6 +35,10 @@ public class RpcSystemConfig {
public static final String RPC_COMPILER_SPI_ATTR = "com.newlandframework.rpc.compiler.AccessAdaptive";
public static final String RPC_ABILITY_DETAIL_SPI_ATTR = "com.newlandframework.rpc.core.AbilityDetail";
public static final String FILTER_RESPONSE_MSG = "Illegal request,NettyRPC server refused to respond!";
+ public static final int SERIALIZE_POOL_MAX_TOTAL = 500;
+ public static final int SERIALIZE_POOL_MIN_IDLE = 10;
+ public static final int SERIALIZE_POOL_MAX_WAIT_MILLIS = 5000;
+ public static final int SERIALIZE_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS = 600000;
private static boolean monitorServerSupport = false;
diff --git a/src/main/java/com/newlandframework/rpc/exception/InvokeModuleException.java b/src/main/java/com/newlandframework/rpc/exception/InvokeModuleException.java
new file mode 100644
index 0000000..eaaa2e9
--- /dev/null
+++ b/src/main/java/com/newlandframework/rpc/exception/InvokeModuleException.java
@@ -0,0 +1,42 @@
+/**
+ * Copyright (C) 2017 Newland Group Holding Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.newlandframework.rpc.exception;
+
+/**
+ * @author tangjie
+ * @filename:InvokeModuleException.java
+ * @description:InvokeModuleException功能模块
+ * @blogs http://www.cnblogs.com/jietang/
+ * @since 2017/9/26
+ */
+public class InvokeModuleException extends RuntimeException {
+ public InvokeModuleException() {
+ super();
+ }
+
+ public InvokeModuleException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public InvokeModuleException(String message) {
+ super(message);
+ }
+
+ public InvokeModuleException(Throwable cause) {
+ super(cause);
+ }
+}
+
diff --git a/src/main/java/com/newlandframework/rpc/netty/MessageRecvHandler.java b/src/main/java/com/newlandframework/rpc/netty/MessageRecvHandler.java
index fd4a5f8..0cca32d 100644
--- a/src/main/java/com/newlandframework/rpc/netty/MessageRecvHandler.java
+++ b/src/main/java/com/newlandframework/rpc/netty/MessageRecvHandler.java
@@ -46,6 +46,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+ cause.printStackTrace();
ctx.close();
}
}
diff --git a/src/main/java/com/newlandframework/rpc/netty/MessageRecvInitializeTask.java b/src/main/java/com/newlandframework/rpc/netty/MessageRecvInitializeTask.java
index d8b19c2..eb31faf 100644
--- a/src/main/java/com/newlandframework/rpc/netty/MessageRecvInitializeTask.java
+++ b/src/main/java/com/newlandframework/rpc/netty/MessageRecvInitializeTask.java
@@ -21,6 +21,8 @@
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
+import java.io.PrintWriter;
+import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.Callable;
@@ -60,7 +62,7 @@ public Boolean call() {
}
return Boolean.TRUE;
} catch (Throwable t) {
- response.setError(t.toString());
+ response.setError(getStackTrace(t));
t.printStackTrace();
System.err.printf("RPC Server invoke error!\n");
return Boolean.FALSE;
@@ -79,6 +81,13 @@ private Object reflect(MessageRequest request) throws Throwable {
return obj;
}
+ public String getStackTrace(Throwable ex) {
+ StringWriter buf = new StringWriter();
+ ex.printStackTrace(new PrintWriter(buf));
+
+ return buf.toString();
+ }
+
public boolean isReturnNotNull() {
return returnNotNull;
}
diff --git a/src/main/java/com/newlandframework/rpc/serialize/hessian/HessianSerializePool.java b/src/main/java/com/newlandframework/rpc/serialize/hessian/HessianSerializePool.java
index c82bf35..72f6228 100644
--- a/src/main/java/com/newlandframework/rpc/serialize/hessian/HessianSerializePool.java
+++ b/src/main/java/com/newlandframework/rpc/serialize/hessian/HessianSerializePool.java
@@ -18,6 +18,11 @@
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
+import static com.newlandframework.rpc.core.RpcSystemConfig.SERIALIZE_POOL_MAX_TOTAL;
+import static com.newlandframework.rpc.core.RpcSystemConfig.SERIALIZE_POOL_MIN_IDLE;
+import static com.newlandframework.rpc.core.RpcSystemConfig.SERIALIZE_POOL_MAX_WAIT_MILLIS;
+import static com.newlandframework.rpc.core.RpcSystemConfig.SERIALIZE_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS;
+
/**
* @author tangjie
* @filename:HessianSerializePool.java
@@ -38,7 +43,7 @@ public static HessianSerializePool getHessianPoolInstance() {
if (poolFactory == null) {
synchronized (HessianSerializePool.class) {
if (poolFactory == null) {
- poolFactory = new HessianSerializePool();
+ poolFactory = new HessianSerializePool(SERIALIZE_POOL_MAX_TOTAL, SERIALIZE_POOL_MIN_IDLE, SERIALIZE_POOL_MAX_WAIT_MILLIS, SERIALIZE_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS);
}
}
}
diff --git a/src/main/java/com/newlandframework/rpc/serialize/protostuff/ProtostuffSerializeFactory.java b/src/main/java/com/newlandframework/rpc/serialize/protostuff/ProtostuffSerializeFactory.java
index aca8a44..546ba50 100644
--- a/src/main/java/com/newlandframework/rpc/serialize/protostuff/ProtostuffSerializeFactory.java
+++ b/src/main/java/com/newlandframework/rpc/serialize/protostuff/ProtostuffSerializeFactory.java
@@ -32,8 +32,8 @@ public ProtostuffSerialize create() throws Exception {
return createProtostuff();
}
- public PooledObject wrap(ProtostuffSerialize hessian) {
- return new DefaultPooledObject(hessian);
+ public PooledObject wrap(ProtostuffSerialize protostuff) {
+ return new DefaultPooledObject(protostuff);
}
private ProtostuffSerialize createProtostuff() {
diff --git a/src/main/java/com/newlandframework/rpc/serialize/protostuff/ProtostuffSerializePool.java b/src/main/java/com/newlandframework/rpc/serialize/protostuff/ProtostuffSerializePool.java
index 0ac1a5f..0882012 100644
--- a/src/main/java/com/newlandframework/rpc/serialize/protostuff/ProtostuffSerializePool.java
+++ b/src/main/java/com/newlandframework/rpc/serialize/protostuff/ProtostuffSerializePool.java
@@ -18,6 +18,11 @@
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
+import static com.newlandframework.rpc.core.RpcSystemConfig.SERIALIZE_POOL_MAX_TOTAL;
+import static com.newlandframework.rpc.core.RpcSystemConfig.SERIALIZE_POOL_MIN_IDLE;
+import static com.newlandframework.rpc.core.RpcSystemConfig.SERIALIZE_POOL_MAX_WAIT_MILLIS;
+import static com.newlandframework.rpc.core.RpcSystemConfig.SERIALIZE_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS;
+
/**
* @author tangjie
* @filename:ProtostuffSerializePool.java
@@ -26,7 +31,6 @@
* @since 2016/10/7
*/
public class ProtostuffSerializePool {
-
private GenericObjectPool ProtostuffPool;
private static volatile ProtostuffSerializePool poolFactory = null;
@@ -38,7 +42,7 @@ public static ProtostuffSerializePool getProtostuffPoolInstance() {
if (poolFactory == null) {
synchronized (ProtostuffSerializePool.class) {
if (poolFactory == null) {
- poolFactory = new ProtostuffSerializePool();
+ poolFactory = new ProtostuffSerializePool(SERIALIZE_POOL_MAX_TOTAL, SERIALIZE_POOL_MIN_IDLE, SERIALIZE_POOL_MAX_WAIT_MILLIS, SERIALIZE_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS);
}
}
}
diff --git a/src/main/java/com/newlandframework/rpc/services/JdbcPersonManage.java b/src/main/java/com/newlandframework/rpc/services/JdbcPersonManage.java
new file mode 100644
index 0000000..22aeee8
--- /dev/null
+++ b/src/main/java/com/newlandframework/rpc/services/JdbcPersonManage.java
@@ -0,0 +1,37 @@
+/**
+ * Copyright (C) 2017 Newland Group Holding Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.newlandframework.rpc.services;
+
+import com.newlandframework.rpc.services.pojo.Person;
+
+import java.util.List;
+
+/**
+ * @author tangjie
+ * @filename:JdbcPersonManage.java
+ * @description:JdbcPersonManage功能模块
+ * @blogs http://www.cnblogs.com/jietang/
+ * @since 2017/9/25
+ */
+public interface JdbcPersonManage {
+ int save(Person p);
+
+ void query(Person p);
+
+ List query();
+}
+
+
diff --git a/src/main/java/com/newlandframework/rpc/services/PersonManage.java b/src/main/java/com/newlandframework/rpc/services/PersonManage.java
index 73eaaa9..2f77096 100644
--- a/src/main/java/com/newlandframework/rpc/services/PersonManage.java
+++ b/src/main/java/com/newlandframework/rpc/services/PersonManage.java
@@ -28,4 +28,8 @@ public interface PersonManage {
int save(Person p);
void query(Person p);
+
+ void check();
+
+ boolean checkAge(Person p);
}
diff --git a/src/main/java/com/newlandframework/rpc/services/impl/JdbcPersonManageImpl.java b/src/main/java/com/newlandframework/rpc/services/impl/JdbcPersonManageImpl.java
new file mode 100644
index 0000000..dd82a58
--- /dev/null
+++ b/src/main/java/com/newlandframework/rpc/services/impl/JdbcPersonManageImpl.java
@@ -0,0 +1,112 @@
+/**
+ * Copyright (C) 2017 Newland Group Holding Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.newlandframework.rpc.services.impl;
+
+import com.newlandframework.rpc.services.JdbcPersonManage;
+import com.newlandframework.rpc.services.pojo.Person;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.sql.DataSource;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author tangjie
+ * @filename:JdbcPersonManageImpl.java
+ * @description:JdbcPersonManageImpl功能模块
+ * @blogs http://www.cnblogs.com/jietang/
+ * @since 2017/9/25
+ */
+@Service
+public class JdbcPersonManageImpl implements JdbcPersonManage {
+ private DataSource dataSource;
+
+ public void setDataSource(DataSource dataSource) {
+ this.dataSource = dataSource;
+ }
+
+ private String toString(Date date) {
+ if (date == null) {
+ return null;
+ }
+ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ return format.format(date);
+ }
+
+ @Transactional
+ @Override
+ public int save(Person p) {
+ //your business logic code here!
+ System.out.println("jdbc Person data[" + p + "] has save!");
+ System.out.println(p);
+ String sql = "insert into person(id,name,age,birthday) values(?,?,?,to_date(?,'yyyy-mm-dd hh24:mi:ss'))";
+ System.out.println(sql);
+ JdbcTemplate template = new JdbcTemplate(this.dataSource);
+ template.update(sql, p.getId(), p.getName(), p.getAge(), toString(p.getBirthday()));
+
+ return 0;
+ }
+
+ @Override
+ public void query(Person p) {
+ //your business logic code here!
+ System.out.println("jdbc Person data[" + p + "] has query!");
+ String sql = String.format("select * from person where id = %d", p.getId());
+ JdbcTemplate template = new JdbcTemplate(this.dataSource);
+ List