Tuesday, May 26, 2009

What is a Middleware? A Platform, A Framework or What?

Some time ago I wrote a post about the difference between "platform" and "framework" and recently a reader, Hemanth Gowda, posted a comment to that text, asking how the concept of "middleware" is positioned between those definitions.

In my opinion, the concept of middleware is all about service providing. While a platform allows a software to run and a framework is more focused on software design, a middleware will provide services for your application. These services will allow your application to support many concurrent users, connect to several data sources, interoperate with services provided by other middleware instances, expand the platform to scale the application, support several frameworks to give flexibility for developers and many other relevant features.

The best example of middleware in the Java world is an application server, such as Glassfish, JBoss and Websphere. They provide all features mentioned above and much more. If you develop an application to run in any of these application servers it will a) support multiple users by default; b) be able to connect to several database from different vendors (Oracle, MySQL, MSSQL); c) access web services (stack or rest); d) send emails; e) provide fameworks for logging, data access, user interface rendering; and f) manage multiple instances of the application server to optimize the use of available hardware, among other advantages. The figure below shows the graphical schema of an application server.



There are other kinds of middleware and I would like to emphasize a) message queue systems (MQS) to mainly interchange asynchronous messages between applications; b) enterprise service bus (ESB) to integrate different kind of services provided by several systems; and c) transaction systems to guarantee the consistency of the data involved in the same business operation, such as database management systems.

Comparing middleware with framework and platform, I would say that a middleware system contributes to reduce the complexity of your application, which is one of the main characteristics of a framework, but it is not actually part of your application as a framework is. A middleware will provide many services for your application, but everything (middleware + application) will run on the available platform, which could be a virtual machine, or an operating system or a mobile device, etc.

I appreciate your questions and I love to answer them here! So, do not hesitate to post your comments as Hemanth did. Thanks for the opportunity to discuss the topic.

Sunday, May 24, 2009

Table Generation Strategy Revised

The JEE platform (EJB 3.0) offers, since its previous version (EJB 2.1), a feature to generate database tables during the application deployment. This feature is currently part of the Java Persistence API (JPA), which can be used even outside the JEE environment, like in desktop applications. My case is exactly a desktop application where I'm using an embedded database to simplify as much as possible the application installation and configuration.

When I started working with this embedded database I have configured my persistence.xml file to "create" the database tables during the deployment time, based on annotations made in the entity classes. This option generates a DDL (Data Definition Language) file that is processed by the JPA framework to generate the tables. Unfortunately, if you change your entity classes, this strategy won't update the existing tables, creating inconsistencies with the data model. Unfortunately, the application will crash. There are other two options: a) "delete and create", which will drop the entire database structure to be created again; and b) "do nothing" and let the developer find a way to manage the database structure. "delete and create" keeps the consistency between entity classes and data model but it loses all the data persisted so far. It might be useful for tests, but the developer should remember to change this option when delivering a new version to end-users, which is the addition of one more point of failure. "do nothing" implies in more work for developers, who will take the responsibility to manage the data structure. Based on the two shameful possibilities ("create" and "delete and create"), "do nothing" seems to be the best and unique option available for serious projects.

Today I'm using the option "do nothing", keeping in mind my constant concern for simplifying the life of end users when installing and using the application. So, every time the application is started I check if the database exists. If not, I execute a ddl script bundled inside the main jar file. I'm using the following code:

public static void main(String[] args) {
{...}
boolean applicationPropertiesLoaded = false;
int attempts = 0;
do {
try {
attempts++;
loadApplicationProperties();
applicationPropertiesLoaded = true;
}
catch(Exception e) {
logger.warning("Application properties"+
"not found. Trying to create the database");
createDatabase();
if(attempts == 2) {
logger.severe(
"Problem to load application properties.");
}
}
} while(!applicationPropertiesLoaded ||
(attempts < 2));
{...}
}

The code above tries to access the database, executing a basic operation like loading the application properties. If the database doesn't exist an exception is thrown and the respective catch block is executed. Inside the catch block the method "createDatabase()" is invoked to create the database. This process is performed at least 2 times, since the application properties should be loaded anyway. See the "createDatabase()" method below:

private static void createDatabase() {
logger.info("Creating the database"+
" for the first time");
List sqlCommands =
loadSQLCommands(Main.class
.getResourceAsStream(
"/META-INF/create-database.sql"));
EntityManagerFactory emf =
Persistence.createEntityManagerFactory(
AbstractEntity.ENTITY_MANAGER);
EntityManager em = emf.createEntityManager();
EntityTransaction et = null;
String sqlCommand = null;
try {
et = em.getTransaction();
et.begin();
for(int i = 0;i < sqlCommands.size();i++) {
sqlCommand = sqlCommands.get(i);
em.createNativeQuery(sqlCommand).executeUpdate();
logger.info("database update: " + sqlCommand);
}
initializeDatabase(em);
}
catch(Exception ex) {
logger.log(Level.WARNING,
"Problem to execute the sql command: "+
sqlCommand, ex);
et.rollback();
}
finally {
et.commit();
em.close();
}
}

