Wednesday, June 28, 2006

That Dang URLConnection Class Bit Me Again

Over the past couple days, I've been reinventing RPC mechanisms by making use of passing serialized java object to and from servlets. I ran into some complexities that I had forgotten existed when trying to use the URLConnection class. In short the trick is to not call URLConnection.getInputStream() until you are finished writing to the URLConnection's output stream. The JavaWorld article Dodge the traps hiding in the URLConnection class helped me again. Here is some example code for posting a serialized object to a servlet and retrieving an object response from the servlet: -------------------------------------------------------------------

public class Client {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost:8080/myContext/myServlet");

        URLConnection servletConnection = url.openConnection();

        // inform the connection that we will send output and accept input

        // Don't use a cached version of URL connection.

        // Specify the content type that we will send binary data
        servletConnection.setRequestProperty("Content-Type", "application/x-java-serialized-object");

        // get input and output streams on servlet
        ObjectOutputStream os = new ObjectOutputStream(servletConnection.getOutputStream());

        // send your data to the servlet
        String s = "Some object I am sending to the server";

        ObjectInputStream is = new ObjectInputStream(servletConnection.getInputStream());

        Object o = is.readObject();

        String returnedValue = (String) o;


import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class InsertFeature extends GenericServlet {

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {

        String contentType = "application/x-java-serialized-object";

        String value = "Thank You";

        try {

            // read an object
            ObjectInputStream is = new ObjectInputStream(request.getInputStream());

            String receivedString = (String) is.readObject();
        } catch (IOException ex) {
            value = "ERROR";
        } catch (ClassNotFoundException ex) {
            value = "ERROR";

        ObjectOutputStream oStream = new ObjectOutputStream(response.getOutputStream());


Tuesday, June 06, 2006

Java Concurrency Annotations

I'm in the process of reading Java Concurrency in Practice by Brian Goetz et al. So far this is a terrific book. It is extraordinarily well written and very informative. I'm intrigued by a set of annotations in the book (@ThreadSafe, @NotThreadSafe, @Immutable, and @GuardedBy). The book suggests these annotations are ideal for use by static analysis tools. I think this is a phenomenal idea and a highly effective use of annotations. I started looking to see if any of the tools I use have rules to utilize these annotations. I did find reference to the JCIP annotations on the FindBugs site, but nothing from PMD or CheckStyle yet. Anyhow downloads, documentation, and source of the JCIP annoations can be found here. Check them out if you need a way to document the intent of thread safety in your classes as well as be warned if the classes are modified in such a way that your original concurrency design is broken by maintenance.

Monday, June 05, 2006

Read What I Read

I currently use the google reader to aggregate rss/atom feeds from many of the sites and communities that I find interesting. I have just found a way to share the articles I find interesting. Old News: With Google Reader I can apply labels to articles. New News: I can make any of my labels public and share them with you. I have applied a javascript clip to my blog template allowing you to view the articles I mark as useful or interesting. You should be seeing a section titled "Interesting Articles" in the right area of this page.