Thrift as a bottom-up to remove the business logic code, can generate a variety of language client and server code, covering the network, IO, process, thread management framework, really huge, but it is clear level, 4 layers each layer to solve different problems, can be used on demand, quite convenient.
+-------------------------------------------+| Server | -- server process Scheduling | (single-threaded, Event-driven etc) | +-------------------------------------------+| Processor | -- RPC Interface processing function distribution, IDL definition interface implementation will be attached to this | (compiler Generated) | +-------------------------------------------+| Protocol | -- Agreement | (JSON, compact etc) | +-------------------------------------------+| Transport | -- Network Transmission | (Raw TCP, HTTP etc) | +-------------------------------------------+
In fact, for the service-side programming technology Daniel, server scheduling may best reflect the personal skills, but from the transport layer, to serialize this layer of work, is indeed a relatively tedious work, you can directly use thrift generated code to complete the problem.
Above is a digression, in Thrift Java Code Implementation Server This layer has a tthreadpoolserver, inside for thread management is to use Threadpoolexecutor, the following paste the core code
Public voidserve () {Try{Servertransport_.listen (); } Catch(ttransportexception ttx) {logger.error ("Error occurred during listening.", TTX); return; } stopped_=false; while(!stopped_) { intFailurecount = 0; Try{Ttransport Client=servertransport_.accept (); workerprocess WP=Newworkerprocess (client); Executorservice_.execute (WP);//This is threadpoolexecutor}Catch(ttransportexception ttx) {if(!stopped_) { ++Failurecount; Logger.warn ("Transport error occurred during acceptance of message.", TTX); }}} executorservice_.shutdown (); //Loop until Awaittermination finally does return without a interrupted//exception. If we don ' t do this, then we'll shut down prematurely. We want//to let the executorservice clear it ' s task queue, closing client sockets//appropriately. LongTimeoutms =Options_.stopTimeoutUnit.toMillis (Options_.stoptimeoutval); Longnow =System.currenttimemillis (); while(Timeoutms >= 0) { Try{executorservice_.awaittermination (Timeoutms, timeunit.milliseconds); Break; } Catch(interruptedexception ix) {LongNewnow =System.currenttimemillis (); Timeoutms-= (Newnow-Now ); now=Newnow; } } }
It is worth noting that the shutdown () method needs to be called when all tasks are performed, this is a lot of examples on the net, but for the last paragraph of the author repeatedly check the state and then exit, this is really not necessary, in the shutdown () method has a similar code (jdk1.7); Furthermore, Java does not affect other threads when the main thread exits, so this code is more redundant:-D
Threadpoolexecutor Source Learning (2)--Application in Thrift