java - Multi-threaded connection manager with Jersey Apache Client 4 -
i have jersey client , running, using apache client 4 library, this:
private client createclient() { apachehttpclient4config cc = new defaultapachehttpclient4config(); // boring stuff here return apachehttpclient4.create(cc); } but default uses basicclientconnmanager, doesn't allow multi-threaded connections.
the apachehttpclient4config javadoc says need set property_connection_manager threadsafeclientconnmanager instance if want multi-threaded operation. can this, , works ok:
private client createclient() { apachehttpclient4config cc = new defaultapachehttpclient4config(); cc.getproperties().put(apachehttpclient4config.property_connection_manager, new threadsafeclientconnmanager()); // boring stuff here return apachehttpclient4.create(cc); } but threadsafeclientconnmanager deprecated. annoying.
the more modern version poolinghttpclientconnectionmanager. unfortunately, though, apachehttpclient4.create() method requires connection manager implementation of clientconnectionmanager (itself deprecated), , poolinghttpclientconnectionmanager doesn't implement interface. if try use it, connection manager gets ignored , we're basicclientconnmanager.
how can end thread-safe client without using that's deprecated?
you can create client follows (see https://github.com/phillbarber/connection-leak-test/blob/master/src/test/java/com/github/phillbarber/connectionleak/integrationtestthatexaminesconnectionpoolbeforeandafterrun.java#l30-l33):
client = new apachehttpclient4(new apachehttpclient4handler(httpclients.custom() .setconnectionmanager(new poolinghttpclientconnectionmanager()) .build(), null, false));
Comments
Post a Comment