The code above accesses the file "create-database.sql" embedded inside the application jar, extracts a list of sql commands, executes one by one, and initializes the database, inserting some initial default data. I list below the method "loadSQLCommands()" that opens the embedded file and reads its content:

public static List loadSQLCommands(
InputStream stream) {
List sqlCommands = new ArrayList();
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));
StringBuffer sb = new StringBuffer();
String line = null;
while((line = reader.readLine()) != null) {
sb.append(line.trim());
}
StringTokenizer st =
new StringTokenizer(sb.toString(), ";");
while(st.hasMoreTokens()) {
sqlCommands.add(st.nextToken());
}
return sqlCommands;
} catch (MalformedURLException ex) {
logger.log(Level.SEVERE,
"Malformed URI of the file.", ex);
} catch (FileNotFoundException fnfe) {
logger.log(Level.WARNING,
"Database script not found.", fnfe);
} catch (IOException ioe) {
logger.log(Level.WARNING,
"Problem to read the script.", ioe);
}
return null;
}

Finally, the method 'initializeDatabase()", invoked at the end of the method "createDatabase()", is listed below:

public static void initializeDatabase(
EntityManager em) {
logger.info("Initializing the database"+
"for the first time.");
List sqlCommands = loadSQLCommands(
Main.class.getResourceAsStream(
"/META-INF/initialize-database.sql"));
for(String sqlCommand:sqlCommands) {
em.createNativeQuery(sqlCommand).executeUpdate();
logger.info("data update: " + sqlCommand);
}
}

With all this code above we could solve only the database creation process. There are still more code to update the database when needed. Because this post became so big, I'm going to describe the update process in a future post in this blog. Maybe, I missed some point here because there are so many details. So, if you had problems to implement it, please leave your comments below and I will try to complement the content to fulfill your needs. Keep following me!

Saturday, May 23, 2009

Two Sentences about Advices

This week two sentences about "advices" called my attention:
"People who ask our advice almost never take it. Yet we should never refuse to give it, upon request, for it often helps us to see our own way more clearly." - Brendan Francis
"The true secret of giving advice is, after you have honestly given it, to be perfectly indifferent whether it is taken or not, and never persist in trying to set people right." - Hannah Whitall Smith

My understanding is that we have to be good people to give good advices and do not expect that people will follow them. It sounds like the principle of forgiveness and generosity, which we can give and do not expect anything in return.



To conclude, a sentence extracted from the book of Randy Pausch, The Last Lecture:
"Almost every day I mention my dad. I do this because when you try to teach something new, most people tend not to pay attention. But if you refer to the experience of someone else, everything seems less arrogant and more acceptable." - Randy Pausch

Wednesday, May 20, 2009

James Gosling Will Open Jazoon '09

I won't be able to attend Jazoon'09 this time. Instead, I'm going to present a research paper in a conference held in Saint Petersburg, Russia. You know, I'm a researcher at this moment and this kind of conference has more priority for me. I've attended Jazoon'07 and Jazoon'08 and I do recommend Jazoon'09 for you. In 2008 I even gave a talk there, representing my Java User Group, which was indeed a great moment for me.



Zurich is great, the weather is perfect and people are nice. You're gonna have fun there, for sure. I feel bad for not going this year because I miss Christian Frey, Jazoon's General Manager, who is a great person. He is that kind of person who is really committed to make everything perfect for you even with all difficulties. I also miss my dear friend Felipe Gaúcho, who is a great Java professional working in Zurich, Switzerland. You will find him at Jazoon for sure, drinking a lot of beers. Btw, the beer is for free there ;).

This year James Gosling, the creator of the Java language, will be the opening-keynote speaker of the conference. I've met James in the last edition of JavaPolis in 2007 (Now it is Devoxx). It's really nice to shake his hand, say hello, listen to his voice and take some pictures. Don't miss the opportunity to meet the guy who created the language that gave you a great job!

Thursday, May 14, 2009

Why not an Organizational Twitter?

In the organization that I'm working on nowadays people who are allocated in national and international projects should fulfill a long form in the first week of every month, describing their working activities and allocation in the previous month. To be honest, I've never seen something as unnecessary as that. They won't have a precise idea about what people were doing with this. Of course, it is not going to be reliable because we don't remember exactly what we did a month ago, how long it took and they don't have a way to validate it. So, we try hard but even for us the information is not that precise. Probably, they use it to calculate salaries or it is an imposition of project funding institutions or something else. Whatever they do with this data, it cannot be used to generate analytical data, since the source is not reliable.

Thinking about this problem I realized that there is a simple and efficient idea out there to solve this organizational issue: Twitter. The new sensation of the internet has proved that people really like to keep other people updated about their personal life. The service increased 1382% year-over-year in February and it keeps on growing every day. It is a kind of microblog that accepts only 140 characters per post, which is good because people don't actually waste time to keep it updated.



What could lead to some waste of time is to follow the updates of other people in the organization. But it is not so impacting due to the size of posts. In most cases, people don't use all 140 characters and their updates are also an important factor of motivation to attract participation. Using a Twitter approach, the report of activities could migrate from a mandatory to a voluntary initiative, supporting the need of the organization for daily reports and increasing the reliability of the information to further analytical exploration.

Tuesday, May 12, 2009

Creativity Blockers

Did you already have the experience of going to a meeting with many ideas in mind, willing to share everything and someone there was so rude with you or just ignored you that you decided to close your mind, shut up and do only what was initially planned to do? Yeah! You have faced a creativity blocker. This kind of person is used to be cold, unsociable and too realistic. You can find them everywhere and the easiest way to recognize them is noticing that they don't believe in you until you prove the contrary.



How to avoid this kind of people? I have no idea. Actually, if you just decide to keep secret about your ideas it is already a good punishment for them. You will make them a little bit more losers.

Wednesday, May 6, 2009

The End of Facebook

Some time ago I reported here the reasons I decided to leave Facebook and review my practices in terms of socialization on the web. Believe me: I left Facebook for no other reason, but too much time dedicated to explore my network and expose myself. I wasn't influenced by anybody else or offended somehow. Since that time I've followed many news about this social network. Between many discussions, I was particularly curious about rumors of the end of Facebook and other social networks following the same model. Reading those articles I realized that part of my behavior when I left the site helps to justify existing theories. In addition, some market tendencies also reinforce their theory by comparing previous collapses with the current scenario. Then I recently decided to go back to observe this trend beside my own curiosity about my friends' news.



So, I listed here some points of reflection:
  • Need to leave the community - If we leave the community, how can we keep contact with our friends in an interesting way? This was my problem. When I left Facebook I tried to find my contacts in other social networks but I didn't find some of them. So, this is some sort of "proprietary friendship" that forces me to use Facebook to keep my whole network.

  • Privacy - this is an essential feature of all social networks. Since we cannot put together all our friends in only one site, most people have profiles in other communities and they have to configure their privacy options many times. Usually, people don't take care about details of their privacy. As a consequence, many profiles are open and their owners are not aware of it, which could be a problem due to evil people doing bad things using the web. A solution is the use of identification services like Google account, Yahoo account, OpenID, and others. You can use these services to authenticate users in your application without asking them to create a new profile, just reusing what they already have. This approach allows a unique profile for many applications, simplifying the privacy configuration. Unfortunately, it is not widely adopted yet.

  • Passive profiles - when a new and better social site is launched some of your friends will create a profile there, reducing their activity in the current one. Thus, you would lose important facts about your friends because they just decided to report in another network. The profiles become passive, only reacting when we send a message or any other alert.

  • Custom social applications - the concept of custom social applications, introduced by Facebook, can also be a potential threat. People can use these applications to promote other social networks or even offer complete external social features of third-party sites, dissipating the original concept of closed community to an aggregator of other communities or social services. This way, the Facebook concept is gradually diluted and transformed in a social platform instead of a centralized network.

  • Social platforms - Google got the concept of Facebook and transformed it in an open social framework that can be inserted in any site on the web. Basically, you can create your own social site and develop social applications to run in existing social sites. This blog, for example, uses this platform to list some friends on the right.

Words like "closed" or "proprietary" are not welcomed in social environments. Soon, social networks like we know today will be gradually dissipated until the whole web becomes a social platform, allowing users to compose social services as they want and keep all their contacts independent of any social service.

Sunday, May 3, 2009

When 55 Does Mean a Big Number

Last Saturday we were committed with what seemed to be a pleasant and relaxing tour through the interior of Belgium. A friend invited us to follow her family in an exciting bike experience, cycling during some hours to complete a route of 55 km called "De 8 Van Zemst", a Flemish name that means "the 8 of the city of Zemst".

Yes, it was a pleasant and exciting tour, but it was far from relaxing. Actually, it was surprisingly challenging because we never knew that 55 km "not walking" would be so tiring and painful. The good news is that we survived. The bad news is that now we need a pad to seat everywhere. I don't know about you, but every time I restart practicing cycling I never get used to the saddle within the first week because of an annoying pain in my butt. Last Saturday I'm sure that I cycled the equivalent of a whole month in just 5 hours. You can't imagine how it hurts.

Looking at other participants I was wondering if they were feeling the same pain. Well, they certainly would say "yes, we feel that pain" but I would hardly believe that, since they looked perfectly smiling, talking and joking sometimes. They are going to repeat this journey during seven days in their July holidays. Well, all we can do is wish all of them a really nice trip! We will be waiting for the news in a safe place in Louvain-La-Neuve.

What I've learned in addition is that there are dozen of other routes in the Belgian territory. The picture below shows the sign of our route on the bottom and the sign of other routes above. I can't really understand the green one but it shows how well organized Belgian people are when dealing with trips and adventures. This is indeed a motivation to explore this nice country, choosing easy routes and progressively migrating to complex ones.



This post is one more opportunity to say Thanks to the family who gave us such a warm welcome. To celebrate and remember this experience we have produced a video with some images of the get together. Enjoy it!

video