Created
March 28, 2012 07:04
-
-
Save nlinker/2224410 to your computer and use it in GitHub Desktop.
Typesafe thrift client wrapper that is reconnecting in case of a failure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.thumbtack.onepm.server; | |
import net.thumbtack.onepm.thrift.FbUser; | |
import net.thumbtack.onepm.thrift.User; | |
import net.thumbtack.onepm.thrift.UserNotFoundException; | |
import org.apache.thrift.TException; | |
import org.apache.thrift.protocol.TBinaryProtocol; | |
import org.apache.thrift.transport.TFramedTransport; | |
import org.apache.thrift.transport.TSocket; | |
import org.apache.thrift.transport.TTransportException; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.List; | |
import java.util.concurrent.Callable; | |
import static net.thumbtack.onepm.thrift.FriendService.Client; | |
/** | |
* POC of the reconnecting friend service client | |
*/ | |
public class FriendServiceClientWrapper implements net.thumbtack.onepm.thrift.FriendService.Iface { | |
private static final Logger logger = LoggerFactory.getLogger(FriendServiceClientWrapper.class); | |
private Client fsClient; | |
public FriendServiceClientWrapper() { | |
this.fsClient = recreateClient(); | |
} | |
private <U> U callWithFsClient(Callable<U> closure) throws TException { | |
try { | |
return closure.call(); | |
} catch (Exception e) { | |
fsClient = recreateClient(); | |
try { | |
return closure.call(); | |
} catch (Exception e1) { | |
throw new TException(e1); | |
} | |
} | |
} | |
private Client recreateClient() { | |
String host = "localhost"; | |
int port = 7005; | |
TSocket socket = new TSocket(host, port); | |
socket.setTimeout(200000); | |
TFramedTransport transport = new TFramedTransport(socket); | |
TBinaryProtocol protocol = new TBinaryProtocol(transport); | |
try { | |
transport.open(); | |
} catch (TTransportException e) { | |
logger.error("Connect to FriendService failed", e.getMessage()); | |
} | |
return new net.thumbtack.onepm.thrift.FriendService.Client(protocol); | |
} | |
@Override | |
public FbUser createFbUser(final FbUser fbUser) throws TException { | |
return callWithFsClient(new Callable<FbUser>() { | |
@Override | |
public FbUser call() throws Exception { | |
return fsClient.createFbUser(fbUser); | |
} | |
}); | |
} | |
@Override | |
public boolean updateFriends(final long facebookId, final List<FbUser> friends) throws TException { | |
return callWithFsClient(new Callable<Boolean>() { | |
@Override | |
public Boolean call() throws Exception { | |
return fsClient.updateFriends(facebookId, friends); | |
} | |
}); | |
} | |
@Override | |
public short getDistance(final long userId, final long candidateId) throws UserNotFoundException, TException { | |
return callWithFsClient(new Callable<Short>() { | |
@Override | |
public Short call() throws Exception { | |
return fsClient.getDistance(userId, candidateId); | |
} | |
}); | |
} | |
@Override | |
public List<Short> getDistances(final long userId, final List<Long> candidateIds) throws UserNotFoundException, TException { | |
return callWithFsClient(new Callable<List<Short>>() { | |
@Override | |
public List<Short> call() throws Exception { | |
return fsClient.getDistances(userId, candidateIds); | |
} | |
}); | |
} | |
@Override | |
public List<FbUser> linkedThrough(final long userId, final long candidateId) throws UserNotFoundException, TException { | |
return callWithFsClient(new Callable<List<FbUser>>() { | |
@Override | |
public List<FbUser> call() throws Exception { | |
return fsClient.linkedThrough(userId, candidateId) | |
} | |
}); | |
} | |
@Override | |
public List<List<FbUser>> linkedThroughMulti(final long userId, final List<Long> candidateIds) throws UserNotFoundException, TException { | |
return callWithFsClient(new Callable<List<List<FbUser>>>() { | |
@Override | |
public List<List<FbUser>> call() throws Exception { | |
return fsClient.linkedThroughMulti(userId, candidateIds); | |
} | |
}); | |
} | |
@Override | |
public List<User> getFriends(final long userId, final short distance) throws UserNotFoundException, TException { | |
return callWithFsClient(new Callable<List<User>>() { | |
@Override | |
public List<User> call() throws Exception { | |
return fsClient.getFriends(userId, distance); | |
} | |
}); | |
} | |
@Override | |
public List<FbUser> getUserFriends(final long userId) throws UserNotFoundException, TException { | |
return callWithFsClient(new Callable<List<FbUser>>() { | |
@Override | |
public List<FbUser> call() throws Exception { | |
return fsClient.getUserFriends(userId); | |
} | |
}); | |
} | |
@Override | |
public long version() throws TException { | |
return callWithFsClient(new Callable<Long>() { | |
@Override | |
public Long call() throws Exception { | |
return fsClient.version(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment