Archive

Archive for the ‘Infrastructure’ Category

Webapplications with node.js – Jade

In my previous post I described how you can install node.js and how to use express.js to create a webapplication for the node.js server. Now it is time to look into the templating engine – Jade – that express uses for outputting html to the browser.

As I did not have any knowledge yet of jade, it was time for me to do a quick study of jade. As a lot of github projects, the initial documentation is rather sparse, since it only showed an example of the jade template language will be translated into html. But luckily clicking on the github fork ribbon showed some more documentation on github. Basically it looked pretty easy, it’s just html with indentation meaning something and with all the >’s and <’s. replaced by ‘(‘ and ‘)’. At least that makes it easier to type in html
Read more…

Webapplications with node.js

I really think that javascript is going to change the server side world just as it has changed the client side world. The web is so much richer today using html 5 and javascript that it is hard to imagine it otherwise. By moving a lot of functionality to the client, the users will have a much better experience using a site, then using simple html pages with only server interactions. However building all those richness only in the client, leaves the server open for hacking attempts. Therefore you will have to code many of the same validations and checks twice. Once on the client for immediate feedback to the users, and once on the server to keep bad requests outside. So if you can use the same validations and checks both on the client and on the server, you can probably develop a bit faster and more secure.
Read more…

Is MongoDB a good alternative to RDBMs databases (like Oracle and Mysql)?

25/11/2011 1 comment

During the 10th Devoxx at Antwerp a presentation was given about MongoDB by Brendan McAdams from 10Gen.
This was one of the presentations during this event which I attended. I have to say I was pleasantly surprised by the ease of use and the features of this product. MongoDB is a NoSQL (non-relational, next-generation operational datastore and databases) database which is a schema-less database having huge advantages over most current existing RDBM databases.

The main pros of MongoDB can be divided in the following three:

  • Flexibility
  • Performance
  • Scalability

The aim of this article is to give an idea of this product, by giving an overview of advantages and disadvantages. Next, I will give some pointers to take into consideration in order to determine if this type of database is suitable for a specific situation or not. At the end of this article I will provide the link to the presentation that was given, providing more details (like example models & queries). Read more…

Configuring context-specific logging

At iPROFS, we have developed a certain habit when it comes to logging configurations.
This originates from certain clients who demand that deployable applications consist of

  1. the application artifacts (wars, ears)
  2. environment-specific configuration files

Being able to provide environment-specific configuration files, enables us also to create environment-specific logging configurations:
acceptance and production environment log at WARN level, test environment at INFO, development environment at DEBUG.

That’s fine, but how do you configure the logging provider per webapp, from a configuration file that’s external to the application?
Because that is what you need (unless you’re fine with creating multiple wars/ears with only a different configuration file set)

Log4j

For Log4j, JBoss already suggests a custom RepositorySelector.
This RepositorySelector lets every context class loader have its own configuration set up with its own log4j.xml file. This solution is targeted at putting the log4j.xml into WEB-INF/classes or whatever.
This is a fine starting point, however it’s still not external to the application.
So at iPROFS we normally use a slightly different version of this RepositorySelector, which looks for a <context-name>-log4j.xml file anywhere on the classpath. Now we can put the log4j configuration files in some external location.
This works great for Log4J.

Logback

Now enter Logback, the successor of Log4J, and a great default implementation of SLF4J.

Logback put quite some effort into documenting some possible setups for achieving per-context logging, but none looks like the Log4J repository selector described above.
So we rolled our own.

Solution

The ClasspathContextLogbackConfigurator we created, looks for a configuration file <context-name>-logback.xml on the classpath, just like the Log4J solution above. It feeds this configuration file to a basic logback configurator, and you’re set.

Combine this with the autoscan feature of Logback and you’ve got a environment-specific, runtime tunable Logback configuration.

To get you up to speed, I put the classes into a mavenized artifact.
This can be included into your webapp as a dependency:

<dependency>
  <groupId>nl.iprofs.modules.context-logging</groupId>
  <artifactId>context-logging</artifactId>
  <version>1.0.2</version>
</dependency>

Check out the source to adapt things to your needs.

Some final thoughts:

  • All webapps should ship their own log4j/logback jars, preventing them from all configuring the same logger instance from the parent classloader
  • where “on the classpath” is, is not discussed here. In Tomcat you could specify a custom directory to be added to the classpath, in $CATALINA_BASE/conf/catalina.properties, with the property ‘common.loader’

