Skip to content

Commit

Permalink
JDBC operation example in NettyRPC by tangjie
Browse files Browse the repository at this point in the history
JDBC operation example in NettyRPC by tangjie
  • Loading branch information
tang-jie committed Sep 26, 2017
1 parent 4e02322 commit fc61f65
Show file tree
Hide file tree
Showing 22 changed files with 629 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (C) 2017 Newland Group Holding Limited
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<https://github.com/tang-jie>
* @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);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}

public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<https://github.com/tang-jie>
* @filename:HessianSerializePool.java
Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public ProtostuffSerialize create() throws Exception {
return createProtostuff();
}

public PooledObject<ProtostuffSerialize> wrap(ProtostuffSerialize hessian) {
return new DefaultPooledObject<ProtostuffSerialize>(hessian);
public PooledObject<ProtostuffSerialize> wrap(ProtostuffSerialize protostuff) {
return new DefaultPooledObject<ProtostuffSerialize>(protostuff);
}

private ProtostuffSerialize createProtostuff() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<https://github.com/tang-jie>
* @filename:ProtostuffSerializePool.java
Expand All @@ -26,7 +31,6 @@
* @since 2016/10/7
*/
public class ProtostuffSerializePool {

private GenericObjectPool<ProtostuffSerialize> ProtostuffPool;
private static volatile ProtostuffSerializePool poolFactory = null;

Expand All @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2017 Newland Group Holding Limited
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<https://github.com/tang-jie>
* @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<Person> query();
}


Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ public interface PersonManage {
int save(Person p);

void query(Person p);

void check();

boolean checkAge(Person p);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Copyright (C) 2017 Newland Group Holding Limited
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<https://github.com/tang-jie>
* @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<Map<String, Object>> 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<Person> 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<Map<String, Object>> rows = template.queryForList(sql);
List<Person> list = new ArrayList<Person>();

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

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Loading

0 comments on commit fc61f65

Please sign in to comment.