The Apache Thrift software framework, for scalable cross-language services development, combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other languages.
publicclassAccountServiceimplementsAccount.Iface{ privatestatic Map<String, String> namePw = new HashMap<>(); privatestatic Map<String, Integer> nameId = new HashMap<>(); privatestatic Map<String, Operation> nameOp = new HashMap<>(); privatestatic Map<Integer, String> idname = new HashMap<>();
@Override publicvoidaddUser(User user)throws TException { int id = user.getUserId(); String name = user.getUsername(); String pass = user.getPassword(); Operation op = user.getOp();
if (name == null || name.length() == 0) { thrownew InvalidOperation(100, "The name should not be empty!"); } if (namePw.containsKey(name)) { thrownew InvalidOperation(101, "The name has been used, please change the name!"); } if (nameId.containsValue(id) || id <= 0) { thrownew InvalidOperation(102, "The id has been used or the id is invalid, please change the id!"); }
@Override public User queryUser(int id)throws TException { System.out.println(id); if (idname.containsKey(id)) { User user = new User(); user.userId = id; String name = idname.get(id); user.username = name; user.password = namePw.get(name); user.op = nameOp.get(name); return user; } else { System.out.println("The id: " + id + " does not exist!"); User user = new User(); user.userId = 0; user.username = ""; user.password = "123456"; user.op = Operation.LOGIN; return user; } }
@Override public List<User> queryUserList()throws TException { List<User> list = new ArrayList<User>(); for (String name : namePw.keySet()) { User user = new User(); user.userId = nameId.get(name); user.username = name; user.password = namePw.get(name); user.op = nameOp.get(name); list.add(user); } return list; } }
publicclassAccountClient{ publicstatic String ip = "localhost"; publicstaticint port = 8090; publicstaticint time_out = 30000;
/** * 使用阻塞式socket * * @author matt * @since Apr 7, 2016 * @throws 无 * void */ publicstaticvoidstartSimpleClient(){ TTransport transport = null; try { // 创建Transport transport = new TSocket(ip, port, time_out); // 创建TProtocol TProtocol protocol = new TBinaryProtocol(transport); // 基于TTransport和TProtocol创建Client Account.Client client = new Account.Client(protocol); transport.open();
// 正常添加用户 User user1 = new User(001, "matt1", "123456", Operation.REGISTER); client.addUser(user1); User user2 = new User(002, "matt2", "123456", Operation.REGISTER); client.addUser(user2); User user3 = new User(003, "matt3", "123456", Operation.REGISTER); client.addUser(user3); User user4 = new User(004, "matt4", "123456", Operation.REGISTER); client.addUser(user4); User user5 = new User(005, "matt5", "123456", Operation.REGISTER); client.addUser(user5);
// 查看全部用户 List<User> list = client.queryUserList(); System.out.println("There are " + list.size() + " users in total."); for (User user : list) { System.out.println(user.userId + " " + user.username + " " + user.password); } // 查询用户 User userq1 = client.queryUser(1); if (userq1 != null) { System.out.println("Query: " + userq1.userId + " " + userq1.username + " " + userq1.password); } else { System.out.println("The id: 1 does not exist!"); } User userq2 = client.queryUser(8); if (userq2 != null) { System.out.println("Query: " + userq2.userId + " " + userq2.username + " " + userq2.password); } else { System.out.println("The id: 8 does not exist!"); }
// 登陆用户 User users = new User(005, "matt5", "123456", Operation.LOGIN); client.addUser(users);
// 添加异常用户 User user6 = new User(006, "", "123456", Operation.REGISTER);// name=null client.addUser(user6); User user7 = new User(006, "matt1", "123456", Operation.REGISTER);// name存在 client.addUser(user7); User user8 = new User(005, "matt6", "123456", Operation.REGISTER);// id异常 client.addUser(user8); } catch (Exception e) { e.printStackTrace(); } }
/** * 非阻塞 * * @author matt * @since Apr 7, 2016 * @throws 无 * void */ publicstaticvoidstartNonblockingClient(){ TTransport transport = null; try { transport = new TFramedTransport(new TSocket(ip, port)); TCompactProtocol protocol = new TCompactProtocol(transport); Account.Client client = new Account.Client(protocol); transport.open();
// 正常添加用户 User user1 = new User(001, "matt1", "123456", Operation.REGISTER); client.addUser(user1); User user2 = new User(002, "matt2", "123456", Operation.REGISTER); client.addUser(user2); User user3 = new User(003, "matt3", "123456", Operation.REGISTER); client.addUser(user3); User user4 = new User(004, "matt4", "123456", Operation.REGISTER); client.addUser(user4); User user5 = new User(005, "matt5", "123456", Operation.REGISTER); client.addUser(user5);
// 查看全部用户 List<User> list = client.queryUserList(); System.out.println("There are " + list.size() + " users in total."); for (User user : list) { System.out.println(user.userId + " " + user.username + " " + user.password); }
// 查询用户 User userq1 = client.queryUser(1); if (!userq1.username.equals("")) { System.out.println("Query: " + userq1.userId + " " + userq1.username + " " + userq1.password); } else { System.out.println("The id: 1 does not exist!"); } User userq2 = client.queryUser(8); if (!userq2.username.equals("")) { System.out.println("Query: " + userq2.userId + " " + userq2.username + " " + userq2.password); } else { System.out.println("The id: 8 does not exist!"); }
// 登陆用户 User users = new User(005, "matt5", "123456", Operation.LOGIN); client.addUser(users);
// 添加异常用户 User user6 = new User(006, "", "123456", Operation.REGISTER);// name=null client.addUser(user6); User user7 = new User(006, "matt1", "123456", Operation.REGISTER);// name存在 client.addUser(user7); User user8 = new User(005, "matt6", "123456", Operation.REGISTER);// id异常 client.addUser(user8); } catch (Exception e) { e.printStackTrace(); } }
Starting the Account server... Register success!! Hello matt1 Register success!! Hello matt2 Register success!! Hello matt3 Register success!! Hello matt4 Register success!! Hello matt5 1 8 The id: 8 does not exist!
客户端运行的结果为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
There are 5 users in total. 1 matt1 123456 3 matt3 123456 2 matt2 123456 5 matt5 123456 4 matt4 123456 Query: 1 matt1 123456 The id: 8 does not exist! InvalidOperation(code:101, reason:The name has been used, please change the name!) at matt.thrift.account.Account$addUser_result$addUser_resultStandardScheme.read(Account.java:1165) at matt.thrift.account.Account$addUser_result$addUser_resultStandardScheme.read(Account.java:1) at matt.thrift.account.Account$addUser_result.read(Account.java:1101) at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:86) at matt.thrift.account.Account$Client.recv_addUser(Account.java:93) at matt.thrift.account.Account$Client.addUser(Account.java:80) at matt.thrift.account.AccountClient.startNonblockingClient(AccountClient.java:135) at matt.thrift.account.AccountClient.main(AccountClient.java:151)