Sunday Mar 09, 2008

Performance analysis and diagnostic aid for Java applications

I just tried the latest iteration of the VisualVM tool, which just released a beta. Very impressive, especially the GlassFish related capabilities. I wonder if there is comparable tooling for other application servers. Here is the functionality in a nutshell:

  • Monitoring and performance analysis for Java SE and EE
  • Integrates the features of several JDK tools
    • jps, jinfo, jstack, jmap, and more
  • Provides lightweight memory and CPU profiling
    • Designed for both development and production
  • Can observe JDK 1.4.2 or higher
  • Provides APIs for writing add-on plugins
Nota bene: If you want to monitor GlassFish apps you'll have to get the plugin from the tool's update center: Tools | Plugin and also some minimal GlassFish configuration is required: In the admin console go to Application Server | Monitor | Runtime | Configure Monitoring and set Web Container to "High". You can also try and use this http://localhost:4848/configuration/monitoringService.jsf?configName=server-config  to open the configuration

Monday Feb 25, 2008

Embedding charts in web applications using NetBeans

You'll be surprised how easy is to build a web application that charts data using the jMaki framework and NetBeans 6. So let's assume that you want to plot the % of revenue an organization receives every month of an year. A pie chart would likely be your best bet:

So how does one build such a chart? These few steps should get you started:

  • First of all you'll need to get install the jMaki plugin from the NetBeans update center (Tools | Plugins | Available Plugins | jMaki Ajax support)
  • Next get the corresponding charting library - a java.net project and install it in NetBeans (Tools | Palette | Add jMaki Library)
  • Create a new web project and select the jMaki framework in the project wizards dialog
  • Drag and drop the "Google Pie"component from the palette (look for a section called - jmaki-charting-widget ...) after the <body> tag in the index.jsp default page of the project
  • Deploy the application and customize as needed

If you are not a Java guy and want to achieve the same results in a PHP project and application use the following code snippet to build the chart.

