Groovy – first time in Tiobe TOP 20

Idea about writing something about Groovy programming language came to my mind when I looked at Tiobe Programming Community Index for October 2013.

Here is the index:



Firstly, I would like to write a few words summarizing how the index is produced.
Tiobe takes into consideration programming languages fulfilling the following two criteria:

  • language should have an own wikipedia page clearly saying that it concerns a programming language
  • the programming language must be Turing complete

The rating itself is calculated by counting hits of the most popular search engines containing for example Google, Youtube, Baidu, Wikipedia, Amazon, Blogger, etc.

As you can see Groovy is at 18th position. It entered the TOP 20 for the first time. Moreover, if you look at its position on October 2012, the change is really impressing. Let’s hope that Groovy will hold its upward trend for the next few months.

More posts concerning Groovy are on their way…

Spring Integration

I have been really impressed by the Spring Integration project recently so I decided to write a few words about it.

Just to make a quick introduction:

Spring Integration can be described as an extension of the Spring programming model which supports Enterprise Integration Patterns.

It enables lightweight messaging within Spring-based applications and supports integration with external systems via declarative adapters. Those adapters provide a higher level of abstraction over Spring’s support for remoting, messaging, and scheduling.

In my opinion the biggest advantage of the project is fact that it provides simple model to build enterprise applications while maintaining separation of concerns at the same time. It results in testable, maintainable and easy to change code. Code in services is responsible only for business logic, while executing services’ methods, synchronous/asynchronous communication, etc. is described in configuration and handled by the framework.

Another aspect of the project I really like is fact that it introduces messaging model into your application. I mean introducing messaging into communication between modules/layers or even particular services in the application. Many people pigeonholes messaging as way to integrate with external systems. I agree that it perfectly suits into such scenarios but why not use it to integrate modules and services which belongs to the same application? Spring Integration provides us with clear and simple tooling to support such architecture.

Let’s move to an example.

Consider simple case where we have a queue of credit card transactions which are to be processed. Transaction under 10000$ are processed by one service while transactions over 10000$ are processed by another one.

What we need is Transaction class:

public class Transaction {
  private BigDecimal amount;

