Pages

Saturday, July 11, 2009

Installing Erlybird Netbeans Plug-In

Nivir asked me how to install erlybird plug-in for Netbeans. So here's the guide.

  1. Have your favorite Netbeans distro installed.

  2. Download and unzip the erlybird distribution. This one, for instance.

  3. Select "Plug-ins" from the "Tools" menu of Netbeans, and select the "Downloaded" tab in the dialog that appeared. Then you can hit "Add Plugins..." and select all the files that were extracted from the erlybird distro.


  4. That's it just hit the "Install" button now on the bottom-left and the plug-in gets installed.

  5. After the plug-in gets installed, it will take quite some time for it to initialize. So be patient.


Nice features in Erlang: hot code swap

I'm still reading a book on Erlang, and just got a part of the 16th chapter over, so I though I'll put some of the nice features is the post now.

First, here's a simple example.




The executor just receives the code which should be executed and calls the handle/1 function in client.erl. So it does:

$> erl
> c(executor).
> c(client).
> client:start().
"Value = Hello"


So what I noticed here is that the compile-time dependency exists only for the client.erl while not for the executor.erl. Another nice point here, is that Mod:handle(Val) compiles without knowing actually what is Mod, i.e. erlang is quite a dynamic language?

Next, I took an example code from the book, and simplified it a bit, in order to get to the point.




Executor is now a process, which sends messages to itself, and also executing the code for a given module via handle/1. Here's the execution result:



What I would like to emphasize here, is:

  • The syntax for spawning a process: spawn(fun() -> some_function() end)

  • The syntax for sending the messages: Pid ! Message

  • The syntax for receiving the messages: receive Pattern1 -> Ex1; Pattern2 -> Ex2; ... end

  • Tail recursion: note that the executor's loop/2 function recursively calls itself at the end of the body. This kind of coding practice allows to write recursive code without consuming the stack, i.e. this call is transformed into a normal loop.

  • Modularity. In the example one can see, that executor.erl is responsible for the infrastructural functionality, e.g. process spawn, message sending, etc. On the other hand, client.erl doesn't know anything about the infrastructure but just implements the "business logic" in handle/2 function. This kind of code separation allows to proceed with hot code swap in the further example.





In the example code above, executor.erl is complemented with a new function swap_code/2, which is used to call the loop/2 function while providing it a new module for Mod. Also, loop/2 is complemented with a block that is actually making the switch to the new client module. The client1.erl module is just a copy of client.erl with just slight changes made to the response logic in handle/1.

Here's the execution result:



So that said, we started the executor module first, then executed the client.erl module's code and after that we switched the functionality to the brand new client1.erl ONLINE, whitout restarting the executor!

The book example used a dictionary to save the state of the application, but I removed it just to simplify the code and get to the point more easily.


The Morale

I'm astonished that erlang provides such functionality out-of-the-box: concise syntax, hot code swap, built-in process distribution, i.e. the executor process in the example could have been started on a separate node or even on the other host! This is a nice language and a great technology. Probably this explains why Scala (which is inspired from erlang) has the momentum now.

Monday, June 29, 2009

WTF: BigDecimal.stripTrailingZeros().toPlainString()

(new BigDecimal("1.00")).stripTrailingZeros().toPlainString(); // "1"
(new BigDecimal("0.00")).stripTrailingZeros().toPlainString(); // "0.00"

Thursday, May 14, 2009

ActiveMQ, Ruby, STOMP Transport

ActiveMQ is a popular Enterprise Messaging and Integration Patterns provider. ActiveMQ is designed to communicate over a number of protocols (such as Stomp), and it also supports plenty of cross language clients.

And so here we have a stomp client for ruby, which I found quite easy to use, however quite a few information is available for it.

What you do to use Apache ActiveMQ and the stomp client for ruby?

1, download and install Apache ActiveMQ.
2, download and install rubygems.
3, configure stomp transport in Apache ActiveMQ. In fact, in conf/activemq.xml of your ActiveMQ installation you can see that stomp transport is already configured:



4, writing a message sender using the stomp client for ruby.
#sender.rb
require 'rubygems'
require 'stomp'
client = Stomp::Client.open "stomp://localhost:61613"
client.send('/queue/myqueue',"Hello World!")
client.close