<?php
  addWidget( array(
                         "name" =>"jmaki.charting.plotkit.pie",
                         "args"=>"{colorScheme:2}",
                         "value"=>"{
                   xAxis : {
                   title : 'Months',
                   labels : [{ label : 'January'},
                             { label : 'February'},
                             { label : 'March'},
                             { label : 'April'},
                             { label : 'May'},
                             { label : 'June'},
                             { label : 'July'},
                             { label : 'August'},
                             { label : 'September'},
                             { label : 'October'},
                             { label : 'November'},
                             { label : 'December'}
                             ]
                             },
                             data : [
                  {label : 'Set 1', values : [25, 45, 25, 45, 50, 25, 35, 25, 25, 20, 35, 45] },



                  ]
              }";));
?>

Sunday Feb 17, 2008

What are the web servers that power the internet?

So how can you tell what server is being used to run a website? Here is a simple Java program that can help one figure out the kind of server that serves a particular website.

package whataretheyrunning;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/\*\*
 \*
 \* @author octav
 \*/
public class SnoopURL {

    String URLString = null;

    public SnoopURL(String URLString) {
        this.URLString = URLString;
    }

    public void snoop() {
        try {
            // build URL
            URL url = new URL(URLString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setRequestMethod("GET";);
            conn.setDoOutput(true);

            // start talking
            conn.connect();

            // int length = conn.getContentLength();
            // System.out.println(" \*\*\*\* the length of the content is " + length + " for URL " + URLString);

            System.out.println(" \*\*\*\* Server type is:  " + conn.getHeaderField("Server";) + "\\n\\n";);

            // get more information
            Map map = conn.getHeaderFields();
            for (int i = 0; i < map.size(); i++) {
                System.out.println(" \*\*\*\* " + conn.getHeaderField(i));
            }

            // cleanup
            conn.disconnect();
        } catch (MalformedURLException ex) {
            Logger.getLogger(SnoopURL.class.getName()).log(Level.SEVERE, null, ex);
        } catch (java.io.IOException ioe) {
            ioe.printStackTrace();
        } finally {
            // do whatever cleanup ...
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    @Override
    public String toString() {
        return super.toString();
    }
}

The output of this program is something like this:

\*\*\*\* Server type is: Sun-Java-System-Web-Server/6.1

\*\*\*\* HTTP/1.1 200 OK
\*\*\*\* Sun-Java-System-Web-Server/6.1
\*\*\*\* Mon, 18 Feb 2008 04:46:48 GMT
\*\*\*\* text/html;charset=UTF-8
\*\*\*\* policyref="http://www.sun.com/p3p/Sun_P3P_Policy.xml", CP="CAO DSP COR CUR ADMa DEVa TAIa PSAa PSDa CONi TELi OUR SAMi PUBi IND PHY ONL PUR COM NAV INT DEM CNT STA POL PRE GOV"
\*\*\*\* Servlet/2.4,JSP/2.0
\*\*\*\* chunked
\*\*\*\* Starload=star-fep5; Path=/
 

Now let's look at some popular websites.

Sun's www.sun.com is using the old Web Enterprise server, version 6.1 (the same is true for java.sun.com). The site the publishes this blog: blogs.sun.com is powered by Sun-Java-System-Web-Server/7.0 (a newer version of the same web server). Google seems to use their own version: gws. Yahoo does not tell you (returns null). Microsoft eats their own dog food: Microsoft-IIS/7.0. Nice. IBM runs something called: IBM_HTTP_Server (I bet is Apache's web server).



 

Tuesday Dec 18, 2007

Patterns in Java & JavaFX Script

One of the thoughts that I had the other day was related to design patterns in JavaFX Script. Patterns are quite popular with developers. The are books on this topic and lots of web resources. The question is how could write, for instance a Singleton, in JavaFX Script.
 
In Java it would look like this:

public class OnlyOne {

    private static OnlyOne singleton = null;

    private OnlyOne() {

    }

    public static synchronized OnlyOne getInstance() {
        if (singleton == null) {
            singleton = new OnlyOne();
        }

        return singleton;
    }
}

/\*\*
\* Test program to prove we've coded the singleton correctly
\*/
public class Driver {

    public static void main(String[] args) {
        // one would try this OnlyOne one = new OnlyOne();
        // and it won't work due to the private constructor
       
        // get an instance
        OnlyOne one = OnlyOne.getInstance();
        System.out.println("Myfirst attempt: " + one.toString());
       
        // get another instance to see if they are the same
        OnlyOne two = OnlyOne.getInstance();
        System.out.println("My second attempt: " + two.toString());
    }
}

The result of running the Driver program is: 

Myfirst attempt: onlyone.OnlyOne@9b49e6
My second attempt: onlyone.OnlyOne@9b49e6

As expected, any time one is trying to get a handle to the OnlyOne object, the same instance is returned.

Monday Dec 03, 2007

It used to be a Java IDE

NetBeans started as a Java IDE, one of the first open source projects at Sun. It was long time ago ('99). Since then, it has evolved rapidly, especially in the last couple of years and became more than an IDE. It is also a platform. In addition , NetBeans has become a delivery vehicle for cool technologies from Sun and the Open Source (e.g. JRuby, GlassFish, Tomcat). Some people may argue that, lately, NetBeans has become choice IDE for Ruby developers. How about the support for C/C++ and UML? It is all there.

 

Nota Bene:

 
 

 

Sunday Nov 18, 2007

Java RESTful web services made easy with NetBeans 6

Web Services have become synonymous with interoperability (A "feature" demanded by the customers that need to make both .NET and Java, IT investments). There are couple of approaches to exposing functionality as a web service. The more established one is using SOAP & WSDL and the "newer" Representational State Transfer (REST) style web services. Of course tooling has not been far behind the trends and all the respectable vendors have some support for web services. JSR 311 has been coming along in describing an API developers can use to build RESTful web services.

In NetBeans 6, you'll find very useful functionality that enables you to build and test a RESTful web service. The best way is to start with with short tutorial. To learn more about the JAX-RS API, take a look at this blog.

By the way, since RESTful Web Services support in Java is still an evolving technology, the tooling resides on the NetBeans update center. Just use the Tools | Plugins menu and look for "RESTfull Web Services" in the Web & Java EE category. Once you install the plugin, you'll also get a few samples that will help you get started.

Get busy!
 

Wednesday Nov 14, 2007

A thread dump worth a 1000 bug reports

The old proverb: "a picture is worth a thousand words", has an interesting mapping in the world of programming. On the Java platform one has a few facilities that enable a developer to get a stack trace that can point to the cause of a UI freeze, slow down in an application performance, etc. A thread dump can save a developer a lot of time in troubleshooting tough problems. In the old days one could start the application and a terminal and call CTRL + Break on the Windows platform or kill -QUIT pid on the Unix platform. Of course if one has access to the sources you can force the application to show you the call stack by doing something "un-natural" such as Thread.getCurrentThread().dumpStack(). Keep in mind that this will crash you application, so don't do it on production code. This wiki entry has more details on how to, gently :-), get a Thread dump.

In a recent conversation with Tor I learned some more details. For instance, on the Windows platform, using Java 6 one could employ the jps -ln command to get the process id of Java application and then use the jstack command on that process id (jstack pid) to get a thread dump. Nice documentation can be found here.

If you really in the mood to experiment, take a look at the Visual VM project that was just launched on java.net. You'll find a cool tool, based on the NetBeans platform, that helps monitor and profile Java applications. It only runs on top on Java 6, however it can work on applications based on JDK 1.4.2 or later.
 

Saturday Jul 07, 2007

Public calendar


To migrate your events from Apple iCal to Google Calendar, follow these steps:

  1. In Apple iCal, select the calendar from the list of calendars in the iCal window.
  2. Select "File" > "Export."
  3. Name the calendar, then save it.
  4. In Google Calendar, ensure that you've created the calendar that you'd like to migrate events to. Then, at the top of the calendar list on the left, click the "Add" down-arrow button and select "Import Calendar."
  5. Click "Browse" and select the appropriate file, then select "Open."
  6. From the drop-down menu, select the calendar to which you'd like to import events.
  7. Click "Import" to complete the import.
One should be careful when exporting the calendar to make sure internal information is protected and not exposed externally. The export operation will include the details of the events as well.

Thursday May 31, 2007

Java FX @ Java One

Everyone (especially in the US) loves a winner and at every Java One you have one product, technology or event that makes the biggest splash. 2007 is shaping up as the year of Java FX. In a nutshell: "JavaFX comprises a comprehensive set of runtime environments, widgets, development tools, and scripting environments At the 2007 JavaOne Conference, Sun introduced two products in the JavaFX family: JavaFX Script and JavaFX Mobile." You owe to yourself to kick the tires on Java FX Script, so here are some pointers to some resources on the web.

As far as I am concerned, this year's conference was one of the better ones, that I've attended (since '96). I have not been so excited since 2003 when we previewed Java Studio Creator. One of the cool things about the conference is the social networking aspect. You end up having a beer with entrepreneurs that will become the next Jeff Bezos, renowned technologists and lot's of other folks with who you collaborate during the year, but they always at the end of the phone line or just a name in the CC list.
 

Thursday May 03, 2007

What's on tap @ NetBeans Day

The NetBeans platform and IDE has evolved tremendously in the last couple of years. Version 6 is around the corner and the Sun will be releasing a preview at NetBeans Day / Java One. So what's on tap? Support for native and dynamic languages (Ruby, JavaScript), Java Studio Creator's ease of use available for desktop applications, integrated profiler, rapid visual web application development, UML, REST W/S, lots of productivity features, etc. The best way to get more info is by attending NetBeans Day (details below).

May 7th, Moscone 

NetBeans Day

Highlights

  • NetBeans Day Opening Session: Jonathan Schwartz and Rich Green
  • Lunch with the Java Posse
  • NetBeans 6.0 IDE
  • Partner Showcase
    • CollabNet, YASU Technologies
  • Swing GUI Building With Matisse: Chapter II
  • NetBeansMobility
  • JRuby: Understanding the Fuss
  • Partner Showcase
    • Intland, USDA, Xoetrope
  • Closing Session: James Gosling

The first 400 attendees will receive a free copy of Rich Client Programming: Plugging into the NetBeans Platform. We will also be giving out 1 GB USB flash drives, loaded with the latest NetBeans 6.0 Milestone. Throughout the day, we will be giving out special edition NB 6.0 T-shirts and NetBeans cubes. Pay attention, you may have to earn your T-shirt by asking or answering a question.

Friday Apr 20, 2007

Java 6 available on Ubuntu Feisty via Multiverse

The good news:

  • Java 6 is now available on Ubuntu 7.04 (Feisty Fawn) from the multiverse repositories
  • A couple of simple commands will install Java 6 on your system:
    1. Get and install Java: sudo apt-get install sun-java6-jdk
    2. Make Java 6 default: sudo update-alternatives --config java
    3. Edit sudo vi /etc/jvm the first entry should point to your JDK /usr/lib/jvm/java-6-sun
    4. Optionally you can install the jre and firefox plugin: sudo aptitude install sun-java6-jre sun-java6-plugin

Of course, you'll be better off following the documentation available on the Ubuntu website.

The not so good news:

  • Java 6 is still not default on Ubuntu 7.04 (after installing the OS you get 1.4.2)
  • You can install Java 6 from the Applications | Add/Remove, however you have to fish for it and "Show" All Available Applications when you search for it

In addition to Java, Glassfish v1 and NetBeans 5.5 are also available from the multiverse repositories. IMHO this software stack makes Ubuntu very competitive vis-a-vis RedHat / JBoss. I'll blog some other time on this topic. 

Monday Apr 09, 2007

Game geeks will love it

Here is an implementation of the popular "Lines" Russian game written by Alexei Mokeev in F3. Business logic and cool graphics in less than 350 lines of code. Take a look at the snippet below to see how one leverages Java the language, and of course this runs on the JVM.

 

import f3.ui.\*;
import f3.ui.canvas.\*;
import java.lang.System;
import java.lang.Math;

 ...

operation Lines.moveBallTo (to:LinesCell) {
    var path:LinesCell\* = [to];
    while(to.marker <> 0) {
       to = getMinimumAround(to);
       insert to as first into path;
    } //OK. We have Path

    activeCell =-1;
    var frm:LinesCell = path[0];
    to = path[sizeof path -1];

    var l:Integer = sizeof path;

    if (l == 2) {
        to.ballColor = frm.ballColor;
        frm.busy = false;
        to.busy = true;
        changed = true;
        dropBalls();
        return;
    }

    // Clearing FROM
    to.ballColor = frm.ballColor;
    frm.busy = false;

    for (cell in path[indexof . <l-1]) (dur (l-2)\*100 linear) {
        cell.marker = -10; //Field in movement
    }

    for (cell in path[indexof . < l]) (dur (l-2)\*200 linear) {
        cell.marker = -1; //Stop markup
        if ((cell.x == to.x) and (cell.y == to.y)) { //Ok. We at the end
            to.busy = true;
                   
            for (c in fld[n|n.marker == -10]) { c.marker = -1;}
            changed = true;
            dropBalls();
        }
    }

}
 

Take a look at the screen shot of the game running on my Mac.

 

Friday Feb 16, 2007

"Pretty" Java

Not long ago I went to Monrovia (East of LA) to meet the Sun folks working on SOA toolability and runtimes. I gave a demo of Java Studio Creator and also learned about their work on developing a new technology. Like any presentation it's the demo that makes or breaks the deal and I was blown away by what Chris Oliver has shown that day.

The technology is called F3 - "form follows function" (the name will likely change), a declarative Java scripting language, which among other features, enables easy binding of UI element to data. Here is a link to his blog and a demo of F3 in action. F3 takes advantage of Java 2D and Swing and enables developers to rapidly develop "icandi" applications. This technology does a better job than anything else I've seen in exposing the power of Java for developing GUI applications.

There is a lot of interest around this project and perhaps if it becomes open source soon, the community will help the technology evolve to a point where is has critical mass and wide industry adoption. Having support for something like F3 in an IDE, such as NetBeans would also help. I suspect that this will happen shortly :-) Stay tuned.
 

About

octav

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today