gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:19:38 24 4
gpt4 key购买 nike

我成功地将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com