RESTful service with TomEE and IntelliJ IDEA

In this tutorial you’ll build a basic RESTful service using the Java EE APIs, IntelliJ IDEA, and TomEE.

Overview

  1. Setup the TomEE application server in IntelliJ.
  2. Create a new project.
  3. Write a RESTful Java class.
  4. Run.

Used in This Tutorial

Prerequisites

  • Download and install IntelliJ.
  • Download and unpack TomEE.

Tutorial

  1. Setup the TomEE application server in IntelliJ.
    1. Start IntelliJ, and on the startup screen go to Configure -> Settings -> Application Servers.Screen Shot 2013-06-04 at 1.54.26 PM
    2. Click the “+” button (top center) to add an application server.
    3. Select “TomEE” for the type of server.Screen Shot 2013-06-04 at 1.55.41 PM
    4. Enter the directory where you unpacked TomEE.Screen Shot 2013-06-04 at 1.56.55 PM
    5. Click “Ok” and “Ok” again.  Then click the little back button in the start up screen to go back to the main menu.
  2. Create a new project.
    1. Select “Create New Project” in the IntelliJ startup screen.
    2. Select “Java Module”
    3. Name it “MyFirstRESTfulService”.
    4. There’s an option here to select your JDK. You may need to configure your JDK, first. This is an IntelliJ requirement, and I don’t remember how I did this, I hope it’s straight forward enough for you.
    5. Click “Next”.
    6. Select “Application Server” then select “TomEE” from the drop down box on the right.Screen Shot 2013-06-04 at 2.00.42 PM
    7. Select “RESTful service” and “Set up library later”.
    8. Click “Finish”.
  3. Write a RESTful Java class.
    1. Double click the project name to expand the tree view and you will see the “src” directory.
    2. Right click “src” and create a package (select New -> Package) called “org.mybiz”.
    3. Right click “org.mybiz” and create a class (select New -> Java Class) called “Greeter”.
    4. Edit the Greeter class until it reads like this:
      package org.mybiz;
      
      import javax.ws.rs.GET;
      import javax.ws.rs.Path;
      
      @Path("greeting")
      public class Greeter {
          @GET
          public String sayHi() {
              return "Hi!!";
          }
      }
  4. Run. Click the green play button to run the RESTful service. You should see messages in an output window at the bottom of IntelliJ. Look for a line like
    INFO: REST Service: http://localhost:8080/greeting/*  -> Pojo org.mybiz.Greeter

    This indicates the service was successfully deployed at the URL specified. Check for exceptions in case something failed.Screen Shot 2013-06-04 at 2.10.15 PM

Point your browser to the correct URL: http://localhost:8080/greeting/, and it works, yay!Screen Shot 2013-06-04 at 2.11.00 PM

VMware Fusion, Parallels Desktop, and VirtualBox

A review, benchmarks, and experience.

Executive Summary

VMware hogs memory, VirtualBox is slow, and Parallels Desktop causes Windows XP Pro to crash.  Parallels Desktop is the fastest and leanest (smallest memory footprint). VMware is rock solid… always does what it’s supposed to with reasonable efficiency. VirtualBox is free.

Technical Summary

Software Version
VMware Fusion 3.1.1 (282344)
Parallels Desktop 6.0.11826 (r611899)
VirtualBox 3.2.10 (r66523)

Virtual machine configuration:

Setting Value
RAM 512MB
HD 20GB
Cores 2

All tests were performed with an install of Windows XP Professional updated to the latest updates at the time of this article and no virus scanner.

Results

Benchmark results for compilation of a Java project I work on (lower is better). The compiler used is javac that comes with the JDK. I compile the project using make in Cygwin.  These were run with 2 cores.  For comparison I compiled the code on the host operating system using Mac OS X and the included JDK.  It’s interesting to note that Parallels performed better than the native code, perhaps due to a more efficient JDK.

Time to compile a real world project in Java (dual core)

I did not do the tests for dual core in VirtualBox, so here are the results for single core for VMware, Parallels, and VirtualBox.

Time to compile a real world project in Java (single core)

And here is the memory usage for a single virtual machine instance.  This is the real + the virtual memory for both the virtual machine process and the application process (if present, VirtualBox let’s you close the application process after the virtual machine is started, so there is 0 MB for the application process for VirtualBox).  The measurement was taken after running the compilation of the Java project, this is an important point because Parallels does not allocate all the memory specified in the virtual machine settings.  Instead, Parallels allocates however much memory is being used by the virtual machine, which will vary depending on how much memory the applications have allocated.

Total memory consumption (MB)

In this measurement the memory consumption of a freshly booted virtual machine is measured and broken up into real, virtual, application real, and application virtual.  You’ll notice that Parallels Desktop’s memory usage is below the 512 MB the virtual machine was configured at!  This is because Parallels dynamically allocates memory for the virtual machine process.  VMware and VirtualBox allocate all the memory up front.

Memory consumption for a freshly booted virtual machine (MB)

Experience

Briefly, my experience was this: The VirtualBox experience was good, albeit slow, but I found it annoying to switch spaces in OS X because Ctrl-<arrow> wouldn’t work unless I pressed and released the Command key first.  VMware gave me a solid and consistent experience with no hiccups, but that memory consumption makes me cringe.  Parallels experience would have been great, had it not caused Windows XP to crash numerous times!!

Parallels Desktop

Parallels Desktop really disappointed me with numerous Windows XP crashes.  Here’s a summary of some of the crashes.

Installing Windows XP Professional using the automatic Parallels installation (Parallels installs XP for you from the CD) caused XP to crash near the end of the installation, every time!  The crash displays a blue screen of death:

A problem has been detected and windows has been shut down to prevent damage to your computer.

IRQL_NOT_LESS_OR_EQUAL

So I installed XP manually (I did the clicking in the installation instead of Parallels) and I got this error:

Distributed Transaction Coordinator Setup Error.

Sub-component Distributed Transaction Coordinator raised an exception while processing the OC_COMPLETE_INSTALLATION setup message.

d:\nt\com\com1x\dtc\ntdtcsetup\src\cdtccore.cpp (line 611)

Error Code = 0xffffffff

The DTC service could not be installed.

Parallels hung Windows a couple times during Windows update.  Here is an example of one time where it appears to be taking a long time downloading.  So I started to download VMware Fusion after this screen was displayed for an hour.  You can see VMware Fusion is downloading rather fast.

Sometimes Windows Update failed altogether:

You’ll notice in the background the accumulation of screen shots of various Parallels failures.  There are no such failures for VMware or VirtualBox.

I don’t really like to go on like this about how many errors I encountered in Parallels Desktop… but I feel I have a duty to report the facts… however gruesome they may be for Parallels.

Once, I shut down Windows and Parallels did not power off the virtual machine after Windows finished shutting down.  A subsequent boot and shutdown of Windows succeeded and Windows did not report a failed shutdown (confirming that Windows did indeed complete the shutdown).

The display drivers in Parallels seem a bit clunky, when I switch spaces in Mac OS X I get funny results and the virtual machine takes a while to redraw the screen correctly (this is a very brief, but noticeable delay, and is not present in VMware or VirtualBox).

Once, I was typing in the Parallels virtual machine and when I pressed the letter ‘t’ it showed up 4 times instead of once.

Overall I think Parallels Desktop 6 has lots of problems and bugs.  This made for an inconsistent and frustrating experience… in fact, it was this experience that led me to write this review.

VMware Fusion

VMware proved to be consistent, clean, and fast experience.  The memory consumption is high; higher than VirtualBox when you look at the Real memory only.

VMware has easy access control on shared folders; it’s easy to set a folder to “read only”.  Parallels has no apparent feature, or at least it isn’t obvious.  VirtualBox also easily allows for read only access.

VirtualBox

One of my favorite features of VirtualBox is the comprehensive keyboard shortcuts.  I guess I’m one of those keyboard power users at heart.  VirtualBox has shortcuts for most everything you want to do, including shutting down and reseting the virtual machine.  Neither Parallels or VMware have shortcuts for hardly anything.

VirtualBox is slower on the compilation test I performed.  But it doesn’t appear so slow when clicking around.  I’ve been using VirtualBox for years and have been pretty satisfied, but the slow compilation and an influx of money led me to explore alternatives.

