2015-10-19 199 views
1

我發送json對象到Java中的azure雲成功。但問題是我的接收者,消息收到很好,但問題是當我想發回它回到PHP: 我送這條消息:打印輸出結果在while循環輸出不同的結果在Java azure

{ 「ID」: 「914897」, 「姓名」: 「破窗」, 「說明」: 「窗口 破」, 「PriorityId」: 「1」 }

當我收到此消息時,我想先打印出消息來驗證我是否得到結果,然後發送它E中的while循環打印正確的,但外界在這裏斷了結果是我的代碼:

try { 

     Configuration config 
       = ServiceBusConfiguration.configureWithSASAuthentication(

       ); 


     ServiceBusContract service = ServiceBusService.create(config); 

     ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT; 
     opts.setReceiveMode(ReceiveMode.PEEK_LOCK); 
     //send object 
     HttpClient httpClient = new DefaultHttpClient(); 
     Gson gson= new Gson(); 
     while (true) { 
      ReceiveQueueMessageResult resultQM = service.receiveQueueMessage("mobile",opts); 
      BrokeredMessage message = resultQM.getValue(); 
      if (message != null && message.getMessageId() != null) { 
       System.out.println("MessageID: " + message.getMessageId()); 
       // Display the queue message. 
       System.out.print("From queue:"); 
       byte[] b = new byte[20000000]; 
       String message_from_queue = null; 
       String thu =null; 
       String jsonn = null; 
       int numRead = message.getBody().read(b); 


       while (-1 != numRead) { 
        message_from_queue = new String(b); 

        message_from_queue = message_from_queue .trim();      
        numRead = message.getBody().read(b); 
       //System.out.print("inside while" +message_from_queue + **"\n");//{"Id":"914897","Name":"Broken window","Description":"Window broken","PriorityId":"1"}** 


           try { 


    HttpPost request = new HttpPost("http://localhost:3308/emlive/index.php/Api/createDefect"); 
    StringEntity params =new StringEntity("defect=" + message_from_queue); 
    request.addHeader("content-type", "application/x-www-form-urlencoded"); 
    request.addHeader("Accept","application/json"); 
    request.setEntity(params); 
    HttpResponse response = httpClient.execute(request); 
    //System.out.printf("---------------------------------Done-------------------------------"); 
    // handle response here... 
    message.setSessionId(""); 
    System.out.println(org.apache.http.util.EntityUtils.toString(response.getEntity())); 
    org.apache.http.util.EntityUtils.consume(response.getEntity()); 
} 
catch (Exception ex) { 
    // handle exception here 
} finally { 
    httpClient.getConnectionManager().shutdown(); 
} 
    } 
        //System.out.print("outside while" +message_from_queue +     "\n");//Broken window","Description":"Window broken","PriorityId":"1"}                

       System.out.println(); 
       System.out.println("Custom Property: " 
         + message.getProperty("MyProperty")); 
       //service.deleteMessage(message); 
       System.out.println("Deleting this message."); 

       //service.deleteMessage(message); 
      } else { 
       System.out.println("Finishing up - no more messages."); 
       break; 
       // Added to handle no more messages. 
       // Could instead wait for more messages to be added. 
      } 

     } 
    } catch (ServiceException e) { 
     System.out.print("ServiceException encountered: "); 
     System.out.println(e.getMessage()); 
     System.exit(-1); 
    } catch (Exception e) { 
     System.out.print("Generic exception encountered: "); 
     System.out.println(e.getMessage()); 
     System.exit(-1); 
    } 

我得到這個結果:當打印內部循環:

while (-1 != numRead) { 
message_from_queue = new String(b); 

    message_from_queue = message_from_queue .trim();      
    numRead = message.getBody().read(b); 

System.out.print("inside while" +message_from_queue + **"\n");//{"Id":"914897","Name":"Broken window","Description":"Window broken","PriorityId":"1"}** 
} 

打印之外while循環:

System.out.print("outside while" +message_from_queue + "\n");/*Broken window","Description":"Window broken","PriorityId":"1"} 
+0

請解釋爲什麼需要使用內循環時發送消息。我認爲內部while循環不需要通過http發送消息。 –

+0

我刪除了while循環,但沒有任何幫助,因爲我仍然得到如下所示的破碎結果:{「Id」:「914451」,「Name」:「Brok這次給了我第一個斷開的字符串。 –

回答

0

我認爲這個問題是這樣做的,而循環內部的POST請求引起的。 while while循環中的代碼用於從隊列中讀取消息,因此HttpClient的POST請求應該位於外部while循環中。

我refered到文檔https://azure.microsoft.com/en-us/documentation/articles/service-bus-java-how-to-use-queues/,並作如下修改代碼:

try { 
    Configuration config = ServiceBusConfiguration.configureWithSASAuthentication("<namespace>", "<sas_key_name>", 
      "<sas_key>", ".servicebus.windows.net"); 
    ServiceBusContract service = ServiceBusService.create(config); 
    ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT; 
    opts.setReceiveMode(ReceiveMode.PEEK_LOCK); 
    // send object 
    // HttpClient httpClient = new DefaultHttpClient(); 
    CloseableHttpClient httpClient = HttpClients.createDefault(); 
    // Gson gson = new Gson(); 
    while (true) { 
     ReceiveQueueMessageResult resultQM = service.receiveQueueMessage("mobile", opts); 
     BrokeredMessage message = resultQM.getValue(); 
     if (message != null && message.getMessageId() != null) { 
      System.out.println("MessageID: " + message.getMessageId()); 
      // Display the queue message. 
      System.out.print("From queue:"); 
      byte[] b = new byte[20000000]; 
      String message_from_queue = null; 
      // String thu = null; 
      // String jsonn = null; 
      int numRead = message.getBody().read(b); 
      while (-1 != numRead) { 
       message_from_queue = new String(b); 
       message_from_queue = message_from_queue.trim(); 
       numRead = message.getBody().read(b); 
       // System.out.print("inside while" +message_from_queue + 
       // **"\n");//{"Id":"914897","Name":"Broken 
       // window","Description":"Window 
       // broken","PriorityId":"1"}** 
      } 
      // System.out.print("outside while" +message_from_queue + 
      // "\n");//Broken window","Description":"Window 
      // broken","PriorityId":"1"} 
      int statusCode = -1; 
      try { 
       HttpPost request = new HttpPost("http://localhost:3308/emlive/index.php/Api/createDefect"); 
       StringEntity params = new StringEntity("defect=" + message_from_queue); 
       request.addHeader("content-type", "application/x-www-form-urlencoded"); 
       request.addHeader("Accept", "application/json"); 
       request.setEntity(params); 
       HttpResponse response = httpClient.execute(request); 
       // System.out.printf("---------------------------------Done-------------------------------"); 
       // handle response here... 
       message.setSessionId(""); 
       System.out.println(EntityUtils.toString(response.getEntity())); 
       EntityUtils.consume(response.getEntity()); 
      } catch (Exception ex) { 
       // handle exception here 
      } finally { 
       httpClient.close(); 
      } 
      System.out.println(); 
      System.out.println("Custom Property: " + message.getProperty("MyProperty")); 
      if (statusCode == 200) { 
       // Remove message from queue. 
       System.out.println("Deleting this message."); 
       service.deleteMessage(message); 
      } 
     } else { 
      System.out.println("Finishing up - no more messages."); 
      break; 
      // Added to handle no more messages. 
      // Could instead wait for more messages to be added. 
     } 

    } 
} catch (ServiceException e) { 
    System.out.print("ServiceException encountered: "); 
    System.out.println(e.getMessage()); 
    System.exit(-1); 
} catch (Exception e) { 
    System.out.print("Generic exception encountered: "); 
    System.out.println(e.getMessage()); 
    System.exit(-1); 
} 

問候