5, writing a message listener using the stomp client for ruby.
#listener.rb
require 'rubygems'
require 'stomp'
client = Stomp::Client.open "stomp://localhost:61613"
client.subscribe "/queue/myqueue" do |message|
puts "received: #{message.body} on #{message.headers['destination']}"
end
client.join
client.close

And so this is it! Just start the ActiveMQ message broker, start the listener (ruby listener.rb), start the sender (ruby sender.rb) and the "Hello World!" message will be transported via ActiveMQ's stomp transport.

Friday, May 8, 2009

Oracle 10g JDBC and Java 5

In addition to the previous bug with Oracle 10g JDBC driver v10.1.0.4 and Java 5, when instead of a very small number a very large number gets inserted, we encountered another bug, which was kind of the opposite. This time the exact version of Oracle driver is 10.1.0.5.

Here's a sample code:
BigDecimal decimal = new BigDecimal("1.03+7");
PreparedStatement stmt = .... 
stmt.setBigDecimal(decimal);
stmt.update("...some sql...");
After the transaction is commited, and we do a query for the result we get .... 10 (!)

inserted 1.03+7 but the result is 10


We cannot rewrite the application, as it already has a lot of Java 5 features in it. One option could be to use retrotranslator and run the application with Java 4. But the problem is that we already use some frameworks that make use of Java 5 API and we cannot overcome the problem just by translating the bytecodes to the older JVM.


Here's the solution I discovered:

When using the scientific notation for instantiating BigDecimal, calling a toString() method will be:

BigDecimal decimal = new BigDecimal("1.03+7");
System.out.println(decimal);
>> 1.03+7

But changing the scale will produce something different:
BigDecimal decimal = new BigDecimal("1.03+7");
System.out.println(decimal.setScale(2));
>> 10300000.00

That's it! You just can set scale for the BigDecimal that you are about to insert into Oracle database :) So far we're good with this solution, although it looks like a crap.

Saturday, May 2, 2009

Erlybird: the Erlang plug-in for Netbeans

Erlybird looks very fine while working in Netbeans. Unfortunately Erlide (the Erlang plug-in for eclipse) wasn't that smooth.



The strange thing about the plug-in settings is that it shows itself like if it would be a JRuby plug-in:



What could be done better, I think, is when I run a script, instead of the erl interpreter I could choose a function to be used to start the script. So it could look like any other program I'm running in IDE.

Friday, May 1, 2009

FIX is digging the ESB world

After Apache Synapse got its FIX integration and Apache Camel followed with the FIX endpoint, we can see now same integration done for Mule ESB. It is interesting, that all the products use QuickFIX/J to implement the integration.

What is it? Is it a hype for the ESB world, to include FIX - there must be a trigger for this kind of features. Do the OMS really demand FIX more and more?

Sunday, April 12, 2009

Sun Tech Days 2009 in St. Petersburg

So I visited Pt.Petersburg in order to take part in Sun Tech Days 2009. Unfortunately the quality of the sessions was twofold, some were missing a lot of technical details. But still it was a good opportunity to visit St. Petersburg - an astonishing city.



The first session I attended was OpenSSO, delivered by Sang Shin


OpenSSO looks very promising. A very modular design of the framework allows to use virtually any authentication scheme.

Next, I attended a session about DTrace, quite a powerful tool for monitoring your applications.




Also, JSR-290 looks very promising!

I also attended the superb talk by Yakov Sirotkin, about organizing the asynchronous jobs using the Oracle AQ.

Also, VirtualBox have been advertised quite heavily.

Wednesday, March 11, 2009

camel-quickfix update: Simple Acceptor Usage Scenario

The simplest usage scenario of a Quickfix/J Acceptor in Apache Camel would be as follows:

from("quickfix-server:server.cfg").
to("bean:someBean");


The bean will receive a class instance derived from quickfix.Message and the user can do what ever he wants with this model. However, very often you may want to map the FIX representation onto your own object model. For this purpose camel-bindy is a nice tool, where you can describe how the FIX string should be mapped onto the object model.

So now we have a bit improved version of the scenario:

DataFormat bindy = ...
from("quickfix-server:server.cfg")
.unmarshall(bindy)
.to("bean:someBean");


So, now the target bean will receive the message which is defined in your own object model.

This is the simplest scenario, but there's more to come.

Disqus for Code Impossible