Getting started with GIT

Sure, there are many posts about GIT, and the documentation is quite good too. Even a crash course for Subversion users is available and covers most jargon and everyday commands.

So why this post? Well, most organizations have not yet upgraded to GIT nor do they intend to in the near future. The main reason for this is that their developers are not familiar with GIT and its advantages. In this post I describe how I use GIT locally and keep in sync with the organization’s Subversion repository. Doing so is a good way to get some exercise with GIT, and except for the remote syncing it is the same as using a GIT repository. Read more…

Categories: Infrastructure, iPROFS, Java Tags: , ,

Dynamically Name your Amazon Cloud Hosts

The Elastic Compute  Cloud (EC2) of Amazon offers flexible scaling for your services. It provides you with a ‘self-owned’ host in the cloud over which you can have full-control by selecting the operating system and ability to configure its applications/services.

The hostname of those hosts are not logically providing its functions. All the hostnames  in the EC2 cloud become of the format ec2-AA-BB-CC-DD.compute-1.amazonaws.com where a more human friendly name for its purpose may be better. Just like an IP address alone no ones really links that name to the purpose of some system, but when a database server would called db.amazonaws.com it makes the purpose of the easier to get. This would neither be possible as names in the same domain must be unique. Dynamic DNS could assist you here if you can insert the EC2 instances in your own domain name, for instance, db.mydomain.example.

In order to provide your hosts in the cloud with logical names, all you must have is a Dynamic-DNS enabled domain name and you need to setup and configure a Dynamic DNS client.

Step 1: Enable your domain name service with Dynamic DNS. For instance, you can register an account in a public service (DynDNS) that enables Dynamic DNS under their name service.

Step 2: Setup a hostname within the Dynamic DNS system. For instance, you can register a dynamically named host the domain of the public service of DynDNS.

Step 3: Install a Dynamic DNS client that connects to the name service. For instance, you can install ddclient at your host.

Step 4: Configure the Dynamic DNS client. For instance, DynDNS has even very nice option to generate the configuration of ddclient for you.

Step 5: Start the Dynamic DNS client that connects to the name service.

Now you reach your host with the assigned name.

Categories: Cloud, Infrastructure Tags:

Elastic Beanstalk, the DNS name of the service

Recently Amazon has launch their Elastic Beanstalk as part of there Cloud service offering. The Elastic Beanstalk functions as an infinite scalable application server while taking away a lot of system administration work. A closer look reveals that it is the combined functionality of the the Elastic Load Balancer and the Elastic Compute Cloud. As a result one does not need to setup any system with an OS, additional applications and neither does one have to consider system security. Therefore, only knowledge of building, uploading and deploying a war file is needed. These typical development tasks enable a developer to run and maintain a web application without the help of a system administrator.

The first step to use the Elastic Beanstalk service is setting up an amazon account if you have not done so already, because you might have bought something there, followed by the sign up for the service.

The second step is log-in to the Amazon Webservices Console and start registering your application. In the AWS console this is referred to as Environment and via ‘Create Environment’ you can setup, upload and deploy your first application. In the AWS console you can also maintain the application via ‘actions’ in the environment associated with the application. The most important part of this process is choosing the name of the application by which it becomes publicly available. The naming convention is <application>.elasticbeanstalk.com, for instance, the application myApplication would be accessed over the Internet as myapplication.elasticbeanstalk.com. You can actually choose any name you like as long as it is unique in the domain elasticbeanstalk.com. Of course, this limits the choice of the application name, but ideally you would run the service in your own DNS domain.

The last but third step which is hardly describe anywhere is to make your application available under your own fully qualified domain name, for instance, www.myapplication.com (assuming you own the domain myapplication.com). In that case a simple CNAME record in DNS where www.myapplication.com refers to myapplication.beanstalk.com. would suffice.

To summarize application setup in the Elastic Beanstalk:

  1. Create/Setup the account for the AWS console and sign up for the Elastic Beanstalk product.
  2. Setup your Elastic beanstalk application in the AWS console and upload/deploy the war file that is your website.
  3. Create a CNAME record in your DNS name referring to the beanstalk application.
Categories: Cloud, Infrastructure, Java Tags: ,

Spring configurations

In larger projects you often end up with some settings which should be stored in configuration files. These are mostly settings that won’t change much during the development lifecycle but can change based on which machine the application is running on. A good example for this is the database connect string that the application should connect on. A developer machine can connect to a mysql instance where as the production machine can connect to a large scale oracle database. When deploying the application to production, the deployers should then always check to see if these settings are still corrrect. This can cause a lot of errors in a real time envrionment.

When using maven, it is possible to capture these settings in profiles, but the builders have to remember to build the application using the correct profile, so that the correct settings can be used.

When using spring this can be done even simpler using the PropertyPlaceholderConfigurer bean. Most developers will likely have used this class, but there are some hidden gems in that class that not all developers are aware of.

Using the PropertyPlaceholderConfigurer is done by specifiying it in the context.xml that your application uses, for instance:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="database.properties"/>
</bean>

In your database.properties file you can set the key-value pairs of the parameters that you want to substitute, e.g.
db.url=com.mysql.jdbc.Driver
db.connect=jdbc:mysql://localhost:3306/test
db.username=testuser
db.password=testuser

Then in the rest of your xml you can use these values by referring to these parameters, e.g.
<bean id="datasource" ...>
<property name="url" value="${db.url}"/>
</bean>

Most developers are already familiar with this class and its usage so far, but you can do some more magic with this class. Especially for the problem of using multiple environment settings as described before, this class can be very convenient. The solution is to have multiple configuration files, where the last one loaded will overwrite the previous ones. If you make sure that the last one is your own properties file, then you are set. This leads to the following configuration:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/services.properties</value>
<value>classpath:/db.properties</value>
<value>file:${user.home}/db.properties</value>-->
</list>
</property>
</bean>

What this configuration does is that it will first load the services.properties in the root of the classpath (hence the classpath:/ notation). After that has been loaded the db.properties will be loaded. Then the file in the user.home directory for db.properties will be loaded, and in here lies the trick. Since this parameter is already parameterized (${user.home} is a system wide parameter), the system will try to load the db.properties that are defined in the user’s home directory. And thus if there does exists such a file in the directory, and it has also the defined parameters as defined in services.properties or db.properties, these user.home settings will override the default properties. In this way each developer can have it’s own database and username password without interfering with the production version. And even better, the production version can contain the username and password overwrites such that other users can not see what those are.

However this can still fail if the file is not present in the user.home directory. Therefor you should also configure this bean as follows:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/services.properties</value>
<value>classpath:/db.properties</value>
<value>file:${user.home}/db.properties</value>-->
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>

Now the system will not fail anymore if the resources defined can not be found.

When using the keys, you can also add default values for when the values are not found in the properties. This can be done by adding a seperator character in the key with a default value. Default value for this separator is the “:” character, so usage of the keys will look like this:
<bean id="datasource" ...>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username:testuser}"/>
</bean>

It is also possible to replace the “:” character seperator with another key, since the “:” can be used inside urls as well. It is also possible to add the default properties itself as properties, but I prefer using a default properties file for this.

Categories: Infrastructure, Java Tags: ,

5 things you didn’t know about … Command-line flags for the JVM

Nice blog post by IBM about JVM command line settings. I like the option to remove explicit garbage collection.

http://www.ibm.com/developerworks/java/library/j-5things11/index.html

Categories: Infrastructure, Java Tags:

Tomcat 7 is just around the corner!

With the Java EE 6 specification becoming final last december, the Apache Tomcat guys have been working hard to implement the new Tomcat 7 that supports the Servlet 3.0 and JSP 2.2 spec. The RC3 has been released yesterday.

The most exciting thing about Tomcat 7 is that it supports the Servlet 3.0 specification which is part of Java EE 6. Servlet 3.0 supports a more modulair style of webapps through webfragment, there is no need for one big web.xml anymore. It also lets you define Servlets and Filters through annotations. Another important new feature is the asynchronous processing of requests. If you want to learn more about the Servlet 3.0 specification check this article on dzone.

Besides the Servlet 3.0 API Tomcat 7 also supports JSP 2.2 and EL 2.2 but does not support any of the Java EE 6 profiles out of the box. Of course you can package all the libraries that you require with your application as you wish.

Tomcat 7 is not yet available through the official Apache Tomcat website but you can download the Tomcat 7 binaries from here: http://people.apache.org/~markt/dev/tomcat-7/. You will need at least Java SE 6 for Tomcat 7 to run.

For most of my application development I use IntelliJ. Even though IntelliJ officially supports Tomcat up untill version 6, your apps will deploy to Tomcat 7 seamlessly.

Categories: Infrastructure Tags: ,
Follow

Get every new post delivered to your Inbox.