  public Transaction(BigDecimal amount){
    this.amount = amount;
  public BigDecimal getAmount(){
    return amount;

Transactions under 10000$ processing service:

public class TransactionProcessingService {
  private static Logger logger = Logger.getLogger(TransactionProcessingService.class);
  public void process(Transaction transaction){"process in TransactionProcessingService; amount=" + transaction.getAmount());

Transactions over 10000$ processing service:

public class AntiMoneyLoundryService {
    private static Logger logger = Logger.getLogger(AntiMoneyLoundryService.class);
    public void process(Transaction transaction) {"process in AntiMoneyLoundryService; amount=" + transaction.getAmount());

Spring configuration:

<beans:beans xmlns=""
    <channel id="transactions" />
    <router input-channel="transactions"
            expression="payload.amount le 10000 ? 'normalProcessing' : 'antiMoneyLoundryProcessing'" />
    <service-activator input-channel="normalProcessing" ref="transactionProcessingService" />
    <service-activator input-channel="antiMoneyLoundryProcessing" ref="antiMoneyLoundryService" />
    <beans:bean id="transactionProcessingService"
                class="sample.TransactionProcessingService" />
    <beans:bean id="antiMoneyLoundryService"
                class="sample.AntiMoneyLoundryService" />

And finally code which sends two messages with two different transactions to the input channel:

ApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/creditCardTransactions.xml", TransactionProcessingDemo.class);

MessageChannel transactionsInputChanngel = context.getBean("transactions", MessageChannel.class);

Message<Transaction> normalTransaction = MessageBuilder.withPayload(new Transaction(new BigDecimal(9999))).build();

Message<Transaction> bigTransaction = MessageBuilder.withPayload(new Transaction(new BigDecimal(90000))).build();



Here is the log produced:

[TransactionProcessingService] process in TransactionProcessingService; amount=9999
[AntiMoneyLoundryService] process in AntiMoneyLoundryService; amount=90000

As we can notice, messages were properly forwarded by the router element to the appropriate services.

Java code should be self explanatory. Have a closer look at Spring configuration file.
In line 8 input channel is defined with id transactions. It defines a point to point message channel to which messages can be send and from which they can be fetched.

Line 9 introduces  router element. A Router determines the next channel a message should be sent based on the incoming message. Code in expression attribute is a SpEL expression to be evaluated at runtime. In the example presented the expression evaluates to a channel name. The other way to map the result to the channel name is using a mapping sub element.
The condition based on which channel is chosen can be also implemented in POJO.

Lines 11-12 contain service-activator definitions. A Service Activator is a component that invokes a service based on an incoming message and sends an outbound message based on the value returned by the service invocation.

Lines 13-16 contain simple beans definitions.

What do we get as a result of such design?

There is no coupling between TransactionProcessingService and AntiMoneyLoundryService.
Choosing the right service is fully handled by Spring Integration. The choose is based on condition placed in the configuration, not in the code itself.
These are the advantages of Spring Integration which we can see based on that simple example.

What is more, Spring Integration introduces and in some way enforces us to design our domain in an event-drive manner. If properly designed it perfectly mirrors the real world. We all live in event-driven environment. Each day we receive so many messages: mails, phone calls, text messages, RSS feeds, just to name a few. Software we produce, the business domain is like a slice of the real world. To increase success of the project the domain we model should reflect the real world the most accurately. Spring Integration is nearly a perfect tool to model that event-driven nature of any business we create software for.

Personally, I truly recommend deeper dive into that part of the Spring ecosystem. I am almost sure that no one will be disappointed.

Smart Pointers (C++)

Smart pointers are very useful feature of modern C++ programming model.

They are defined in the std namespace in the <memory> header file.

Let’s see the raw and smart pointer in action:

void rawPointer() {
    sql::Driver * driver = sql::mysql::get_driver_instance();
    sql::Connection* connection = driver->connect(host, user, pass);
//use connection

    delete driver;
    delete connection;
void smartPointer() {
    std::shared_ptr<sql::Driver> driver(sql::mysql::get_driver_instance());
    std::shared_ptr< sql::Connection > con(driver->connect(host, user, pass));
//use connection
//shared pointers are deleted when all pointer owners have gone out of their scope or given up ownership

As we can see there is a clear advantage of using smart pointers instead of the raw ones.

The standard library provide three smart pointer types which should be replacement for raw pointers as long as new code is developed:

  • unique_ptr
    • allows one and only one owner of the underlying pointer
    • deleted when the owner goes out of scope it is defined in
    • cannot be shared
    • cannot be copied
    • small and efficient
  • shared_ptr
    • underlying pointer can be shared between multiple owners
    • not deleted till all owners have gone out of scope or given up ownership
    • can be shared
    • the size is equal to two pointers: one for the raw pointer, one for the block containing the reference count
  • weak_ptr
    • use in conjunction with shared_ptr
    • provides access to a raw pointer which is owned by one or more shared pointer instances but does not take part in reference counting
    • deleted when the only reference to the object is weak_ptr reference

I firstly realized the difference between unique_ptr and shared_ptr when tried to create a unique_ptr and share it among two different objects:

std::shared_ptr<sql::Driver> driver(sql::mysql::get_driver_instance()); 
std::unique_ptr< sql::Connection > con(driver->connect(host, user, pass)); 
std::unique_ptr< sql::Statement > stmt(con->createStatement()); 
stmt->execute("USE " + db); 

Obviously, it cannot be done and the error is raised.

When unique_ptr is replaced by shared_ptr everything works all right. Connection object can  be shared between two DAO objects and we are assured that the pointer is deleted when it is not needed by any of the owners:

std::shared_ptr<sql::Driver> driver(sql::mysql::get_driver_instance()); 
std::shared_ptr< sql::Connection > con(driver->connect(host, user, pass)); 
std::shared_ptr< sql::Statement > stmt(con->createStatement()); 
stmt->execute("USE " + db); 

Visual C++ DataGridView – data binding

DataGridView is a control introduced in .NET Framework version 2.0. displaying data in customizable grid.  The control can be used in any application developed on .NET Framework using any language (C++, C#, VB) based on CLR (Common Language Runtime).
The control can be customized through manipulating of DataGridView class properties. Some of them are: DefaultCellStyle, CellBorderStyle, GridColor, just to name a few.
That is the short introduction do DataGridView Control. However, what I would like to concentrate in this post is specifying a data source for the control. There are several ways introduced in MSDN documentation:

DataGridView::DataSource Property

What was needed to implement in an application was using as a datasource some collection of simple POCO’s (Plain Old C/C++ Object).

So going this way, a POCO is needed:

ref class StudentDataGridItem
    Int32^ _id;
    String^ _name1;
    String^ _name2;
    String^ _surname;  //leave more properties in sake of readibility
    property int^ id     {       Int32 ^ get() {
        return _id;
    }       void set ( Int32^ value) {
        _id = value;
    }     }      property String^ name1     {       String^ get() {
        return _name1;
    }       void set ( String^ value) {
        _name1 = value;
    }     }      property String^ name2     {       String^ get() {
        return _name2;
    }       void set ( String^ value) {
        _name2 = value;
    }     }      property String^ surname     {       String^ get() {
        return _surname;
    }       void set ( String^ value) {
        _surname = value;
    }     }

The collection of objects of the above class is returned from a simple DAO:

ArrayList^ StudentsDAO::getStudents()
 Statement* stmt = conn->createStatement();
 ResultSet *res;
 ArrayList^ students = gcnew ArrayList();
  res = stmt -> executeQuery ("SELECT u.user_id as id, surname, name_1 as name1, name_2 as name2 FROM user u, student_extra s where u.user_id = s.user_id AND deleted = 0"); 
  while (res -> next()) {
   StudentDataGridItem^ student = gcnew StudentDataGridItem();
   student->id = res->getInt("id");
   student->surname= StringUtils::stdStringToSystemString(res->getString("surname"));
   student->name1 = StringUtils::stdStringToSystemString(res->getString("name1"));
   student->name2 = StringUtils::stdStringToSystemString(res->getString("name2"));

 }catch (SQLException &e) {
  //exception dealing logic
 return students; 

and bound to DataGridViewControl:

this->studentsDataGridView->DataSource = (System::Object ^) studentsDAO.getStudents();

What is assumpted is that control has columns specified with DataPropertyName equal to the property from POCO from which value goes to the particular column.

As a result data grid with proper columns is displayed, hovewer it contains no data. DAO returns data, it is bound to grid’s data source but nothing is displayed. What is more, it works perfectly in C# and probably in VB (not tested by myself) but not in C++.

To make it work, Bindable (documentation: MSDN BindableAttribute Class) attribute is needed to be specified on each and every property which is bound to the data grid column. The proper POCO should look like this:

 ref class StudentDataGridItem
 Int32^ _id;
 String^ _name1;
 String^ _name2;
 String^ _surname;
 //leave more properties in sake of readibility

    property int^ id
      Int32 ^   get() { return _id; }
      void set ( Int32^ value) { _id = value; }
    property String^ name1
      String^   get() { return _name1; }
      void set ( String^ value) { _name = value; }

    property String^ namee2
      String^   get() { return _name2; }
      void set ( String^ value) { _name2 = value; }

    property String^ surname
      String^   get() { return _surname; }
      void set ( String^ value) { _surname = value; }

Now, everything works properly and data grid displays data from the bound collection.

Scala function error “Type mismatch” in Intellij Idea vs Scala Eclipse

Using Scala we can define a function in a form when name and parameters are specified followed by the definition of the function body.

For example:

def abs(x: Double) = if (x < 0) -x else x
As you can see, curly braces are not mandatory when all those elements are put in one line.
When you put that code in Scala worksheet both in Intellij Idea and Scala Eclipse (Scala Eclipse IDE) everything is all right. Code compiles and runs.

The previous example was very simple. However, functions are more complex in real life so there is no way to stick them in one line. It is not a problem at all, you define multiline functions simply by embracing the function body in curly braces:

def listLength(list: List[_]): Int = {
  if (list == Nil) 0 else 1 + listLength(list.tail)

Code of course compiles and runs both in Intellij Idea and Scala Eclipse.

What happens if you omit the braces?

def listLength(list: List[_]): Int = if (list == Nil) 0 else 1 + listLength(list.tail)
That is the point where I spot difference in behavior between those two IDE.
While Scala Eclipse runs that code without any error, Intellij Idea gets stuck with the following error message:
error: type mismatch;
   found   : Unit
   required: Int
             if (list == Nil) 0
I am sure that that error message does not tell much to Scala beginners about the source of the problem.
Let’s investigate the error message more deeply.

Firstly, find out what type Unit is.
Type Unit is similar to what is known in Java as void. In Java void means that function does not return anything. However, every method in Scala returns a value. To deal with the situation when Scala function does not have anything to return, type Unit was introduced. Eventually Unit is defined on the bytecode level as void, but in Scala source level it is still a type.Come back to the error message. It says that expected returned type is Int but the function returns Unit in fact. What probably happened is situation, when only line:

if (list == Nil) 0

is interpreted, Scala adds the else statement in that form:

else ()

where the mentioned rule of returning Unit when there is nothing to return, is applied.
So the rewritten if finally looks as follows:

if (list == Nil) 0 else ()

If part returns Int, while else part returns Unit. More general type is taken and the whole function tries to return Unit while expecting Int from function definition.

I met that problem when trying to run in Intellij Idea the code example presented in Scala Eclipse. Multiline function was coded without curly braces and only Intellij Idea thrown the error presented.

Greg Young in Lublin

The big event for Lublin’s IT community is approaching. Greg Young has already come to the town. He is giving a talk regarding CQRS and Event Sourcing on Monday 22.03.
Besides his vast technical knowledge, he is a brilliant speaker who can really infect people around with his passion.
I am sure that there is no need to introduce that guy to anyone but maybe it would be worth mentioning some web resources regarding his activities:

Greg Young blog 

His stuff on InfoQ:

     CQRS document by Greg Young

     DDDD by Greg Young

    ActiveMQ-CPP bug

    I was faced with strange bug in ActiveMQ-CPP library recently.
    I just wanted to make sure that message broker (ActiveMQ) redelivers not acknowledged messages to the consumer applying some delay. For example 10 seconds.
    Let’s say that consumer processes message and some error occurs like there is no connectivity to the external system where processed message is stored. I do not want the message to be lost but rather returned to the broker and redelivered after some time.
    Quick look at the API and documentation and it is clear that such behavior can be implemented using RedeliveryPolicy.
    There are some properties which can be used to adjust the policy:

    Option Name Default Description
    cms.RedeliveryPolicy.backOffMultiplier 5.0 Used to extend the redelivery window when the _useExponentialBackOff option is enabled.
    cms.RedeliveryPolicy.collisionAvoidancePercent 15 Percentage of randomness to add to redelivery backoff to keep multiple consumers in a rollback from redelivering at the same time.
    cms.RedeliveryPolicy.initialRedeliveryDelay 1000 Amount of time to wait before starting redeliveries, in milliseconds.
    cms.RedeliveryPolicy.maximumRedeliveries 6 Maximum number of time to attempt a redelivery before giving up and NAck’ing the message.
    cms.RedeliveryPolicy.useCollisionAvoidance false Should the Random avoidance percentage be added into the redelivery delay.
    cms.RedeliveryPolicy.useExponentialBackOff false Should redeliveries be slowed on each cycle.

    Unfortunately, even if I tried almost all possible combination I could not forced the broker to redeliver messages after specified redelivery delay. Messages came back to the consumer immediately. It caused really high processor utilization and, what is more, log files were growing very quickly.

    As last resort I dived into library source code and that is what I found there:

    if( internal->redeliveryDelay > 0 && !this->internal->unconsumedMessages->isClosed() ) {
        // TODO - Can't do this until we can control object lifetime.
        // Start up the delivery again a little later.
        // this->internal->scheduler->executeAfterDelay(
        // new StartConsumerTask(this), internal->redeliveryDelay);
    } else {

    That must be a joke… API gives you a way to set redelivery delay, documentation says it can be done… But that is what you find in the source code.
    Just to be proper I use 3.4.5 version of the library and the bug is fixed in 3.5.0 version.