2.2 监听队列并反馈
-
/**
-
* 开启server
-
*/
-
private void startServer() {
-
try {
-
LOG.info("Waiting for RPC calls.....");
-
while (true) {
-
//获得文本消息
-
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
-
BasicProperties props = delivery.getProperties();
-
//返回消息的属性
-
BasicProperties replyProps = new BasicProperties.Builder()
-
.correlationId(props.getCorrelationId())
-
.build();
-
long receiveTime = System.currentTimeMillis();
-
JSONObject json = new JSONObject();
-
try {
-
String message = new String(delivery.getBody(), "UTF-8");
-
int n = Integer.parseInt(message);
-
LOG.info("Got a request: fib(" + message + ")");
-
json.put("status", "success");
-
json.put("result", fib(n));
-
} catch (Exception e) {
-
json.put("status", "fail");
-
json.put("reason", "Not a Number!");
-
</LOG.error("receive message failed!", e);