Printout results in while loop output different results in Java azure - java

I am sending json object to azure cloud in java successfully.but the problem is my reciever,the message is recieved well but the problem is when i want to send it back to PHP:
I am sending this message:
{"Id":"914897","Name":"Broken window","Description":"Window
broken","PriorityId":"1"}
As I receive this message I want to first printout the message to verify whether i am getting the results and i sent it.however inside the while loop is printing correct but outside a broken results here is my Code:
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);
}
I am getting this results : Printing inside while loop:
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"}**
}
Printing outside while loop:
System.out.print("outside while" +message_from_queue + "\n");/*Broken window","Description":"Window broken","PriorityId":"1"}

All Thanks to Dominic Betts from this link https://azure.microsoft.com/en-us/documentation/articles/service-bus-java-how-to-use-queues/#comments
I used the following code to achieve my goal:
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(message_from_queue );

I think the issue was caused by doing the POST request in the inside while loop. Codes in the inside while loop is for reading messages from queue, So the POST request of HttpClient should be in the outside while loop.
I refered to the doc https://azure.microsoft.com/en-us/documentation/articles/service-bus-java-how-to-use-queues/ and modified your code as below:
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);
}
Best Regards

Related

How to handle HTTP request using Java Socket?

I am trying to implement sample HTTP server using Java socket and executor service for concurrency. However every 2nd request is failing when I run the test using JMeter with 2 or more requests or browser for example.
How to properly handle the request? Here is the sample source code:
public class Service {
public static void main(String[] args) throws Exception {
var serverSocket = new ServerSocket(8080);
var executors = Executors.newFixedThreadPool(4);
while(true) {
try {
var server = serverSocket.accept();
executors.submit(() -> {
try {
var text = "sample";
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
System.out.println("Getting empty request");
var response = "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: " + text.length() + "\r\n\r\n"
+ text;
server.getOutputStream().write(response.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
System.out.println("Executor error:" + e.toString());
e.printStackTrace();
} finally {
try {
System.out.println("Closing server");
server.close();
} catch (Exception e) {
System.out.println("Executor error2: ");
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
break;
}
}
serverSocket.close();
}
}
Your first problem lies in your response.
"HTTP/1.1 200 OK\r\n"
That allows for keep-alive, which you're not handling. A basic JMeter sampler tries to use keep alive, that is why you always fail on the second attempt.
You can change it to
"HTTP/1.0 200 OK\r\n"
That does not support keep alive, so you'll get a lot more successes with your current code. For me I only get a couple 1000 responses before JMeter has another error, but I don't know what the error is.
To support keep alive, I need to parse the request. Here is an example.
int clients = 0;
while(true) {
try {
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
var server = serverSocket.accept();
final int client_no = clients++;
System.out.println("handling: " + client_no);
executors.submit(() -> {
int sent = 0;
try {
var is = server.getInputStream();
var os = server.getOutputStream();
var text = "sample";
byte[] tb = text.getBytes(StandardCharsets.UTF_8);
char[] buffer = new char[256];
int cr_count = 0;
while( true ){
int i=0;
int r = is.read();
if(r == -1) break;
while( r != -1 ){
char c = (char)r;
if( c == '\n' ){
cr_count++;
} else if( c != '\r' ){
cr_count = 0;
}
buffer[i++] = c;
if(cr_count == 2) break;
r = is.read();
}
//System.out.println("request: " + new String(buffer));
var response = "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: " + tb.length + "\r\n\r\n";
os.write(response.getBytes(StandardCharsets.UTF_8));
os.write(tb);
os.flush();
sent++;
}
} catch (Exception e) {
System.out.println("Executor error:" + e.toString());
e.printStackTrace();
} finally {
try {
System.out.println("Closing connection!");
server.close();
} catch (Exception e) {
System.out.println("Executor error2: ");
e.printStackTrace();
}
System.out.println("sent " + sent + " responses to client " + client_no);
}
});
} catch (Exception e) {
e.printStackTrace();
break;
}
}
This will run a JMeter test for me. It can use either 1.0 or 1.1 and finish 10's of thousands of requests. If I use keep alive (1.1) each client handles many requests, if I don't use keep alive (1.0) each client handles 1 request.
If I dont read the request header, then the http 1.0 version will stop after a couple thousand requests.
It is also a separate issue from your original "dies after the second request." which is because your are not actually using HTTP 1.1 !

Concatinating JSON elements using java-json

I have a program that reads in a CSV file and returns a JSON String.
However using JsonArray and JsonObject only yield the last row of results from the file. How do I concat numerous rows of data into a JSON object?
Output
[{"name":"Other_Renwables","load":"14.3"},{"name":"Other_Renwables","load":"14.3"},{"name":"Other_Renwables","load":"14.3"},{"name":"Other_Renwables","load":"14.3"},{"name":"Other_Renwables","load":"14.3"},{"name":"Other_Renwables","load":"14.3"}]
Code
JSONObject jObject = new JSONObject();
JSONArray jArray = new JSONArray();
int count = 0;
try{
logger.info("\nSending 'GET' request to URL : " + url);
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
int responseCode = response.getStatusLine().getStatusCode();
logger.info("Response Code : " + responseCode);
if (responseCode != 404){
try(BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()))) {
if(reader != null){
//StringBuilder builder = new StringBuilder();
String aux = "";
while ((aux = reader.readLine()) != null) {
String[] tab = aux.split(",");
if (count > 0){
try {
jObject.put("name", tab[0]);
jObject.put("load", tab[1]);
jArray.put(jObject);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
count++;
}
retVal = jArray.toString();
}
}
}
return retVal;
}finally{
httpClient.getConnectionManager().shutdown();
}
You are using same object and over-writing it in your loop.
This instantiation:
JSONObject jObject = new JSONObject();
must be in your loop.
try {
JSONObject jObject = new JSONObject();
jObject.put("name", tab[0]);
jObject.put("load", tab[1]);
jArray.put(jObject);
}

wait for method of process class doesn't return

I have Process Class in my code and that couldn't read the inputbufferreader data when i try to reading the larger number of data from inputbuffer and waitfor method of process class never return anything
this issue is happened in live server
but below code running fine and read the all the data from inputbuffer in local server
private static JSONObject ExecJniApp(String inputJsonString) throws JniException
{
int exitStatus = 0;
String workingDirectory = JniSettings.getJniAppDirectory();
String command = workingDirectory + binaryName;
System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
String outputString = "";
try
{
Process p = Runtime.getRuntime().exec(command, null, new File(workingDirectory));
// send input vi stdin
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("\n>>>>>>>>>>");
writer.write(inputJsonString);
writer.write("<<<<<<<<<<\n");
writer.flush();
if (JniSettings.isLinux())
{
p.waitFor();
}
else
{
// System.out.println("ExecJniApp: windows wait for");
}
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
for (;;)
{
String line = reader.readLine();
if (line == null)
{
break;
}
// System.out.println(line);
outputString += line + "\n";
}
// exit code of command and log error detail
// exit status = 0 -> Success
exitStatus = p.exitValue();
if (exitStatus != 0)
{
System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
System.out.println("ExecJniApp: outputString:\n" + outputString);
throw new Exception("Exit status other than zero :- " + exitStatus + "\noutput: ");
}
}
catch (Exception e)
{
// System.out.println("ExecJniApp failed");
e.printStackTrace();
throw new JniException("JniInterface failed");
}
System.out.println("ExecJniApp: outputString:\n" + outputString);
int index1 = outputString.indexOf(">>>>>>>>>>");
if (index1 == -1)
{
// System.out.println("ExecJniApp failed, Invalid output format");
throw new JniException("ExecJniApp failed, Invalid output format");
}
index1 += 10;
int index2 = outputString.indexOf("<<<<<<<<<<");
if (index2 == -1 || index2 <= index1)
{
// System.out.println("ExecJniApp failed, Invalid output format");
throw new JniException("ExecJniApp failed, Invalid output format");
}
String outputJsonString = outputString.substring(index1, index2);
// System.out.println("ExecJniApp: outputJsonString: " + outputJsonString);
JSONParser parser = new JSONParser();
JSONObject obj = null;
try
{
obj = (JSONObject) parser.parse(outputJsonString);
}
catch (ParseException e)
{
// System.out.println("ExecJniApp failed, Json parse failed");
e.printStackTrace();
throw new JniException("JniInterface failed, Json parse failed");
}
Object errorObject = obj.get("error");
if (errorObject != null)
{
String errorString = errorObject.toString();
// System.out.println("ExecJniApp failed, " + errorString);
throw new JniException("ExecJniApp failed, " + errorString);
}
return obj;
}
Just replace your code with below code.
Must close your InputStream connection.
Process p = Runtime.getRuntime().exec(command, null, new File(workingDirectory));
// send input vi stdin
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
writer.write("\n>>>>>>>>>>");
writer.write(inputJsonString);
writer.write("<<<<<<<<<<\n");
writer.flush();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
for (;;)
{
String line = reader.readLine();
if (line == null)
{
break;
}
// System.out.println(line);
outputString += line + "\n";
}
p.getOutputStream().close();
p.getInputStream().close();
}
catch(Exception e)
{
e.printStackTrace();
}
if (JniSettings.isLinux())
{
p.waitFor();
}
else
{
// System.out.println("ExecJniApp: windows wait for");
}
// exit code of command and log error detail
// exit status = 0 -> Success
exitStatus = p.exitValue();
if (exitStatus != 0)
{
System.out.println("ExecJniApp: inputJsonString: " + inputJsonString);
System.out.println("ExecJniApp: outputString:\n" + outputString);
throw new Exception("Exit status other than zero :- " + exitStatus + "\noutput: ");
}

XML Request with HttpClient throws Null Exception

try
{
String xmlReq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request_inquiry><partner_id>0999</ partner_id><terminal_type>6012</ terminal_ type><product_code>4001</product _code><date_time>20130715115100</date_time><trx_id>SDFSF11234424ADFA</trx_id><data><cust_id>030913320611</cust_id></data></request_inquiry>";
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
HttpPost httpPost = new HttpPost("202.169.43.53:52056/transaction");
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=ISO");
// httpPost.setHeader(HttpHeaders.CONTENT_LENGTH, Integer.toString(xmlReq.length()));
StringEntity se = new StringEntity(xmlReq, ContentType.TEXT_XML);
httpPost.setEntity(se);
System.out.println("Request>>"+httpPost);
StringBuilder html = new StringBuilder("");
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() != 200) {
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024] ;
while(in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println("Output HTML>> "+html.toString());
}
else{
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024] ;
while(in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println(html);
}
} catch (Exception ex) {
throw new SystemException(Common.ERROR_OTHER, ex.getMessage());
}
}
catch(Exception ex) {
System.out.println("Exception>>"+ex.getMessage());
}
I've tried many ways to send XML request to server. and one of the way is look likes the code above. And I have no idea why throws NullException? Is there something wrong with my code? Thanks for help.
The actual exception is in the line
HttpPost httpPost = new HttpPost("202.169.43.53:52056/transaction");
saying
java.lang.IllegalArgumentException
at java.net.URI.create(URI.java:841)
at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:76)
at Test.main(Test.java:22)
Caused by: java.net.URISyntaxException: Illegal character in scheme name at index 0: 202.169.43.53:52056/transaction
at java.net.URI$Parser.fail(URI.java:2810)
at java.net.URI$Parser.checkChars(URI.java:2983)
at java.net.URI$Parser.checkChar(URI.java:2993)
at java.net.URI$Parser.parse(URI.java:3009)
at java.net.URI.<init>(URI.java:577)
at java.net.URI.create(URI.java:839)
... 2 more
It is because the URI is missing the protocol like http:// or https://
Ex:
try {
String xmlReq = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><request_inquiry><partner_id>0999</ partner_id><terminal_type>6012</ terminal_ type><product_code>4001</product _code><date_time>20130715115100</date_time><trx_id>SDFSF11234424ADFA</trx_id><data><cust_id>030913320611</cust_id></data></request_inquiry>";
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 30);
httpClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 30);
HttpPost httpPost = new HttpPost("http://202.169.43.53:52056/transaction");
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "text/xml;charset=ISO");
// httpPost.setHeader(HttpHeaders.CONTENT_LENGTH,
// Integer.toString(xmlReq.length()));
StringEntity se = new StringEntity(xmlReq, ContentType.TEXT_XML);
httpPost.setEntity(se);
System.out.println("Request>>" + httpPost);
StringBuilder html = new StringBuilder("");
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() != 200) {
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024];
while (in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println("Output HTML>> " + html.toString());
} else {
InputStream in = httpResponse.getEntity().getContent();
byte b[] = new byte[1024];
while (in.read(b) != -1) {
html.append((new String(b)).toString());
b = new byte[1024];
}
System.out.println(html);
}
} catch (Exception ex) {
ex.printStackTrace();
}
Note: When you are logging exceptions make sure you log the stack trace as well, since it will give you more details about the exception like which class, method and line caused the exception.

how to use the HTTP range header in J2ME?

is it possible to use range header in HTTP with HTTP GET request?
if yes than how?
I just want to download bytes from server with specified bytes.
i.e if I want to download bytes from 0 to 255.
See this sample code,
HttpConnection connection = null;
InputStream inputstream = null;
try
{
connection = (HttpConnection) Connector.open(url); // Enter your URL here.
//HTTP Request
connection.setRequestMethod(HttpConnection.GET);
connection.setRequestProperty("Content-Type","//text plain");
connection.setRequestProperty("Connection", "close");
// HTTP Response
System.out.println("Status Line Code: " + connection.getResponseCode());
System.out.println("Status Line Message: " + connection.getResponseMessage());
if (connection.getResponseCode() == HttpConnection.HTTP_OK)
{
System.out.println(
connection.getHeaderField(0)+ " " + connection.getHeaderFieldKey(0));
System.out.println(
"Header Field Date: " + connection.getHeaderField("date"));
String str;
inputstream = connection.openInputStream();
int length = (int) connection.getLength();
if (length != -1)
{
byte incomingData[] = new byte[length];
inputstream.read(incomingData);
str = new String(incomingData);
}
else
{
ByteArrayOutputStream bytestream =
new ByteArrayOutputStream();
int ch;
while ((ch = inputstream.read()) != -1)
{
bytestream.write(ch);
}
str = new String(bytestream.toByteArray());
bytestream.close();
}
System.out.println(str);
}
}
catch(IOException error)
{
System.out.println("Caught IOException: " + error.toString());
}
finally
{
if (inputstream!= null)
{
try
{
inputstream.close();
}
catch( Exception error)
{
/*log error*/
}
}
if (connection != null)
{
try
{
connection.close();
}
catch( Exception error)
{
/*log error*/
}
You could try this:
HttpConnection httpConnection = (HttpConnection) Connector.open("http://www.server.com/file");
httpConnection.setRequestMethod(HttpConnection.GET);
httpConnection.setRequestProperty("Range","0-255");
Check the docs out: HttpConnection

Categories