Single URLConnection to fetch only the first line in java using BufferedReader - java

I am trying to write a Java program that should automatically download text from a website if and only if it gets updated. The problem I am running into is using only one HTTPURLConnection to do that because if i don't there will be billions of HTTPURLConnections to the web server since I am using a while(true) loop. Here is my work-in-progress, the getMsg() method receives a url and opens an HTTPURLConnection. Currently I am starting a new connection every time I have to read a line, which is not the most efficient way I am sure. How do I keep reading the same line with the same HTTPURLConnection?
// Starts a new URLConnection to "localhost/currentmsg.dat"
// Receives JSON string from the URLConnection
// Sets up a different variable for each object received from the URL for eg. if delete=1 means that the admin is requesting to delete a message from the screen.
import java.io.*;
import java.net.*;
import org.json.*;
import java.io.*;
import java.util.Scanner;
public class jListenerURL {
// Current arguments retrieved from the URL
private static int msgID = 0;
private static int aptID = 1; // Apartment ID of current device
private static int unitID = 3; // Unit ID of current device
static String message; // Message received from admin
static int delete; // Delete a message?
static int dmsgID; // What message to delete?
public static void jListener() {
URL url;
boolean keepGoing = true;
String msg = "";
try {
url = new URL("http://www.lotussmoke.com/msgtest/currentmsg.dat");
while (keepGoing) {
msg = getMsg(url);
JSONObject jObj = null;
try {
jObj = new JSONObject(msg);
}
catch (JSONException je) {
System.out.println("JSON Exception for message, try restarting terminal.");
}
int current = jObj.getInt("msgID");
int targetaptID = jObj.getInt("aptID");
int targetunitID = jObj.getInt("unitID");
// Keep listening, if the message changes meaning a different msgID then print that message
if (current!=msgID && targetaptID == aptID && targetunitID == unitID) {
msgID = jObj.getInt("msgID");
message = jObj.getString("message");
delete = jObj.getInt("delete");
dmsgID = jObj.getInt("dmsgID");
if (delete==1) {
// Delete a message
System.out.println("Delete msg ID? " + dmsgID);
continue;
}
System.out.println("Message ID: " + msgID);
System.out.println("Apartment ID: " + aptID);
System.out.println("Unit ID: " + unitID);
System.out.println("Message: " + message);
}
}
}
catch (MalformedURLException e) {
System.err.println();
}
}
public static void main(String args[]) throws Exception {
jListener();
}
private static String getMsg(URL url) {
HttpURLConnection con = null;
try {
con = (HttpURLConnection) url.openConnection();
} catch (IOException e1) {
e1.printStackTrace();
}
BufferedReader in = null;
String msg = "";
try {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String received;
while((received = in.readLine()) != null) {
//System.out.println(received);
msg = received;
}
in.close();
}
catch (IOException e) {
e.printStackTrace();
}
return msg;
}
}

Why don't you simply declare your HttpURLConnection object outside of your while loop ? It will then not open a connection at each call inside the loop.

HttpURLConnection cannot be reused, but it can reuse an open connection to the same server internally by setting the header Connection: keep-alive. It doesn't make sense if you connect to different servers, obviously.
One way to efficiently test whether there are changes, is to use If-Modified-Since header or the like If-Unmodified-Since, If-None-Match or If-Match (see HTTP Headers). In this case the web-server decides to deliver a new document if there are changes or just sends the response code 304 Not Modified without a body.
One last thing regarding the use of members (and especially static members): I'd refactor this code and the only item which would be left as static is static void main(). You can see the static members as some kind of global variables. Observing something like 'the connection is returning the same message' might be a effect of inappropriate exception handling and usage of static members.

Related

Can I use wait() in a RESTful webservice?

I have a RESTful webservice which I use for a server on NetBeans.
This webservice should get many requests from clients (multiplayer game).
I'm still new to this topic but if I understand it right- every call from clients to my webservice is a thread safe - because every connection to the web service is on a different thread (all my variables are inside the webservice methods) is this true?
And this brings me to my question:
Can I use wait(); inside a webservice method? let's say I'm waiting for two client connections, so the second connection will use notifyAll();
But since the webservice is not really a thread I don't know if it's possible to use these methods there? What should I use instead??
This is my webservice:
#Path("/w")
public class JSONRESTService {
String returned;
#POST
#Consumes("application/json")
#Path("/JSONService")
public String JSONREST(InputStream incomingData) {
StringBuilder JSONBuilder = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
String line = null;
while ((line = in.readLine()) != null) {
JSONBuilder.append(line);
}
returned = "transfer was completed";
// This is what I'm trying to add but I know that I can't:
// count is a static variable, every new connection will increase this value
// only one player is connected
if (Utility.count == 1)
wait (); //wait for a 2nd player to connect to this webservice
// 2nd player is connected to this webservice
if (Utility.count == 2)
notifyAll (); // notify the 1st player
} catch (Exception e) {
System.out.println ("Error Parsing: - ");
returned ="error";
}
System.out.println ("Data Received: " + JSONBuilder.toString ());
return (returned);
}
}
Client:
JSONObject jsonObject = new JSONObject("string");
// Step2: Now pass JSON File Data to REST Service
try {
URL url = new URL("http://localhost:8080/w/JSONService");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(jsonObject.toString());
out.close();
//string answer from server:
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while ((line = in.readLine()) != null) {
sb.append(line);
System.out.println("\n"+line);
in.close();
} catch (Exception e) {
System.out.println("\nError while calling JSON REST Service");
System.out.println(e);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
} } }`
You can always use wait() and notify() as it affects the thread the code is running on. Whether or not you should use it depends on the situation.
If you want a queue of players then use a queue :)
A little example I knocked up...
#Path("/w")
public class JSONRESTService {
private static BlockingQueue<Player> queue = new ArrayBlockingQueue<>(999);
#POST
#Consumes("application/json")
#Path("/JSONService")
public String JSONREST(InputStream incomingData) {
Player thisPlayer = ...; // Get player from session or something
System.out.println (thisPlayer.getName() + " starting...");
try {
if (queue.isEmpty()) {
System.out.println ("waiting for an opponent");
queue.add(thisPlayer);
synchronized (thisPlayer) {
thisPlayer.wait();
}
} else {
System.out.println ("get next in queue");
Player opponent = queue.take();
opponent.setOpponent(thisPlayer);
thisPlayer.setOpponent(opponent);
synchronized (opponent) {
opponent.notify();
}
}
System.out.println (thisPlayer.getName() + " playing " + thisPlayer.getOpponent().getName());
} catch (Exception ex) {
ex.printStackTrace();
}
}
static class Player {
private String name;
private Player opponent;
Player (String name) {
this.name = name;
}
public String getName() {
return name;
}
public Player getOpponent() {
return opponent;
}
public void setOpponent(Player opponent) {
this.opponent = opponent;
}
}
}
Yes. All local variables inside methods are thread-safe. Class fields variables might be thread-safe or might be not. It is up to you. If rest controller has singleton scope (usually by default it has) that's mean that class fields are shared among the all requests.
So, technically you can use some shared lock object to synchonize on it. Try to do it. But it's better to do it in async mode. See Reverse Ajax Comet technique with long polling in this article.
Alternatively you might use Reverse Ajax with Websockets and send 'Transfer was received' back to client without any idle.

non-static variable cannot be referenced from a static context java [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 7 years ago.
I am writing a program to connect to the console pixel example sketch with java. Im still prety new and I got this error:
non-static variable fast cannot be referenced from a static context
I don't know what the error means but my code is:
package javaapplication5;
import java.net.*;
import java.io.*;
import java.util.Scanner;
/**
*
* #author preferreduser
*/
public class JavaApplication5 {
int fast = 0;
public static void main(String[] args) throws IOException {
Scanner x = new Scanner(System.in);
System.out.print("Yun ip: ");
String IP = x.nextLine();
System.out.println("Loding...");
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/1");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/0");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
System.out.println("Connected to YUN on "+ IP);
OUTER:
while (true) {
Scanner y = new Scanner(System.in);
System.out.print("> ");
String str = y.nextLine();
switch (str) {
case "on":
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/1");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
} break;
case "off":
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/0");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
} break;
case "help":
System.out.println("");
System.out.println("on exit");
System.out.println("off help");
System.out.println("");
break;
case "exit":
try {
// Create a URL for the desired page
URL url = new URL("http://"+ IP +"/arduino/digital/13/0");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
} break OUTER;
}
if ( fast == 1 ){
URL oracle = new URL("http://"+ IP +"/arduino/digital/13");
try (BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
}
} else {System.out.println("Success");}
}
}
}
I want to connect to an arduino yun and type commands like on or off and that part worked. I wanted to add an optional option fast to eliminate connecting to http:// * /aruino/digital/13 each time you typed in a command to make things faster. This was my start. I'm going to add a command for it but I can't until I get this figured out
You can access member variable of a class directly only by making it static. Om making an variable static one more method to access it is by class_name.variable_name.
Otherwise you have to make an object of the class and then you can access that varible through that object.
Example:
either you change
int fast=0; to static int fast = 0;
or you do this
JavaApplication5 j5 = new JavaApplication5(); and now access the variable by j5.fast and do further calculations.
change int fast = 0; to static int fast = 0;
You are using variable fast in main method which is a static method. All the variables that are used in any static method should be static. The reason is static method is common for a class, it does not depend on the instance of the class. So it can not use any instance variable(unless you specify which particular instance to use) inside it because the method does not know which instance variable to use.

Java program hangs on TTransport transport = new THttpClient("this.host.com")

I'm writing a Thrift application that can take input from an outside website and process it. However, the program is hanging at the line:
TTransport transport = new THttpClient("this.host.com");
And I'm not sure why it's happening. It's not giving me any error messages and such but it also doesn't progress forward. It just hangs when I receive a request from a client that calls buildModel(). Here's my code:
private void buildModel() throws UnknownHostException {
// Map of user preferences by Mahout user id
FastByIDMap<Collection<Preference>> userIDPrefMap = new FastByIDMap<Collection<Preference>>();
System.out.println("Building model");
try {
TTransport transport = new THttpClient(this.host);
TProtocol protocol = new TBinaryProtocol(transport);
MyCustomDatabase.Client client = new MyCustomDatabase.Client(protocol);
ConnectionParams con_params = new ConnectionParams();
con_params.setUser(this.username);
con_params.setPassword(this.password);
Connection con = client.open_connection(con_params);
ResultSet res = client.execute_sql(con, "select * from " + this.database + "." + this.tableName, null);
for (Tuple t : res.getTuples()) {
List<ByteBuffer> cells = t.getCells();
int userID = Integer.parseInt(new String(cells.get(0).array()));
int itemID = Integer.parseInt(new String(cells.get(1).array()));
int ratingValue = Integer.parseInt(new String(cells.get(2).array()));
Collection<Preference> userPrefs = userIDPrefMap.get(userID);
if (userPrefs == null) {
userPrefs = Lists.newArrayListWithCapacity(2);
userIDPrefMap.put(userID, userPrefs);
}
userPrefs.add(new GenericPreference(userID, itemID, ratingValue));
}
} catch(Exception e) {
e.printStackTrace();
}
this.delegate = new GenericDataModel(GenericDataModel.toDataMap(userIDPrefMap, true));
}
Any help greatly appreciated!
Its hard to tell exactly what the issue is but some things to note:
1) new THttpClient(this.host) should take a URI string not a host name. In the Thrift Java library this is the constructor that you are calling:
public THttpClient(String url) throws TTransportException {
try {
url_ = new URL(url);
this.client = null;
this.host = null;
} catch (IOException iox) {
throw new TTransportException(iox);
}
}
If you are just passing a host name instead of a URI you probably won't get the results you expect.
2) In the javadoc for the THttpClient class they suggest using the Apache HttpClient constructor instead of the default HttpURLConnection one anyhow, both for better performance and to avoid exhausting the open file descriptor limit under heavy load.
I would suggest that try passing a valid URI to the constructor, and if that does not work try using HttpClient instead, or use both of those measures.

TCP Time out while fetching MetaData for Radio Streaming in Blackberry

I am creating an Radio app which will stream from the various different URL's.
Now while fetching the stream from the url's we also want the content that is which song is playing on i.e metadata.
Now for this i run another thread in a TimerTask that will create the HTTP connection say like after every 40 seconds.
While using the simulator for OS 5 and above there is no problem but when i run the same thread for two to three iterations the metadata is recieved consistently but afterwards we got the TCP time out exception and this halts the over all system and device even losts the internet connection.
Can you please suggets any alternatives which can solve the problem.
I am also sharing the code that will get the metaData from the streaming URL.
class metaData extends Thread {
public void run() {
try {
boolean metaDataCheckBit = false;
StreamConnection streamConnection = (StreamConnection)Connector.open(url);
HttpConnection httpConnection = (HttpConnection)streamConnection;
httpConnection.setRequestProperty("Icy-metadata", "1");
int httpStatus = httpConnection.getResponseCode();
if (httpStatus==HttpConnection.HTTP_OK) {
String mint = httpConnection.getHeaderField("icy-metaint");
InputStream inputStream = streamConnection.openInputStream();
int length = Integer.parseInt(mint);
int b = 0;
int count = 0;
while(count++ < length){
b = inputStream.read();
}
int metalength = ((int)b)*16;
if (metalength <= 0) return;
byte buf[] = new byte[metalength];
inputStream.read(buf,0,buf.length);
final String metaData = new String(buf);
final String streamTille =
metaData.substring(14, metaData.indexOf("StreamUrl")-2);
System.out.println("Stream Title--------"+streamTille);
UiApplication.getUiApplication().invokeAndWait(new Runnable(){
public void run() {
try {
titleRf.setText(streamTille);
} catch (Exception e) {
System.out.println("Exception: " + e.toString());
}
}
});
}
} catch (Exception e){
System.out.println("Exception in meta data...."+e);
}
}
}

Why is my client-server app repeating expected output?

I am having some problems figuring out this issue. I have a server that takes a string from a client, the first four characters of which act as a 'command' of sorts. rep: replaces the stored string, app: appends to it.
This works mostly fine, however when I use the rep: command, it repeats the client's string twice. So, if I inputted rep:foo the server returns 'foofoo'. I have tried analysing the code and can't see any immediate problems.
When adding some test output commands to both the server and the client, to see what the variables hold, I get the result as expected (the string input on the client minus the command characters). The code for both classes is below:
Server:
import java.io.*;
import java.net.*;
import java.util.*;
public class SynchServer
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
final int PORT = 1234;
Socket client;
ClientHandler2 handler; // thread for client
int clientCount = 0;
//set up server socket
try
{
serverSocket = new ServerSocket(PORT);
}
catch(IOException ioEx)
{
System.out.println("Cannot open socket!");
System.exit(1);
}
// client connections and related actions:
do
{
// wait for client...
client = serverSocket.accept();
System.out.println("Client accepted...\n");
// assign client a connection number
clientCount++;
// create thread
handler = new ClientHandler2(client, clientCount);
// run thread
handler.start();
}while(true);
}
}
class ClientHandler2 extends Thread
{
// declare thread variables
private Socket client;
private Scanner input;
private PrintWriter output;
private static String text = "";
int clientNum; // picked up from main
// constructor - set up socket and streams
public ClientHandler2(Socket socket, int clientCount)
throws IOException
{
client = socket;
clientNum = clientCount;
// streams...
// from client
input = new Scanner(client.getInputStream());
// to client
output = new PrintWriter(client.getOutputStream(), true);
}
// thread actions:
public void run()
{
String head, tail, received;
do
{
// read in line from client
received = input.nextLine();
// split input line in two - head is first four
// characters for the command, tail is for rest of
// line - the text to be manipulated:
head = received.substring(0,4);
tail = received.substring(4);
// find command and choose relevant method to execute
if(head.equals("rep:"))
{
replaceText(tail);
}
else if(head.equals("app:"));
{
appendText(tail);
}
// no further tests needed - makes server ignore
// invalid commands (Add some kind of message?)
// send modified (or not) string back to client:
output.println(clientNum + ": " + text);
}while(!received.equals("QUIT"));
// close socket connection
try
{
System.out.println("Closing connection...");
client.close();
}
catch(IOException ioEx)
{
System.out.println("Unable to close connection!");
}
}
private synchronized void replaceText(String value)
{
text = value;
}
private synchronized void appendText(String value)
{
text += value;
}
}
Client:
import java.io.*;
import java.net.*;
import java.util.*;
public class SynchClient
{
public static void main(String[] args) throws IOException
{
// declare variables
InetAddress host = null;
final int PORT = 1234;
Socket socket;
Scanner networkInput, keyboard;
PrintWriter networkOutput;
// assign host address:
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("Host ID not found!");
System.exit(1);
}
// Set up socket to server and IO streams:
socket = new Socket(host, PORT);
// from server
networkInput = new Scanner(socket.getInputStream());
// to server
networkOutput =
new PrintWriter(socket.getOutputStream(),true);
// user input
keyboard = new Scanner(System.in);
String message, response;
do
{
// get user input
System.out.print("Enter message ('QUIT' to exit): ");
message = keyboard.nextLine();
// validate user input - ensure string is >= 4 chars
// long
while(message.length() < 4)
{
System.out.print("Try again: ");
message = keyboard.nextLine();
}
// send message to server:
networkOutput.println(message);
// received response from server
response = networkInput.nextLine();
// output server response
System.out.println(response);
}while(!message.equals("QUIT"));
}
}
I really cannot figure this out, and while not vitally important, I'd like to know for reference what is going wrong. So, any hints would be nice.
Dude.
else if(head.equals("app:"));
see the semicolon at the end there? :-) Remove that, and your problems should magically go away.
edited to add: The semicolon at the end of the else block terminates the condition and the code in the brackets below is thus executed every iteration of the while loop in ClientHandler2.run()

Categories