Yes, you need an object pool like https://commons.apache.org/proper/commons-pool/ or https://github.com/DanielYWoo/fast-object-pool I prefer TCP over HTTP since TCP is much faster, if you need HTTP Google protocol buffers or JSON would be better choices.
On 7/6/2016, 6:57 PM, "Tuan Le Dinh" <[email protected]> wrote: >Could you please elaborate with an example? > >Applying to my mentioned code, does it mean maintaining a pool of >userServiceClient, >each with different THttpClient, protocol and mp? > >Kind regards, >Tuan Le > >On Tue, Jun 7, 2016 at 7:14 PM, Daniel Wu <[email protected]> wrote: > >> Thrift client/connection is not thread safe. Use a lock or object pool. >> >> On 7/6/2016, 5:51 PM, "Tuan Le Dinh" <[email protected]> wrote: >> >> >Hello, >> > >> >I have a working Thrift client in the below snippet. >> > >> >TTransport transport = new THttpClient(new >> >Uri("http://localhost:8080/api/"));TProtocol protocol = new >> >TBinaryProtocol(transport);TMultiplexedProtocol mp = new >> >TMultiplexedProtocol(protocol, "UserService");UserService.Client >> >userServiceClient = new >> >> >UserService.Client(mp);System.out.println(userServiceClient.getUserById(100)); >> > >> >When running the client within multi-threaded environment >> > >> >threads[i] = new Thread(new Runnable() { >> > @Override >> > public void run() { >> > System.out.println(userServiceClient.getUserById(someId)); >> > }} >> > >> >I got an exception: *out of sequence response* >> > >> >org.apache.thrift.TApplicationException: getUserById failed: out of >> >sequence response >> >at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:76) >> > >> >I guess the reason is that Thrift generated Client is not thread safe. But >> >if I want multi-clients to call the same method getUserById() >> simultaneously, >> >how can I make it? >> > >> >I made a copy of this question on Stackoverflow. You can see it here >> > >> http://stackoverflow.com/questions/37674466/how-to-make-a-thrift-client-for-multiple-threads >> >Kind regards, >> >Tuan Le >> >> >> >> >>
