An Amazon S3 Script, a Video and a question

Hi guys and gals! It’s been a long time since I last posted something. I’ll talk a bit about the reasons behind it in the end of this post, but first the main course: the S3 Script.

I had to write a script to fix some images I have in my small store (Lojinha), and the images are all uploaded to Amazon S3. So I figured I should do it in Scala, and also that I should create a video explaining how I did it. You can find the script in a gist here.

The video is here and, with it, I bring you a question. The video is in Portuguese – which obviously means that if you don’t speak this language, this is a problem. So the question: Are you interested in this content in English?

This is an important question, that goes back to my point in the first paragraph: the reason why I’m writing less here: I’m focusing a lot more on my Youtube channels (Dev, Games and Personal). Now, if you are a non-Portuguese speaker and if you tell me that you want to be able to understand the videos anyway, this might give me some energy to solve this problem.

The solution I have in mind is not creating whole new videos in English – that would be just too much work. But Youtube supports Closed Captions – which means I could just translate whatever I’m saying in Portuguese and add subs to the videos. Would that be compelling for you?

Thanks for sharing your opinions!


Posted in scala | Tagged , , , , , , , , | Leave a comment

Akka Essentials Book Review

Recently I had the opportunity to read the Akka Essentials book. This post is a brief review of this publication. Hope you enjoy.

The only good thing about this book is that it is short. Ok, that’s an exaggeration, but don’t expect too much from this book. To keep things in perspective, I read the kindle edition of this book this last month – so perhaps some of the problems mentioned below are specific to this version.

First thing I need to say about this book is that, a lot of times, it has at least awkward (to not say wrong) English sentences. It really could use some serious revision here. Some formatting review would also be good: in the “About the Reviewers” section there is even a whole duplicated paragraph.

The book covers lots of stuff about Akka – so it might be a good first glance at what this framework can do. Just don’t trust too much the code samples – more on that in a moment. Another interesting point, which might be good for some people, and bad for others, is that it presents examples both in Scala and Java.

In the sample code, lots of times, the author has slices of code that he uses to explain something. Than he goes on to add the entire code again, in a single unit. This is another point where I guess some people will like, and others won’t. I didn’t – it feels like wasting space that could be used to better explain some concepts.

Now to my pet peeve: a sample message, in an earlier chapter in the book. Try to guess what’s wrong in the code bellow (extracted from the book) before reading on:

