There are times that you want to run a java task periodically. In this case, you may consider using java timer class. The following is an example how you might use it. This example also demonstrate how to use java to send Http request.

import java.util.*;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;

class HttpRequest extends TimerTask {
        String query_code;

        HttpRequest (String query_code)
        {
            this.query_code = query_code;
        }

        @Override
        public void run()
        {
            String request_url = "http://host/controller/method.html?q=" + query_code;
            try
            {
                request_url = request_url.replaceAll(" ", "%20");
                System.out.println("Sending request to " + request_url);
                URL url = new URL(request_url);
                InputStreamReader reader = new InputStreamReader(url.openStream());
                BufferedReader in = new BufferedReader(reader);
                String response;

                while((response = in.readLine()) != null)
                {
                    String[] temp = response.split(";");

                    for(String code : temp)
                    {
                        System.out.println(code);
                    }
                }

                in.close();
            }
            catch (Exception e)
            {
                System.err.println("Error posting request to " + request_url);
            }
        }
    }
class Test {
    public static main (String[] args)
   {
        HttpRequest request = new HttpRequest("query");
       Timer request_timer = new Timer(true);
       //0 means no delay, 5000 is the interval
       request_timer.schedule(request, 0, 5000);
   }
}

So you need to set up a class that extends TimerTask class. Do note that this class needs to override the method ‘run’. This is the method that actually being scheduled to run periodically. Then you need to create a Timer instance and use the schedule method to schedule a new task.
Read the rest of this entry »

Recently I am working on a comet application project. The comet server is written in java and on top of Grizzly framework. The web application is written in ruby on rails. One of the things that I need to do is to use Java to send http request to the ruby on rails server to get some data. So here is how you could send a http request to get a text file response.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class HTTPRequest {
   public static void main (String[] args) {
      String request_url = "http://host/controller/method.html?code=Straight Times";
      try
      {
           URL url = new URL(request_url);
           InputStreamReader reader_stream = new InputStreamReader(url.openStream());
           BufferedReader bf = new BufferedReader(reader_stream);
           String line;
           Vector details = new Vector();
           while ( (response = bf.readLine() ) != null )
           {
               for ( String detail : response.split(";") )
               {
                    details.add(detail);
               }
           }
           System.out.println(details.toString());
           bf.close();
      }
      catch (Exception e)
      {
           System.err.println("Error sending http request: " + e.getMessage());
       }
   }
}

It looks quite straight forward. However, there is a bug here. If you run the example above, it will always give you exception with error message “Bad Server request status 400″. Do notice that the following line:

String request_url = "http://host/controller/method?code=Straight Times";

This is where it gives bugs. Notice that there are some spaces between the request url. The request won’t be posted properly. The correct solution is to encode the request url or simply replace the empty space with %20 in this case.

String request_url = "http://host/controller/method?code=Straight Times";
request_url = request_url.replaceAll(" ", "%20");

Read the rest of this entry »