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> rows = template.queryForList(sql); + + if (rows.size() == 0) { + System.out.println("record doesn't exist!"); + return; + } else { + for (Map row : rows) { + System.out.println(Integer.parseInt(row.get("ID").toString())); + System.out.println((String) row.get("NAME")); + System.out.println(Integer.parseInt(row.get("AGE").toString())); + System.out.println(toString((Date) row.get("BIRTHDAY"))); + System.out.println("\n"); + } + } + } + + @Override + public List query() { + //your business logic code here! + System.out.println("jdbc Person query!"); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String sql = "select * from person"; + JdbcTemplate template = new JdbcTemplate(this.dataSource); + List> rows = template.queryForList(sql); + List list = new ArrayList(); + + for (Map row : rows) { + Person p = new Person(); + p.setId(Integer.parseInt(row.get("ID").toString())); + p.setName((String) row.get("NAME")); + p.setAge(Integer.parseInt(row.get("AGE").toString())); + p.setBirthday((Date) row.get("BIRTHDAY")); + list.add(p); + } + return list; + } +} + diff --git a/src/main/java/com/newlandframework/rpc/services/impl/PersonManageImpl.java b/src/main/java/com/newlandframework/rpc/services/impl/PersonManageImpl.java index be55258..27fe6a5 100644 --- a/src/main/java/com/newlandframework/rpc/services/impl/PersonManageImpl.java +++ b/src/main/java/com/newlandframework/rpc/services/impl/PersonManageImpl.java @@ -26,15 +26,32 @@ * @since 2016/11/7 */ public class PersonManageImpl implements PersonManage { + @Override public int save(Person p) { //your business logic code here! System.out.println("person data[" + p + "] has save!"); return 0; } + @Override public void query(Person p) { //your business logic code here! System.out.println("person data[" + p + "] has query!"); } + + @Override + public void check() { + throw new RuntimeException("person check fail!"); + } + + @Override + public boolean checkAge(Person p) { + if (p.getAge() < 18) { + throw new RuntimeException("person check age fail!"); + } else { + System.out.println("person check age succ!"); + return true; + } + } } diff --git a/src/main/java/com/newlandframework/rpc/services/pojo/Person.java b/src/main/java/com/newlandframework/rpc/services/pojo/Person.java index 5ae6cc5..5c51907 100644 --- a/src/main/java/com/newlandframework/rpc/services/pojo/Person.java +++ b/src/main/java/com/newlandframework/rpc/services/pojo/Person.java @@ -16,6 +16,7 @@ package com.newlandframework.rpc.services.pojo; import java.io.Serializable; +import java.util.Date; /** * @author tangjie @@ -28,8 +29,18 @@ public class Person implements Serializable { private int id; private String name; private int age; + private Date birthday; + + public Date getBirthday() { + return birthday; + } + + public void setBirthday(Date birthday) { + this.birthday = birthday; + } public int getId() { + return id; } @@ -54,7 +65,7 @@ public void setName(String name) { } public String toString() { - return String.format("Person <>", id, name, age); + return birthday != null ? String.format("Person <>", id, name, age, birthday) : String.format("Person <>", id, name, age); } } diff --git a/src/test/java/com/newlandframework/test/AsyncRpcCallErrorTest.java b/src/test/java/com/newlandframework/test/AsyncRpcCallErrorTest.java new file mode 100644 index 0000000..fccf90a --- /dev/null +++ b/src/test/java/com/newlandframework/test/AsyncRpcCallErrorTest.java @@ -0,0 +1,58 @@ +/** + * 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.test; + +import com.newlandframework.rpc.async.AsyncCallObject; +import com.newlandframework.rpc.async.AsyncCallback; +import com.newlandframework.rpc.async.AsyncInvoker; +import com.newlandframework.rpc.exception.AsyncCallException; +import com.newlandframework.rpc.services.CostTimeCalculate; +import com.newlandframework.rpc.services.pojo.CostTime; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author tangjie + * @filename:AsyncRpcCallErrorTest.java + * @description:AsyncRpcCallErrorTest功能模块 + * @blogs http://www.cnblogs.com/jietang/ + * @since 2017/9/26 + */ +public class AsyncRpcCallErrorTest { + public static void main(String[] args) { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:rpc-invoke-config-client.xml"); + + final CostTimeCalculate calculate = (CostTimeCalculate) context.getBean("costTime"); + + AsyncInvoker invoker = new AsyncInvoker(); + + try { + CostTime elapse0 = invoker.submit(new AsyncCallback() { + @Override + public CostTime call() { + throw new RuntimeException("calculate fail 1!"); + } + }); + + System.out.println("1 async nettyrpc call:[" + "result:" + elapse0 + ", status:[" + ((AsyncCallObject) elapse0)._getStatus() + "]"); + } catch (AsyncCallException e) { + System.out.println(e.getMessage()); + context.destroy(); + return; + } + + context.destroy(); + } +} diff --git a/src/test/java/com/newlandframework/test/PojoCallErrorTest.java b/src/test/java/com/newlandframework/test/PojoCallErrorTest.java new file mode 100644 index 0000000..9ce0713 --- /dev/null +++ b/src/test/java/com/newlandframework/test/PojoCallErrorTest.java @@ -0,0 +1,66 @@ +/** + * 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.test; + +import com.newlandframework.rpc.exception.InvokeModuleException; +import com.newlandframework.rpc.services.PersonManage; +import com.newlandframework.rpc.services.pojo.Person; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author tangjie + * @filename:PojoCallErrorTest.java + * @description:PojoCallErrorTest功能模块 + * @blogs http://www.cnblogs.com/jietang/ + * @since 2017/9/26 + */ +public class PojoCallErrorTest { + public static void test1(PersonManage manage) { + try { + manage.check(); + } catch (InvokeModuleException e) { + //e.printStackTrace(); + System.out.println(e.getMessage()); + } + } + + public static void test2(PersonManage manage) { + try { + Person p = new Person(); + p.setId(20150811); + p.setName("XiaoHaoBaby"); + p.setAge(1); + manage.checkAge(p); + } catch (InvokeModuleException e) { + //e.printStackTrace(); + System.out.println(e.getMessage()); + } + } + + public static void main(String[] args) { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:rpc-invoke-config-client.xml"); + + PersonManage manage = (PersonManage) context.getBean("personManage"); + + try { + test1(manage); + test2(manage); + } finally { + context.destroy(); + } + } +} + diff --git a/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcClientErrorTest.java b/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcClientErrorTest.java new file mode 100644 index 0000000..679017e --- /dev/null +++ b/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcClientErrorTest.java @@ -0,0 +1,54 @@ +/** + * 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.test.jdbc; + +import com.newlandframework.rpc.exception.InvokeModuleException; +import com.newlandframework.rpc.services.JdbcPersonManage; +import com.newlandframework.rpc.services.pojo.Person; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author tangjie + * @filename:NettyRpcJdbcClientErrorTest.java + * @description:NettyRpcJdbcClientErrorTest功能模块 + * @blogs http://www.cnblogs.com/jietang/ + * @since 2017/9/25 + */ +public class NettyRpcJdbcClientErrorTest { + // FIXME: 2017/9/25 确保先启动NettyRPC服务端应用:NettyRpcJdbcServerTest,再运行NettyRpcJdbcClientTest、NettyRpcJdbcClientErrorTest! + public static void main(String[] args) { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:rpc-invoke-config-jdbc-client.xml"); + + JdbcPersonManage manage = (JdbcPersonManage) context.getBean("personManageJdbc"); + + //验证RPC调用服务端执行失败的情况! + Person p = new Person(); + p.setId(20150811); + p.setName("XiaoHaoBaby"); + p.setAge(999999999); + + try { + int result = manage.save(p); + System.out.println("call pojo rpc result:" + result); + } catch (InvokeModuleException e) { + e.printStackTrace(); + System.out.println(e.getMessage()); + } finally { + context.destroy(); + } + } +} + diff --git a/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcClientTest.java b/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcClientTest.java new file mode 100644 index 0000000..c857fcf --- /dev/null +++ b/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcClientTest.java @@ -0,0 +1,63 @@ +/** + * 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.test.jdbc; + +import com.newlandframework.rpc.services.JdbcPersonManage; +import com.newlandframework.rpc.services.pojo.Person; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.List; + +/** + * @author tangjie + * @filename:NettyRpcJdbcClientTest.java + * @description:NettyRpcJdbcClientTest功能模块 + * @blogs http://www.cnblogs.com/jietang/ + * @since 2017/9/25 + */ +public class NettyRpcJdbcClientTest { + // FIXME: 2017/9/25 确保先启动NettyRPC服务端应用:NettyRpcJdbcServerTest,再运行NettyRpcJdbcClientTest、NettyRpcJdbcClientErrorTest! + public static void main(String[] args) { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:rpc-invoke-config-jdbc-client.xml"); + + JdbcPersonManage manage = (JdbcPersonManage) context.getBean("personManageJdbc"); + + try { + Person p = new Person(); + p.setId(1); + p.setName("小好"); + p.setAge(2); + p.setBirthday(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2015-08-11 16:47:00")); + int result = manage.save(p); + manage.query(p); + System.out.println("call pojo rpc result:" + result); + + System.out.println("---------------------------------------------"); + + List list = manage.query(); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } catch (ParseException e) { + e.printStackTrace(); + } finally { + context.destroy(); + } + } +} + diff --git a/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcServerTest.java b/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcServerTest.java new file mode 100644 index 0000000..65786be --- /dev/null +++ b/src/test/java/com/newlandframework/test/jdbc/NettyRpcJdbcServerTest.java @@ -0,0 +1,33 @@ +/** + * 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.test.jdbc; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author tangjie + * @filename:NettyRpcJdbcServerTest.java + * @description:NettyRpcJdbcServerTest功能模块 + * @blogs http://www.cnblogs.com/jietang/ + * @since 2017/9/25 + */ +public class NettyRpcJdbcServerTest { + // FIXME: 2017/9/25 确保先启动NettyRPC服务端应用:NettyRpcJdbcServerTest,再运行NettyRpcJdbcClientTest、NettyRpcJdbcClientErrorTest! + public static void main(String[] args) { + new ClassPathXmlApplicationContext("classpath:rpc-invoke-config-jdbc-server.xml"); + } +} + diff --git a/src/test/java/com/newlandframework/test/jdbc/create_table(oracle).sql b/src/test/java/com/newlandframework/test/jdbc/create_table(oracle).sql new file mode 100644 index 0000000..0cf81fd --- /dev/null +++ b/src/test/java/com/newlandframework/test/jdbc/create_table(oracle).sql @@ -0,0 +1,7 @@ +create table person +( +id number(12) not null, +name varchar2(32) not null, +age number(4), +birthday date +); \ No newline at end of file diff --git a/src/test/resources/rpc-invoke-config-jdbc-client.xml b/src/test/resources/rpc-invoke-config-jdbc-client.xml new file mode 100644 index 0000000..03bd34c --- /dev/null +++ b/src/test/resources/rpc-invoke-config-jdbc-client.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/rpc-invoke-config-jdbc-server.xml b/src/test/resources/rpc-invoke-config-jdbc-server.xml new file mode 100644 index 0000000..5557e2d --- /dev/null +++ b/src/test/resources/rpc-invoke-config-jdbc-server.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/rpc-server.properties b/src/test/resources/rpc-server.properties index 6990ca3..1f74da4 100644 --- a/src/test/resources/rpc-server.properties +++ b/src/test/resources/rpc-server.properties @@ -1,3 +1,6 @@ #rpc server's ip address config rpc.server.addr=127.0.0.1:18887 -#rpc.server.addr=10.1.8.5:18887 \ No newline at end of file +#rpc.server.addr=10.1.8.5:18887 + +rpc.server.echo.api.port=18886 +