parent
a21dd29e01
commit
ab19a7e43c
@ -0,0 +1,85 @@ |
|||||||
|
package core.thrift; |
||||||
|
|
||||||
|
import org.apache.commons.pool2.impl.GenericObjectPool; |
||||||
|
import org.apache.thrift.TServiceClient; |
||||||
|
import org.apache.thrift.async.TAsyncClient; |
||||||
|
import org.apache.thrift.async.TAsyncClientManager; |
||||||
|
import org.apache.thrift.protocol.TBinaryProtocol; |
||||||
|
import org.apache.thrift.protocol.TProtocol; |
||||||
|
import org.apache.thrift.protocol.TProtocolFactory; |
||||||
|
import org.apache.thrift.transport.TNonblockingSocket; |
||||||
|
import org.apache.thrift.transport.TNonblockingTransport; |
||||||
|
import org.apache.thrift.transport.TSocket; |
||||||
|
import org.apache.thrift.transport.TTransport; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import javax.annotation.PostConstruct; |
||||||
|
import java.lang.reflect.Constructor; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
@Component |
||||||
|
public class SocketConfig { |
||||||
|
@Value("${thrift.ip}") |
||||||
|
private String ip; |
||||||
|
|
||||||
|
@Value("${thrift.port}") |
||||||
|
private Integer port; |
||||||
|
|
||||||
|
@Value("${thrift.timeout}") |
||||||
|
private Integer timeout; |
||||||
|
|
||||||
|
|
||||||
|
private GenericObjectPool<TNonblockingSocket> tNonblockingSocketPool; |
||||||
|
|
||||||
|
private GenericObjectPool<TSocket> tSocketPool; |
||||||
|
|
||||||
|
|
||||||
|
private final Map<Class, Constructor> clientMap = new HashMap<>(); |
||||||
|
|
||||||
|
@PostConstruct |
||||||
|
public void init(){ |
||||||
|
tNonblockingSocketPool=new GenericObjectPool<>(new TNonblockingSocketPool(ip,port,timeout)); |
||||||
|
tSocketPool=new GenericObjectPool<>(new TSocketPool(ip,port,timeout)); |
||||||
|
} |
||||||
|
|
||||||
|
public <C extends TAsyncClient> void doExecute(Class<C> c, AsyncClient<C> thrift) throws Exception { |
||||||
|
//从池里获取一个Transport对象
|
||||||
|
TNonblockingSocket trans = tNonblockingSocketPool.borrowObject(); |
||||||
|
thrift.doExecute(client(c, trans)); |
||||||
|
//把一个Transport对象归还到池里
|
||||||
|
tNonblockingSocketPool.returnObject(trans); |
||||||
|
} |
||||||
|
|
||||||
|
public <C extends TServiceClient> void doExecute(Class<C> c,ServiceClient<C> thrift) throws Exception{ |
||||||
|
TSocket trans=tSocketPool.borrowObject(); |
||||||
|
thrift.doExecute(client(c,trans)); |
||||||
|
tSocketPool.returnObject(trans); |
||||||
|
} |
||||||
|
|
||||||
|
private interface Thrift<E> { |
||||||
|
void doExecute(E client) throws Exception; |
||||||
|
} |
||||||
|
|
||||||
|
public interface AsyncClient<E extends TAsyncClient> extends Thrift<E>{} |
||||||
|
|
||||||
|
public interface ServiceClient<E extends TServiceClient> extends Thrift<E>{} |
||||||
|
|
||||||
|
public <T extends TTransport,C> C client(Class<C> c, T trans) throws Exception { |
||||||
|
if (trans instanceof TNonblockingTransport) { |
||||||
|
if (!clientMap.containsKey(c)) { |
||||||
|
clientMap.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class)); |
||||||
|
} |
||||||
|
TProtocolFactory protocol = new TBinaryProtocol.Factory(); |
||||||
|
TAsyncClientManager clientManager = new TAsyncClientManager(); |
||||||
|
return (C) clientMap.get(c).newInstance(protocol, clientManager, trans); |
||||||
|
} else { |
||||||
|
if (!clientMap.containsKey(c)) { |
||||||
|
clientMap.put(c, c.getConstructor(TProtocol.class)); |
||||||
|
} |
||||||
|
TBinaryProtocol protocol = new TBinaryProtocol(trans); |
||||||
|
return (C) clientMap.get(c).newInstance(protocol); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
package core.thrift; |
||||||
|
|
||||||
|
import org.apache.thrift.async.TAsyncClient; |
||||||
|
import org.apache.thrift.transport.TNonblockingSocket; |
||||||
|
|
||||||
|
import java.net.Socket; |
||||||
|
|
||||||
|
public class TNonblockingSocketPool extends TTransportPool<TNonblockingSocket,TAsyncClient> { |
||||||
|
|
||||||
|
|
||||||
|
public TNonblockingSocketPool(String ip, Integer port, Integer timeout) { |
||||||
|
super(ip, port, timeout); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Socket socket(TNonblockingSocket o) { |
||||||
|
return o.getSocketChannel().socket(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public TNonblockingSocket create() throws Exception { |
||||||
|
return new TNonblockingSocket(ip,port,timeout); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
package core.thrift; |
||||||
|
|
||||||
|
import org.apache.thrift.TServiceClient; |
||||||
|
import org.apache.thrift.transport.TSocket; |
||||||
|
|
||||||
|
import java.net.Socket; |
||||||
|
|
||||||
|
public class TSocketPool extends TTransportPool<TSocket,TServiceClient>{ |
||||||
|
|
||||||
|
|
||||||
|
public TSocketPool(String ip, Integer port, Integer timeout) { |
||||||
|
super(ip, port, timeout); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Socket socket(TSocket o) { |
||||||
|
return o.getSocket(); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public TSocket create() throws Exception { |
||||||
|
TSocket socket=new TSocket(ip,port,timeout); |
||||||
|
socket.open(); |
||||||
|
return socket; |
||||||
|
} |
||||||
|
} |
@ -1,70 +0,0 @@ |
|||||||
package core.thrift; |
|
||||||
|
|
||||||
import org.apache.commons.pool2.impl.GenericObjectPool; |
|
||||||
import org.apache.thrift.async.TAsyncClient; |
|
||||||
import org.apache.thrift.async.TAsyncClientManager; |
|
||||||
import org.apache.thrift.protocol.TBinaryProtocol; |
|
||||||
import org.apache.thrift.protocol.TProtocolFactory; |
|
||||||
import org.apache.thrift.transport.TNonblockingSocket; |
|
||||||
import org.apache.thrift.transport.TNonblockingTransport; |
|
||||||
import org.springframework.beans.factory.annotation.Value; |
|
||||||
import org.springframework.stereotype.Service; |
|
||||||
|
|
||||||
import javax.annotation.PostConstruct; |
|
||||||
import java.io.IOException; |
|
||||||
import java.lang.reflect.Constructor; |
|
||||||
import java.lang.reflect.InvocationTargetException; |
|
||||||
import java.util.HashMap; |
|
||||||
import java.util.Map; |
|
||||||
|
|
||||||
@Service |
|
||||||
public class ThriftClientPool { |
|
||||||
|
|
||||||
@Value("${thrift.ip}") |
|
||||||
private String ip; |
|
||||||
|
|
||||||
@Value("${thrift.port}") |
|
||||||
private Integer port; |
|
||||||
|
|
||||||
@Value("${thrift.timeout}") |
|
||||||
private Integer timeout; |
|
||||||
|
|
||||||
private static GenericObjectPool<TNonblockingSocket> pool; |
|
||||||
|
|
||||||
private static final Map<Class<? extends TAsyncClient>, Constructor> asyncClient = new HashMap<>(); |
|
||||||
|
|
||||||
private static final Map<Class<? extends TAsyncClient>, Constructor> synchronizeClient = new HashMap<>(); |
|
||||||
|
|
||||||
@PostConstruct |
|
||||||
public void init() { |
|
||||||
pool = new GenericObjectPool<>(new ConnectionPoolFactory(ip, port, timeout)); |
|
||||||
} |
|
||||||
|
|
||||||
public static <E extends TAsyncClient> void doExecute(Thrift<E> thrift,Class<E> c) throws Exception { |
|
||||||
//从池里获取一个Transport对象
|
|
||||||
TNonblockingSocket socket = pool.borrowObject(); |
|
||||||
thrift.doExecute(client(c,socket)); |
|
||||||
//把一个Transport对象归还到池里
|
|
||||||
pool.returnObject(socket); |
|
||||||
} |
|
||||||
|
|
||||||
private static <E extends TAsyncClient> E client(Class<E> c, TNonblockingSocket socket) throws NoSuchMethodException, IOException, IllegalAccessException, InvocationTargetException, InstantiationException { |
|
||||||
if (!asyncClient.containsKey(c)) { |
|
||||||
asyncClient.put(c, c.getConstructor(TProtocolFactory.class, TAsyncClientManager.class, TNonblockingTransport.class)); |
|
||||||
} |
|
||||||
TAsyncClientManager clientManager = new TAsyncClientManager(); |
|
||||||
TProtocolFactory protocol = new TBinaryProtocol.Factory(); |
|
||||||
return (E) asyncClient.get(c).newInstance(protocol,clientManager,socket); |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
public interface Thrift<E extends TAsyncClient> { |
|
||||||
void doExecute(E client) throws Exception; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,14 +0,0 @@ |
|||||||
package db.config; |
|
||||||
|
|
||||||
import org.hibernate.EmptyInterceptor; |
|
||||||
|
|
||||||
public class MyEmptyInterceptor extends EmptyInterceptor { |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override |
|
||||||
public String onPrepareStatement(String sql) { |
|
||||||
return super.onPrepareStatement(sql); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,18 @@ |
|||||||
|
--菜单表 |
||||||
|
create table menu |
||||||
|
( |
||||||
|
id int auto_increment--自增主键 |
||||||
|
primary key, |
||||||
|
href varchar(32) not null,--菜单超链接 |
||||||
|
menu_name varchar(10) not null,--菜单名 |
||||||
|
icon varchar(20) null,--菜单图标 |
||||||
|
parent_id int not null,--父菜单id |
||||||
|
menu_level int not null,--菜单树深度 |
||||||
|
sort int not null,--排序 |
||||||
|
constraint menu_href_uindex |
||||||
|
unique (href) |
||||||
|
) |
||||||
|
engine=InnoDB |
||||||
|
; |
||||||
|
|
||||||
|
|
@ -1,4 +1,5 @@ |
|||||||
public class MainTest { |
public class MainTest { |
||||||
public static void main(String[] args) { |
public static void main(String[] args) { |
||||||
|
|
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue