How I spent my Christmas holidays

gravity02

The Christmas tradition here at Codevomit is to spend some hours in front of our computers and churn out code as soon as it comes to our minds. After all, that is what codevomit is all about.

The goal of the past year holidays was to gain some basic knowledge of Scalable Vector Graphics and, possibly, of Git. I see SVG employed almost everywhere today and I am always impressed by the nice visual results that you can get. I wondered what I would have been able to do with it. In order to achieve such an ambitious result, I choose an example that I first implemented in Visual Basic when I was about seventeen and since then it always kept going around my mind. This is the outcome I am sooo proud to present to you. It took about 8 – 9 hours to get the basic engine to work, then another countless hours of relentless play to fix some major issues and have a lot fun. The source code is here and it’s still under development since I am currently working on a 3D version (and it works surprisingly well!).

The application is a 2D discrete simulation of celestial bodies interacting with each other according to Newton’s Law of Universal Gravitation. Technically, it is simply a web page with a couple of javascript files or, as they like to call it nowadays, an HTML5 + Javascript application. No server side work is done except for the good old Apache serving the page.

Instructions

First, you have to select the kind of celestial body that you want to add to the simulation. In order to do that, click on one of the “mass” buttons. Then, click on the canvas and, while you are still holding the left mouse button, drag. You will see that a red line follows your mouse movement: that line is the initial speed vector that your body will have. The longer it is, the faster your object will go.

speed-vector

At this point, the objects are still immobile. The simulation can be started, stopped, reset, etc…, with the buttons at the bottom of the canvas. You can also enable traces with the corresponding button and get some nice draws like the one I pasted at the beginning of the post.

When you get a nice configuration, you can save it with the “SAVE” button. The state of the simulation is serialized to text (XHTML + JSON) and written in the textbox, from which you can copy it. You can restore it at any time: just paste the text back to the textbox and click the “RESTORE” button.

Advertisements

Setting up a restlet with Camel in JBoss Fuse

If you have already successfully gone through the steps of the previous tutorial, this is going to be quite fast and straightforward. All you need to know is that in Camel you can set up a restlet to listen to a specific URL/port with the “restlet” prefix.

Let’s assume that we want to listen for REST request and turn the body of the request into a message to be queued on one of our (already existent) ActiveMQ. As in the previous example, we can accomplish the task without even write a line of Java code.

We can add the following elements to our blueprint.xml file:

    <route>
        <from uri="restlet:http://localhost:8282/demo-endpoint?restletMethod=POST"></from>
        <camel:setBody>
            <simple>test=true</simple>
        </camel:setBody>
        <log message="The request body is: ${body}"></log>
        <to uri="activemq:sink-queue" pattern="InOnly"></to>
        <to uri="mock:result"></to>
    </route>

Here I’m specifying the restletMethod parameter, which tells Camel to only respond to GET HTTP request (i.e. any other HTTP verb will result in the request to be discarded). The setBody element is used for making thigs simpler: it sets the body of the exchange to the child value, as you might expect. Now we can “clean-install” the project as usual and switch to the Karaf console. Update the package with a osgi:update command (here, 251 is the ID of my bundle, you will probably have a different one. You can find out what ID your bundle has by executing a “list” command):

JBossFuse:karaf@root> osgi:update 251

After this, execute “list” and take a look at the output. In correspondence with the updated bundle you should see something like this:

[ 251] [Active     ] [GracePeriod ] [       ] [   60] A Camel Blueprint Route (1.0.0)

This is because the bundle has a missing dependency: the camel-restlet Feature. The bundle has been successfully installed, but it cannot be started until the missing dependency is satisfied. The missing feature is not installed by default in Fuse. Luckily, you can easily get it by typing the following command:

JBossFuse:karaf@root> features:install camel-restlet

To learn more about Features, refer to the official documentation.

Restart your bundle. The restlet is now ready. You can try and call it with whatever method you prefer. For simplicity, i present you the command used to perform a test with curl:

curl --data "test=true" http://localhost:8282/demo-endpoint