Thrift Java practice

Source: Internet
Author: User

Thrift Java practice
Comparing Thrift with other transmission methods, xml and JSON are too large, but xml is traditional and not complex. Json is small and novel, but not perfect. Thrift is too small to be used. It is not as light as the first two. high concurrency, 2. large Data Transmission volume, 3. multi-language environment socket is the tcp network layer, http is the application layer 1. compile an IDL Interface Definition File

namespace java org.acooly.thrift.demo.generalcode     struct Contact{      1:i32 id      2:string name      3:i64 birthday      4:string phoneNo      5:string ipAddress      6:map<string,string> props  }     service ContactManager{    void save(1:Contact contact)    void remove(1:i32 id)    list<Contact> getAll();    list<Contact> query(1:map<string,string> conditions)  }

 

Generator-r -- gen java thriftdemo. thrift generate java code thrift-0.9.1.exe-r -- gen php thriftdemo. thrift generate php code 3. Implement business logic (that is, implement the service method in the interface definition file)
package org.acooly.thrift.demo.server;import java.util.ArrayList;import java.util.Calendar;import java.util.List;import java.util.Map;import org.acooly.thrift.demo.generalcode.Contact;import org.acooly.thrift.demo.generalcode.ContactManager;import org.apache.thrift.TException;public class ContactManagerImpl implements ContactManager.Iface{    public List<Contact> getAll() throws TException {        List<Contact> contacts = new ArrayList<Contact>();        contacts.add(new Contact(1,"zhangpu",Calendar.getInstance().getTimeInMillis(),"1389612222","192.168.2.1",null));        return contacts;    }    public List<Contact> query(Map<String, String> conditions) throws TException {        List<Contact> contacts = new ArrayList<Contact>();        contacts.add(new Contact(1,"zhangpu",Calendar.getInstance().getTimeInMillis(),"1389612222","192.168.2.1",null));        return contacts;    }    public void remove(int id) throws TException {        System.out.println("invoke: remove,id = " + id);    }    public void save(Contact contact) throws TException {        System.out.println("invoke: save,contact = " + contact);             }          }

 

4. Write server code
package org.acooly.thrift.demo.server;import org.acooly.thrift.demo.generalcode.ContactManager;import org.acooly.thrift.demo.generalcode.ContactManager.Iface;import org.apache.thrift.protocol.TCompactProtocol;import org.apache.thrift.protocol.TCompactProtocol.Factory;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TSimpleServer;import org.apache.thrift.server.TServer.Args;import org.apache.thrift.transport.TServerSocket;public class ThriftServer {    public static void main(String[] args) throws Exception{        TServerSocket serverSocket = new TServerSocket(8111);        ContactManager.Processor<Iface> processor = new ContactManager.Processor<Iface>(new ContactManagerImpl());        Factory factory = new TCompactProtocol.Factory();        Args ag = new Args(serverSocket);        ag.outputProtocolFactory(factory);        ag.inputProtocolFactory(factory);        ag.processor(processor);        TServer server = new TSimpleServer(ag);        server.serve();    }     }

 

5. Write client code
package org.acooly.thrift.demo.client;import java.util.Calendar;import java.util.List;import org.acooly.thrift.demo.generalcode.Contact;import org.acooly.thrift.demo.generalcode.ContactManager;import org.apache.thrift.protocol.TCompactProtocol;import org.apache.thrift.protocol.TProtocol;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport;public class ThriftClient {    public static void main(String[] args) throws Exception{                 TTransport transport = new TSocket("localhost",8111);        TProtocol protocol = new TCompactProtocol(transport);        ContactManager.Client client = new ContactManager.Client(protocol);        transport.open();                 List<Contact> list = client.getAll();        System.out.println(list);                 client.save(new Contact(1,"zhangpu",Calendar.getInstance().getTimeInMillis(),"1389612222","192.168.2.1",null));                 client.remove(1);        transport.close();    }}

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.