I also have Linux installations I use for testing software, and they perform better than Windows under VirtualBox.  In fact, compile times for this java project in Linux (Ubuntu 10) under VirtualBox beat all of the above at about 5 minutes 43 seconds.

The most annoying behavior of VirtualBox is that I must press the Command key before I can use Ctrl-<arrow> to switch spaces in Mac OS X.  This is a highly specific behavior to how I work and have my computer configured, but VMware and Parallels allow me to switch spaces without releasing the keyboard first.

Summary

Parallels is the fastest of them all but the new version Parallels Desktop 6 seems to be buggy and needs some attention from the developers.  VirtualBox is free and strong for Linux, but slow for Windows.  VMware is a solid virtual machine with consistent behavior and performance, but uses lots of memory.

Drag and Drop with Gtk2 and Perl

For the impatient, scroll down to the complete code example.  You may find this to be all you need.

I’ve struggled with drag and drop using Gtk2-perl for years… now I’m sharing my discoveries so you don’t have to struggle, too!

Here are some resources I used to create this article:

I’m mainly concerned with drag and drop within and between a tree or list (Gtk2::TreeView).  This turns out to be a rather complicated subject.  There are three ways to setup drag and drop in a tree view: set_reorderable, enable_model_drag_dest/enable_model_drag_source, and your own low level implementation using drag_dest_set/drag_source_set.  You can only use one of these methods.  The methods will step on each other and badness ensues when you try to use more than one.  set_reorderable is a convenience function for setting up reordering of elements within a tree using drag and drop.  It does not allow items to be dragged between different trees.  For these reasons we will not cover this function here.  Implementing your own low level drag and drop is very powerful and opens doors to unlimited possibilities, but requires a lot of work and I’ve found the middle road to be just fine for applications I’ve worked on.  The middle road (using enable_model_drag_dest and enable_model_drag_source) gives you lots of expected functionality with little effort.

In this tutorial the dragged item is moved and all code assumes the item is to be moved.  If you need the item to be copied, linked, or something else, you’ll need to modify the code.

set_reorderable – caution!

Gtk2::TreeView->set_reorderable – sets up drag and drop for reordering elements in the tree view.  No drag and drop is enabled between other widgets or applications with this method.  This is great until you start to want both reordering and drag and drop to other widgets.  Enabling this feature interferes with calls to drag_source_set and drag_dest_set, because it makes its own calls to those methods.  (note: if you’re using Glade to build your user interface, the “set_reorderable” can be set using Glade, effectively hiding the call to set_reorderable from you).

Basic Overview

The drag and drop process follows these steps internally:

  1. User drags a row from one view to another.
  2. Gtk calls drag_data_get on the source widget.
  3. The drag_data_get handler (written by you) puts the dragged data into a selection.
  4. Gtk calls drag_data_received on the destination widget.
  5. The drag_data_received handler (written by you) puts the dragged data from the selection into the destination and calls finish on the drag context.
  6. Gtk updates the view.
  7. User is happy.

As the developer all you need to do (not as simple as it sounds until you’ve done it once) is implement step 3, step 5, and connect the signals to the signal handlers in steps 3 and 5.

Nitty Gritty Details

There are so many nitty gritty details to programming in Gtk2 and Gtk2-perl, it drives me nuts!!  Here are some that I discovered while working on the code example below… but by no means is this a complete list.  Please leave comments with details you discovered that may be of use to me and others.

  • get_selected only works if GTK_SELECTION_MULTIPLE is not used.  See my ($model,$iter) = $tv_selection->get_selected; below.
  • enable_model_drag_dest can only be called once on a TreeView (if called multiple times, only the last call is used).  If you need multiple targets or actions you’ll have to list all the targets as arguments and combine all the actions into a Perl array reference, for example: [ 'move', 'link', 'copy' ]

Complete Code Example

The code highlighted in pastel red controls the drag and drop functionality.  All other code sets up the window, views, initialization code, etc..

#!/opt/local/bin/perl

use strict;

use Gtk2 '-init';

my $main = Gtk2::Window->new;

