您的位置:首页 > 编程语言 > Java开发

Thrift 示例(包含自定义异常, 枚举, 传输Java Bean)

2016-03-29 20:53 597 查看
编写demoHello.thrift

namespace java com.micmiu.thrift.demo

enum RequestType{
SAY_HELLO,
QUERY_TIME
}
struct Request{
1: required RequestType type;
2: required string name;
3: optional i32 age;
}

exception RequestException{
1: required i32 code;
2: optional string reson;
}

service HelloWorldService{
string doAction(1: Request request) throws (1: RequestException qe);
}


2.生成相关的Java类

.\thrift-0.8.0.exe -r -gen java .\demoHello.thrift


可以看到枚举, 异常, Service都生成了,

生成的文件列表如下:

HelloWorldService.java
Request.java
RequestException.java
RequestType.java


3.编写服务端的doAction实现

package com.micmiu.thrift.demo;

import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;

import java.util.Date;

/**
* Created by yinliang on 2016/3/29.
*/
public class HelloWordServiceImpl implements HelloWorldService.Iface {
public String doAction(Request request) throws RequestException, TException {
System.out.println("Get request: " + request);
if (StringUtils.isBlank(request.getName()) || request.getType() == null) {
throw new RequestException();
}

String result = "Hello " + request.getName();

if (request.getType() == RequestType.SAY_HELLO) {
result += ", Welcome";
} else {
result += ", Now is " + new Date().toLocaleString();
}
return result;
}
}


4.编写服务端

package com.micmiu.thrift.demo;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

import java.io.IOException;
import java.net.ServerSocket;

/**
* Created by yinliang on 2016/3/29.
*/
public class HelloWordServer {
public static final int SERVER_PORT = 8090;

public static void main(String[] args) {
System.out.println("Runing server...");

try {
TProcessor tProcessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWordServiceImpl());
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tProcessor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
TServer server = new TSimpleServer(tArgs);

server.serve();
} catch (TTransportException e) {
System.out.println("Server start error!!!");
}
}
}


5.编写客户端

package com.micmiu.thrift.demo;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
* Created by yinliang on 2016/3/29.
*/
public class HelloWordClient {
public static final String SERVER_IP = "localhost";
public static final int SERVER_PORT = 8090;
public static final int TIMEOUT = 30000;

public static void main(String[] args) {
TTransport transport = null;
try {
transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
TProtocol protocol = new TBinaryProtocol(transport);
HelloWorldService.Client client = new HelloWorldService.Client(protocol);

transport.open();
Request request = new Request();
request.setAge(22);
request.setName("zhangsan");
request.setType(RequestType.SAY_HELLO);
String result = client.doAction(request);
System.out.println(result);

request.setType(RequestType.QUERY_TIME);
result = client.doAction(request);
System.out.println(result);

transport.close();

} catch (Exception e) {

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: