Another way to do it would be to use ThreadLocal connections. https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html <https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html>
> On Jun 7, 2016, at 6:14 AM, 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 > > > >