my $src_tree   = Gtk2::TreeView->new;
my $dest_tree  = Gtk2::TreeView->new;
my $src_model  = Gtk2::TreeStore->new("Glib::String");
my $dest_model = Gtk2::TreeStore->new("Glib::String");

# Setup trees
# src tree
$src_tree->set_model($src_model);
my $col = Gtk2::TreeViewColumn->new;
$src_tree->append_column($col);
my $renderer = Gtk2::CellRendererText->new;
$col->pack_start($renderer,1);
$col->add_attribute($renderer,"text",0);
# dest tree
$dest_tree->set_model($dest_model);
$col = Gtk2::TreeViewColumn->new;
$dest_tree->append_column($col);
$renderer = Gtk2::CellRendererText->new;
$col->pack_start($renderer,1);
$col->add_attribute($renderer,"text",0);
# end setup trees
# Drag and drop
$src_tree->enable_model_drag_source(
    'button1-mask',
    'move',
    [ 'text/plain', 'same-app', 1 ]
);
$dest_tree->enable_model_drag_dest(
    'move',
    [ 'text/plain', 'same-app', 1 ]
);
# signals for drag and drop:
$src_tree->signal_connect_after( drag_data_get => \&drag_data_get );
$dest_tree->signal_connect( drag_data_received => \&drag_data_received );
# end drag and drop
# setup ui
my $hbox = Gtk2::HBox->new;
my $vbox = Gtk2::VBox->new;

$hbox->pack_start($src_tree,1,1,5);
$hbox->pack_start($dest_tree,1,1,5);

$vbox->pack_start($hbox,1,1,5);
$main->add($vbox);

$main->set_size_request(640,480);
#end setup ui

# Fill models with test data
$src_model->set($src_model->append(undef),0,"One");
$src_model->set($src_model->append(undef),0,"Two");
$src_model->set($src_model->append(undef),0,"Three");
$dest_model->set($dest_model->append(undef),0,"Four");
$dest_model->set($dest_model->append(undef),0,"Five");
$dest_model->set($dest_model->append(undef),0,"Six");
# end fill models

$main->show_all;

Gtk2->main;
# Drag and drop signal handlers.

sub drag_data_get {
    my ($widget,$context,$selection,$info,$time,$data) = @_;
    my $tv_selection = $widget->get_selection;
    my ($model,$iter) = $tv_selection->get_selected;
    my $value = $model->get_value($iter,0);
    $selection->set($selection->target,8,$value);
    return 0;
}

sub drag_data_received {
    my ($widget,$context,$x,$y,$selection,$info,$time,$data) = @_;
    my $value = $selection->data;
    my $model = $widget->get_model;
    my ($path,$position) = $widget->get_dest_row_at_pos($x,$y);
    my $iter = undef;
    if ( $path ) {
        my $drop_iter = $model->get_iter($path);
        if (
            $position eq 'before' ||
            $position eq 'GTK_TREE_VIEW_DROP_BEFORE' ||
            $position eq 'into-or-before' ||
            $position eq 'GTK_TREE_VIEW_DROP_INTO_OR_BEFORE'
        ) {
            $iter = $model->insert_before(undef,$drop_iter);
        } else {
            $iter = $model->insert_after(undef,$drop_iter);
        }
    } else {
        $iter = $model->append(undef);
    }
    $model->set($iter,0,$value);
    $context->finish(1,1,$time);
    return 0;
}

CPU usage no more! indexing and mdworker, while compiling

When compiling programs on Mac OS X, mdworker indexes the compiled binaries… while the code is compiling.  Or horrors of horrors!  mdworker is the background process that gathers and indexes meta information (meta data) for Spotlight.  This is a great tool, but we don’t want or need it to index binaries as we are compiling a large project.

To disable indexing (and therefore disable mdworker, but not disable Spotlight) use the command:

sudo mdutil -i off -a

If you like your indexing and like to use Spotlight (like I do), be sure to turn things back on after you’re finished compiling with the command:

sudo mdutil -i on -a

But perhaps Apple has configured mdworker to only use idle CPU time, so that it doesn’t take valuable resources from the compiler.  This is a good question for a later post.  What’s your experience been like?