import java.util.List;
public final class MapData {
  private final List<WordCount> dataList;
  public List<WordCount> getDataList() {
    return dataList;
  public MapData(List<WordCount> dataList) {
    this.dataList = dataList;

We have a class named MapData, whose objects are going to be used as messages to actors. It has a java.util.List attribute which is not guaranteed to be immutable, and no defensive copies are used. The problem with this is that such mutable messages can corrupt the actors, making them unreliable at all – this is even mentioned in the official Akka documentation, and is a serious error: – take a look at the “Messages and immutability” section.

In Scala, writing immutable messages is a lot easier, so at least there the author should have a correct message, right?  Unfortunately not. This is his Scala version for the message:

case class MapData(dataList: ArrayBuffer[WordCount])

Oh man… Even the Scala version of the message is wrong! ArrayBuffer is a mutable data structure. You really have to get out of your way to make this error. It is normal for people that are starting to use actors to write this kind of code, but I really expect more from a book trying to TEACH Akka. I wrote a bit about this subject here.

Another thing a bit strange or off-putting in the book is that sometimes it feels like the author is just throwing information at you, randomly. Also, a lot of times, the text says one thing about the code, and the code itself is a bit different from what is mentioned in the text – another clear lack of revision. In one example, when talking about routers for the first time, the author says we could pass messages in a round robin fashion using routers (which is correct). Then he goes on to show a sample code using a … BroadcastRouter! This kind of router has a completely different semantics.

All in all, since I love Akka, I would be happy to be able to recommend this book. The latter chapters even got better then the first ones. But I can’t. You are probably better off just reading the official documentation.

Posted in scala | Tagged , , , , , , , , , , , , , | Leave a comment

NetBeans and Scala Screencasts

Last week I started recording screencasts. It’s a new experience, and I’m enjoying it. For now, its only in portuguese, but if you are english speaking and would like to watch them, I’ll consider recording in english as well. Just let me know.

I have two screencasts recorded and published so far: one about JSF with Scala, and another one about using NetBeans for Scala development. If you understand portuguese, please take a look and let me know what you think!

The screencasts can be found in my Youtube channel here.

Posted in scala | Tagged , , , , | Leave a comment

Lojinha (small store) is in production!


In this (a bit old) post, I mentioned an application I’m developing, called Lojinha – which stands for “small store” in portuguese. This development has two goals: exercise and explore some technologies I’m studying and using, and to sell (for real) some old stuff I have at home that I don’t use or want anymore.

I completely forgot to mention it here (which is the reason for this post), but Lojinha is now in production… for about a couple of months. You can find it here. It’s really refreshing to have it out there. It is a very simple application, yet it took me a while to find the time to put into it.

If you are interested in anything I’m selling, just post a bid (a fair one please), or contact me directly – either way is fine.

If you don’t care about my stuff, but are curious about how I developed that, the source code is available in my github. The application uses Play Framework 2.0 and Akka 2.0 – nice stuff!

Like I mentioned in the original post, I wanted to deploy the application in some cloud provider – and the one I was more familiar with won: Amazon EC2. It is running on a micro instance, and so far it is really working well.

Any feedback is welcome, both about the code and about the store itself.

Posted in scala | Tagged , , , , , , , | 1 Comment

How to write a desktop / standalone application in Scala?

An student asked this question to me a while ago, and I figured it might be valid for other people starting with Scala as well. So I’ll try to give general directions in this post.

The idea is not to write a full tutorial; instead, I’ll show you the possible paths you have.

First, the basics. In Java, when we want to have a standalone application, we have an structure similar to the following:

public class Main {
  public static void main(String args[]) {
    System.out.println("Hello World!");

Since Scala also runs on top of the Java Virtual Machine, the structure will  be similar, with a few tweaks. The first one is related to the fact that Scala doesn’t have static fields, so we will have to use an object.

The second tweak is that we won’t even need the main method – we will use an utility class that have that method and it will call the code we pass to the object constructor. This is what the result looks like:

object Main extends App {
  println("Hello World!")

In practical terms, both versions will work the same way – but as always, the Scala version will be much simpler. We could also directly implement the main method in the Scala version, instead of extending App, but I’ll left that as an exercise to the reader.

The next question could be: how to write user interfaces with Scala?

The answer to this question is very direct as well: the same way we would write UIs in Java. For example, the following code uses Java Swing to create a simple window application:

import javax.swing._

object Main extends App {
  val frame = new JFrame("Hello World!")
  frame.setSize(300, 300)

The code above is a direct translation from what a Java version would be. It leaves a bit to be desired, so if you want more, you can take a look at Scala Swing – which is a set of wrappers on top of the Java Swing API, to make it easier to use.

Another option is to follow the new trend in Java land: Java FX. You can use Java FX directly from Scala, or use a set of wrappers called Scala FX – whatever suits you better.

I hope I made it clear how you could start coding a desktop and / or standalone application in Scala. If not, please let me know.

Posted in java, scala | Tagged , , , , , , , | Leave a comment

A little bit about Scala eXchange 2012

Last week I went to Scala eXchange 2012, in London. The conference was great, and I’ll have more to say about it later, perhaps through other means different than here. Not sure yet. I’ll also talk about the event in the next Scaladores Meeting.

The first thing I want to say is that I love London, and it was really great to go there again, after a few years. So thank you Skills Matter for being in London!!

Now to the meat of the event. Scala. Two days full of Scala, both ending in beer and pizza. Can’t ask for much more. I’ll quickly mention a few talks I liked, together with some pictures. First off, the event was in a really charming location, as you can see in the picture bellow:

the crypt - scala exchange

the crypt – scala exchange

The conference started with Martin Odersky himself. He talked a lot about the progress of scala in general. First, from the point of view of his course in Coursera. Then, he proceeded to talk about the new features coming on scala 2.10.

Martin Odersky

Martin Odersky

There were several other talks during the day. One of them was the Enrik’s talk about the Typesafe console architecture, which was quite nice, and Spiewak’s talk about compilers. This last one was just… amazing. If you are curious, you can watch the video of the talk here, its really worth your time. Now, a couple more pictures:

Enrik on the Typesafe Console

Enrik on the Typesafe Console

Spiewak on Compilers

Spiewak on Compilers

The first day ended with an Akka code kata. I spent some time there to see how people “from the source” are teaching Akka. Interesting to see I’m doing it myself on a similar way =)

The second day was interesting as well, full of small speeches. I liked them in general. One was really good: Roland Kuhn’s talk about Akka. He talked about what is coming on the next Akka release, and it is promising.

Roland on Akka

Roland on Akka

Another one was very promising as well: Jon Pretty, talking about This is an IO framework he is working on which sounds really interesting. He did lots of demos reading and writing data, always with simple and readable lines of code. I’m curious to see how far this project is going.

Jon on Rapture IO

Jon on Rapture IO

In summary, it was a long (and a bit expensive…) trip to get to Scala eXchange, but the whole experience was well worth the effort. I hope to return next year.

Posted in scala | Tagged , , , , , , , , | Leave a comment

What about the Scala community?

Here in Brazil, the scala community is still at its early stages. We are few, but we exist. And more people are always learning about the language, and some of them join the community. A friend and I even created a user group for in-person meetings, the Scaladores. We have almost a year now. And that is why I can say new people are getting to know Scala: we almost always have new faces in the meetings.

Now, how is the Scala community in your region? Europe in general, and in special London and Berlin seems quite nice in this regards. I’m always hearing about meetings and stuff in London, and will even be attending a conference there. Some places like the Bay Area in the USA also makes some noise. Where else?

I guess one of the things that shows how Scala is really growing, world wide, is the availability of courses and trainings, like the ones offered by Typesafe. And there are other companies doing this as well, like Dick Wall’s Escalate Software. So… now we have it in Brazil. In a partnership with Globalcode, I created a Scala course, to try to fill this gap.

Now the Scala ecosystem is more complete down here in São Paulo, Brazil. We have a user group, people using scala, new people learning (and loving) the language all the time, and now a training. What else is missing? What else do we need to help Scala grow even more in Brazil? Any comments, suggestions …. ?

Posted in scala | Tagged , , , , , , , | Leave a comment

Scala Exchange 2012

And so I decided. I will be attending Scala Exchange, at Skills Matter, in London, November 19th-20th.

I was looking for a good excuse to travel to London for a while, and this conference seems to be it. A good chance to meet nice people, and to visit a city I love.

Anyone out there is in for a beer or two?

Posted in scala | Tagged , , , , | 1 Comment

Defensive object copies in java and how scala avoids it

In one word: immutability. But read on for the whole history =)

In another, old post, I wrote about the usage a primitive long value instead of Date objects in attributes. This kind of thing is usually called defensive copy, i.e, we copy the value we received in a setter or constructor, instead of assigning it directly. We could have, also, copied the date as a new date object. This may sound simple, but wait until you have big objects, with complex hierarchies.

In theory, the clone method could help us solve this problem. I wrote about this method a long while ago: the bads of the java clone method, illustrating a bit how the java language sometimes makes us think the wrong way. The Effective Java book explain this problem also, so maybe you’ll want to check it out.

But that analysis on the clone method was by no means complete. More than what I mentioned previously, there is at least one more thing that is bad. Extracted from the javadoc:

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it.

It means that, whatever it does, the clone method doesn’t return a real copy of the object, but a shallow one instead. And it goes as far as saying that you are responsible to make the copy right, by modifying fields… now imagine doing that with an object that references a couple of others, which by turn references others and others… exponentially difficult to get it right.

class A(b: B) extends Cloneable {
  override def clone() = super.clone
  override def toString() = "[A: %s]".format(b)

class B(c: C) extends Cloneable {
  override def clone() = super.clone
  override def toString() = "[B: %s]".format(c)

class C(var x: Int) extends Cloneable {
  override def clone() = super.clone
  override def toString() = "[C: %d]".format(x)

val c = new C(10)
val b = new B(c)
val a = new A(b)

val a2 = a.clone
c.x = -99

The sample code above is in Scala, but Java code would be very similar. So, is the code correct? When I do c.x = -99, should it affect only a, or both a and a2? Currently it does the later:

scala&gt; println(a)
[A: [B: [C: -99]]]

scala&gt; println(a2)
[A: [B: [C: -99]]]

So here is where the recursive cloning would take place. I would have to make the clone method inside A call the clone method from B, and so on. Not fun. The java language is not nice here, but perhaps the problem is deeper. Lets try to change our way of thinking.

After a few years in Scala land, I find there is something more to add. One thing that Scala tries to convince you is that you should have immutable objects as much as possible. That means that your objects never changes – you create new ones if need be. That also means that they can be shared easy and safely – thus clone is not necessary anymore.

Back to the sample code above, c.x = -99 would never be allowed – so it isn’t a problem at all. To make it happen, our new C class would look like this:

class C(x: Int)

Removing the var keyword effectively makes x immutable. What if you really want a new x? You have to create a whole new A. If this class had more fields, it could be boring, so scala helps us here. Lets change our classes to be immutable, forget about cloning stuff, and to be case classes. Lets also add another attribute to A.

case class A(b: B, name: String)
case class B(c: C)
case class C(x: Int)

val c = C(10)
val b = B(c)
val a = A(b, "jcranky")

First, being case classes, we don’t need to bother with the toString overrides anymore. Nor with the new operator. And we also got a prize: the copy function. With this, we can copy an object and change only what we want. This is a sample execution of out new classes:

scala&gt; println(a)

val a2 = a.copy(b = B(C(-99))

scala&gt; println(a)

scala&gt; println(a2)

The only drawback is that you have to design your application with this kind of structure in mind. From that on, only bonuses. You don’t have too much to worry with locking for example, since there is no mutable stable to watch over.

Posted in java, scala | Tagged , , , , , , , , , | 1 Comment

akka microkernel with maven

And then I have to use the Akka Microkernel.

Not that this is bad news. The bad news is that I have to do it with maven. The project I’m doing this for is already using maven, and has a few modules – which makes migrating to sbt out of question. Akka has a nice sbt plugin that generates the package for a microkernel distribution of your akka project – but there is no such thing for maven.

How to solve this problem with maven? Fortunately, it isn’t that hard. There are probably lots of other ways to do it, but my solution will use the maven-assembly-plugin. The boring part, as always with maven, is to write the proper xml. I’ve created a new assembly descriptor, called akka.xml, that goes as follows:

<assembly xmlns=""

Two points to notice: the xml assumes an akka start script at src/main/resources/akka/start and a configuration file at src/main/resources/akka/application.conf, so you will have to create those. For me, I just copied the default start generated by the akka sbt plugin and created an empty application.conf, and will tweak them later.

Now, to enable the plugin add the following to the plugins section of your pom.xml:


This one also assumes something: the presence of the previous xml code, in the folder src/main/assembly/akka.xml.

That should do it! Now just unpack the zip file in your server and run your Bootable akka code =]



Since the xml mentioned above went through small tweaks, I added them in this GIST. So if you want to see the most recent version, go there 😉

Posted in scala | Tagged , , , , , , , , , , | 13 Comments