Skip to content

Instantly share code, notes, and snippets.

@fujohnwang
Created October 13, 2014 03:06
Show Gist options
  • Save fujohnwang/b994a74e15d0e448e8ef to your computer and use it in GitHub Desktop.
Save fujohnwang/b994a74e15d0e448e8ef to your computer and use it in GitHub Desktop.
msgpack and http accessor sample code
/**
* Hello world!
*/
public class App {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger("App");
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
System.out.println("-----------------------we got here?");
e.printStackTrace();
}
});
System.out.println(">>>>>>>>>>>>>>>1");
final AtomicReference<AsyncHttpClient> http = new AtomicReference<AsyncHttpClient>();
System.out.println(">>>>>>>>>>>>>>>2");
try {
http.set(new AsyncHttpClient());
System.out.println(">>>>>>>>>>>>>>>3");
MessagePack marsheller = new MessagePack();
Future<Response> f = http.get().preparePost("http://localhost:8080/csw/users/sample2").setBody(marsheller.write(new SampleMsg())).addHeader("Content-Type", "application/x-msgpack").addQueryParameter("p", "just a test").execute();
// Future<Response> f = http.get().prepareGet("http://localhost:8080/csw/users/sample").execute();
// check header and status code first!!!
Response response = f.get();
if (response.getStatusCode() == 200) {
// SampleMsg message = marsheller.read(response.getResponseBodyAsBytes(), SampleMsg.class);
// System.out.println(message.name);
System.out.println(response.getResponseBody());
// System.out.println(marsheller.read(response.getResponseBodyAsBytes()));
} else {
switch (response.getStatusCode()) {
case 401:
System.err.println("401");
break;
default:
System.err.println("status code=" + response.getStatusCode());
System.out.println("body=" + response.getResponseBody());
break;
}
}
} catch (Throwable ex) {
logger.error("fucking wrong: {}", ex);
} finally {
AsyncHttpClient client = http.get();
if (client != null) client.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment