java - Java azure中while循环输出不同结果的打印结果

标签 java php azure

我成功地将 json 对象发送到 java 中的 azure cloud。但问题是我的接收器,消息接收良好,但问题是当我想将其发送回 PHP 时: 我正在发送此消息:

{"Id":"914897","Name":"Broken window","Description":"Window broken","PriorityId":"1"}

当我收到此消息时,我想首先打印该消息以验证我是否收到结果并发送了它。但是,在 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 循环内打印:

 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"} 

最佳答案

感谢来自此链接的 Dominic Betts https://azure.microsoft.com/en-us/documentation/articles/service-bus-java-how-to-use-queues/#comments

我使用以下代码来实现我的目标:

 StringBuilder stringBuilder = new StringBuilder();
 stringBuilder.append(message_from_queue );

关于java - Java azure中while循环输出不同结果的打印结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33208975/

相关文章:

php - 如何修复语法错误,意外的 '' (T_STRING)?

php - 如何使用在线 cPanel 数据库在 php 中连接动态数据库

Azure ML 工作区无法为 VNet 后面的工作区链接存储帐户上传数据

azure - 是否可以将 Windows 事件日志获取到应用程序洞察中?

java - 如何在不触发 insertUpdate() 的情况下将文本插入文档

java - Oozie 作业处于运行状态

java - 是否可以在 Swing 中为 JFrame 创建不同的外观?

java - Java 中等效的 findAll python 方法

php - 在文件夹中的所有文件中搜索字符串

asp.net - 是否可以使用 Azure AD B2C 以编程方式登录用户?