Skip to content

Instantly share code, notes, and snippets.

@wendal
Created December 27, 2013 03:54
Show Gist options
  • Save wendal/8142368 to your computer and use it in GitHub Desktop.
Save wendal/8142368 to your computer and use it in GitHub Desktop.
用java访问一个redis为后端的http订阅服务. 服务器单向推送
public String subscribe(String key) {
if (Strings.isBlank(key))
return null;
try {
log.info("subscribe >> " + key);
String url = "某某服务器" + "/subscribe?key=" + key;
Response resp = Sender.create(url).setTimeout(60 * 60 * 1000).send();
if (resp == null)
return null;
if (resp.getStatus() == 404) {
// 服务器不支持
log.info("server not support subscribe");
return null;
}
if (resp.getStatus() == 500) {
return "";
}
if (resp.isOK()) {
String str = resp.getContent();
str = Strings.sBlank(str);
log.info("subscribe get >> " + key + " " + str);
return str;
}
}
catch (HttpException e2) {
if (e2.getCause() instanceof java.net.SocketTimeoutException) {
if (e2.getCause().getMessage().contains("Read timed out")) {
log.info("Catch : Read timed out in subscribe, it is OK");
return "";
}
}
}
catch (Throwable e) {
Peanut.checkInterrupted(e);
log.warn("subscribe Err", e);
}
log.info("subscribe fail >> " + key);
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment