use of org.tech.model.impl.HelloImpl in project tech by ffyyhh995511.
the class Test3 method main.
public static void main(String[] args2) {
try {
// 非阻塞式的,配合TFramedTransport使用
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
// 关联处理器与Service服务的实现
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloImpl());
// 目前Thrift提供的最高级的模式,可并发处理客户端请求
TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(serverTransport);
args.processor(processor);
// 设置协议工厂,高效率的、密集的二进制编码格式进行数据传输协议
args.protocolFactory(new TCompactProtocol.Factory());
// 设置传输工厂,使用非阻塞方式,按块的大小进行传输,类似于Java中的NIO
args.transportFactory(new TFramedTransport.Factory());
// 设置处理器工厂,只返回一个单例实例
args.processorFactory(new TProcessorFactory(processor));
// 多个线程,主要负责客户端的IO处理
args.selectorThreads(2);
// 工作线程池
ExecutorService pool = Executors.newFixedThreadPool(3);
args.executorService(pool);
TThreadedSelectorServer server = new TThreadedSelectorServer(args);
System.out.println("Starting server on port " + 7911 + "......");
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.tech.model.impl.HelloImpl in project tech by ffyyhh995511.
the class ThriftDemo method nioServer.
/**
* 基于非阻塞IO(NIO)的服务端
*/
public void nioServer() {
try {
// 传输通道 - 非阻塞方式
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
// 异步IO,需要使用TFramedTransport,它将分块缓存读取。
TTransportFactory transportFactory = new TFramedTransport.Factory();
// 使用高密度二进制协议
TProtocolFactory proFactory = new TCompactProtocol.Factory();
// 设置处理器 HelloImpl
TProcessor processor = new Hello.Processor(new HelloImpl());
// 创建服务器
Args tArgs = new Args(serverTransport);
tArgs.transportFactory(transportFactory);
tArgs.protocolFactory(proFactory);
tArgs.processor(processor);
TServer server = new TThreadedSelectorServer(tArgs);
System.out.println("Start server on port 7911...");
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.tech.model.impl.HelloImpl in project tech by ffyyhh995511.
the class Test1 method main.
/**
* 编写服务端,发布(阻塞式IO + 多线程处理)服务
*
* @param args
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
try {
// 设置传输通道,普通通道
TServerTransport serverTransport = new TServerSocket(7911);
// 使用高密度二进制协议
TProtocolFactory proFactory = new TCompactProtocol.Factory();
// 设置处理器HelloImpl
TProcessor processor = new Hello.Processor(new HelloImpl());
// 创建服务器
Args args2 = new Args(serverTransport);
args2.protocolFactory(proFactory);
args2.processor(processor);
TServer server = new TThreadPoolServer(args2);
System.out.println("Start server on port 7911...");
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.tech.model.impl.HelloImpl in project tech by ffyyhh995511.
the class Test2 method main.
public static void main(String[] args) {
try {
// 传输通道 - 非阻塞方式
TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(7911);
// 异步IO,需要使用TFramedTransport,它将分块缓存读取。
TTransportFactory transportFactory = new TFramedTransport.Factory();
// 使用高密度二进制协议
TProtocolFactory proFactory = new TCompactProtocol.Factory();
// 设置处理器 HelloImpl
TProcessor processor = new Hello.Processor(new HelloImpl());
// 创建服务器
Args tArgs = new Args(serverTransport);
tArgs.transportFactory(transportFactory);
tArgs.protocolFactory(proFactory);
tArgs.processor(processor);
TServer server = new TThreadedSelectorServer(tArgs);
System.out.println("Start server on port 7911...");
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations