When building a real application you often find yourself having to deal with different stages of the software. The most common stages are development, testing and production, but you can have many more. This means that you need a different environment to deploy the application on each of the current stages. You use different environments to be able to perform versioning, different configurations, test bug fixes and so on. This also poses challenges on upgrading environments, changing shared configuration or keeping track of the servers. I will show you how to do it with Tomcat.
The easiest way to set up multiple Tomcat instances is to duplicate the entire Tomcat folder and change a few configurations. I don’t advise doing it this way, since it’s harder to maintain, harder to spin up new instances and harder to upgrade. Instead, we will set up the instances in a much more flexible way, by duplicating only a few things and keeping a shared base folder for all instances.
I’ve used version 7.0, but this should also work with other versions. I’m also doing the setup in a Unix like environment. This can also be accomplished in a Windows box, but the commands need to be adjusted.
Unzip the installation folder to a directory of your choice. I just recommend to do it in a parent folder and you can use a name like tomcat or server.
Now, instead of using the unzipped folder, we are going to create a link to it, like this: ln -s apache-tomcat-7.0.64/ current
To keep this simple, we are going to create two instances: development and production. But keep in mind that you can create as many as you want by making the necessary adjustments to the scripts.
Folders
Create a folder now named instances or environments. Inside, create a folder named development:
If you wish, you can now remove these folders from the Tomcat install folder, but is not mandatory.
Home and Base
The idea here is to share the main Tomcat folders and each instance has a copy of their personal folders to not clash with each other. Tomcat defines two environment variables called CATALINA_HOME and CATALINA_BASE that allow us to do that.
Create a bin folder in the instances development. Add the following exec.sh script:
Note: If you are using MacOSX, you might need to install core-utils using brew and replace readlink by greadlink to achieve the proper behaviour.
This script is going to set up the proper configuration variables to point to our shared Tomcat and the specific instance folders. Note the properties http.port, https.port, ajp.port and shutdown.port are included in the CATALINA_OPTS environment variable. With these we can pass specific configuration to the server.xml file. Tomcat is smart enough to perform property replace substitution as long as you have the proper placeholders in place.
Files
All these operations are performed in the development folder instance.
conf/server.xml
Edit the file conf/server.xml do the following changes;
Replace
By
8080
${http.port}
8443
${https.port}
8009
${ajp.port}
Note: Unfortunately the only place where property replacement doesn’t work is the shutdown port. I think this is a bug in Tomcat and should be fixed. So for now, we need to keep it hardcoded.
bin/exec.sh
On the bin folder, create links to exec.sh to the following files: catalina.sh, startup.sh, shutdown.sh.
This will allow you to call the original Tomcat, but by calling the exec.sh set up first. The magic is done by the line $CATALINA_HOME/bin/"$(basename "$0")" "$@" in the exec.sh script.
Run
The instance should be ready to be executed. Just run it as you would do it normally by executing sh catalina.sh run or sh startup.sh from the development instance folder.
Additional Instances
Just duplicate the development instance folder to a production one and edit the bin/exec.sh to update it with different ports. You can user 7080 for http, 7443 for https, 7009 for ajp and 7005 for the shutdown.
Since property replacement is not working properly for the shutdown port, we need to manually edit conf/server.xml from the production instance and replace 8005 by 7005. When this bug is fixed, and you actually use a property, you don’t have to worry about doing this.
Note: You might need to reestablish the proper links in the scripts catalina.sh, startup.sh and shutdown.sh stored in the bin folder.
After this, your second instance production is ready to run. If you need more, just repeat the last steps, making sure to pick ports that don’t conflict with the instances already set up.
Perks
With this set up you can now:
Create new instances easily with minimum changes. You can actually have one untouched unchanged instance, that you can use to copy from to create others.
Update the Tomcat version, just by installing a new distribution and updating the link to current.
If you place jars in the libs folder of the HOME installation, they become instantly available to all instances.
Instead of duplicating the conf folder, you can actually link to the one in HOME and also share the configuration between all environments. Or just link to the files you want to share.
Remove an instance, by just deleting its BASE folder.
Last Thursday, 9 July 2015, the ninth meeting of Coimbra JUG was held at the Department of Informatics Engineering of the University of Coimbra, in Portugal. The attendance was great. We had around 30 persons and considering that July is a month where a lot of people go on vacations, we can’t complain. We had the pleasure to listen to António Gonçalves talking about CDI. A great opportunity to interact with one of the greatest experts in CDI and Java EE in general. The best of all: it was in Portuguese!
In my opinion, CDI is a specification with great potential. It will probably become one central piece of all the other specifications in the Java EE world if is not already. For that reason, it makes sense to dedicate sessions to CDI CDI. Even more, when a lot of developers don’t know it is out there and others use little bits, without knowing that they are using CDI. Not many people from the audience was using CDI. António, demonstrated some of the core features of the specification. With the help of JBoss Forge, António created a small web app and showcased Dependency Injection, Qualifiers, Producer, Scopes, Alternatives and Events.
As always, we had surprises for the attendees. IntelliJ sponsored our event, by offering a free license to raffle among the attendees. Congratulations to Vitor Loureiro for winning the license. Develop with pleasure! We also handed a few Tomitribe and ZeroTurnaround t-shirts.
The third edition of Devoxx UK 2015 was in London, between 17th and 19th June. This was my first conference since I joined Tomitribe. The dynamics were a little different for me this time. I actually had a triple role: attendee, speaker and exhibitor. Since Tomitribe was also sponsoring the event I had to spend some time at our booth to interact with other developers using TomEE and to promote to the ones that don’t know it yet. During that time, we also worked to push out the latest release of TomEE with most of the Java EE 7 support!
Venue
Devoxx UK was held at the same location as last year: The Business Design Centre. This year, more rooms were available, since the number of attendees was also higher. I don’t have the exact number, but I guess that close to 1 thousand might have attended the event. The Exhibition Hall was more or less the same, but I was surprised to see that some usual sponsors didn’t make it this time. The Community Hall pretty much had a non-stop Hackergarten well managed by Heather VanCura. By the way, what is wrong with the picture? I rule as a photographer, right? 🙂
Sessions
The program was interesting enough, a lot of diverse subjects and you had five options to attend on each scheduling bracket. I was not always into sessions, since I also needed to spend some time in the Exhibition and Community Halls. Not a problem, because all the sessions were recorded and are going to be available on Parleys, which is cool. These are my top 3 sessions (from the ones I have attended):
You should get the first two sessions on Parleys. But the third one is already available:
Also, have a quick look to this awesome Ignite Session by Tonya Rae Moore:
My Session
I’ve got to speak about the Five Ways to Not Suck at Being a Java Freelancer. It may sound a little strange, since I’ve put the Freelancer life on hold. I’ve submitted the session before that event and I think it’s still valid to provide some of my experience during my Freelancer life. Here is a session about it:
This is not exactly related to the conference itself, but since I was in London I’ve met with my good friend Simon Maple to conduct our second Book Reading session. If you are not aware, vJUG started a new initiative: the Book Reading Club. The idea is for the attendees to gather around a book, read it and discuss it. We started with the awesome book Effective Java by Joshua Bloch. We had our second meeting around the book and it was fun to hear Josh explain some of the decisions made around the development of the Collections API, Generics and For each statement. A must see:
Community
The Community was awesome as always! This time I had the pleasure to have with me some very good friends from Portugal:
When thinking of stuff to do in the Hackergarten, I had the idea to write a small piece of code to perform Method Caching using the new JCache API. I’ve soon discovered that I’ve gotten beaten by Andy Gee (no hard feelings). Anyway, we had some interesting discussions about it with Bruno Baptista and Sebastian Daschner. Check the result: JCacheExamples.
Java EE BoF
Also on the Java EE BoF we had the usual discussion between Java EE and Spring. In my honest opinion, I think this discussion is pointless. Both technologies can be used together and everyone is free to pick the parts that fit their project more. On the other hand, I think that Spring has done a fantastic job with Spring.io website. Java EE has a lot of information, but it’s all spread around the Internet. I believe that this causes a bad impression to new developers and in the end they may end up favouring Spring more.
Also, the adoption of Java EE 7 has been slow in the organizations. There is no Application Server support yet and companies are lacking the resources to perform the migration. I’ve personally been working in a Java EE 5 to 7 migration and I intent to make a session about it.
20 Years of Java
Do you have any idea what was going on when Java was born?
During the event, there was a lady taking polaroid pictures of developers. These pictures went into a wall and organized by the year you wrote the first line of Java code. I remember that it was in 1996 for me. I was writing an HTML website, but was unhappy that I couldn’t do any kind of dynamic behaviour. Searching on the web, I’ve found the solution: Java Applets! I bought a book called “Learn Java in 30 Days” and I wrote my first line of Java in the Notepad. I wondered where was IntelliJ at that time!
Final Thoughts
That’s all! Hope you enjoyed this report. Cya next year!
Development tools have been evolving over the last few years. Github revolutionized the way we share and contribute code between projects. Docker made distributions of applications much easier by allowing you to provide an environment for your application to run. Both these technologies simplified the distributions of your applications, but there is still a pain point. Developers that want to tap into your source, edit and build it still have a hard time to set up all the tools. I believe that Codenvy might be the tool to fill this gap.
What is Codenvy? Codenvy is an IDE in the cloud. You can use it to view, edit, build and deploy code. All of this with just a browser. Codenvy integrates closely with Github and Docker and here is when things become interesting. This allows you to set up an environment that you can share with anyone. Why is this interesting? I already had a few readers asking me for help to set up their environments because they struggle to do it themselves. Several combinations of IDE’s and Operating Systems make the task difficult. Maybe there is another way.
My application about Java EE 7 and Angular is probably the most popular I have. I’ve decided to provide it in Codenvy to hopefully make it easier for all of you that want to try the application out.
Setup
After you signed up for Codenvy, you can easily create a new project by pointing your working sources to a git repository. In this case we are going to use, of course the Java EE 7 with Angular Github repository. You might need to specify to Codenvy that this is a Maven project. If everything was setup properly, you should get something similar to this:
In the IDE window, you can browse the code with syntax highlighting and basic code completion. You can also build the project with maven, if you use the Build menu.
Runners
Checking the code is nice, but we are more interested in actually running the code. Codenvy uses a preset of Docker containers to cover a large number of application servers and environments. There can be found in the Runners tab. Unfortunately, there is no Runner provided for Wildfly and the Glassfish one, doesn’t come with the required database. I’ve ended up by writing my own Docker container to provide a custom made Runner for the Java EE 7 Angular sample application. Here are the Dockerfiles:
Codenvy Wildfly Dockerfile
This Dockerfile is just to set up the environment needed to run the application. Add Java and Wildfly to the base Codenvy container.
This Dockerfile is to map the source code available in Codenvy to a folder in the Docker container. In this way, we can then deploy the code to an application server.
RUN sudo touch~/wildfly/standalone/deployments/ROOT.war.dodeploy
Don’t bother too much about this. Codenvy will import into the project the Dockerfiles that are stored at the project structure in .codenvy/runners/environments/[NAME]. Since I added my runner to the project sources, this will be imported automatically.
Run the Application
To run the application, just press the Play button in the top right corner. You should see the log of the container starting up and also an url at the bottom. This url is the one you need to use to access the application.
And that’s it! You can now play with the application! You even have a Terminal at your disposal.
Sharing the environment
It was easy to set up the environment here, but wouldn’t it be cool if we could just share it with other people? You could do that by using Factories. Just import your Codenvy project into a Factory and you have available an url to share, which will set up everything for you. So, to access the Java EE 7 Angular application, please use:
Unfortunately, I’ve also encountered a few problems when I was setting up the environment.
The code does not get updated in the mapped folder of the running container. If you make changes to the code, you need to restart the container. Even if they are just HTML or Javascript changes for instance.
In Safari the Runner buttons are not visible, but they are clickable.
In Firefox I couldn’t type a dash or other special characters in the terminal.
Terminal sometimes is not visible.
The code not updating is a real pain. Other things are minors. I hope that this could be fixed soon.
Conclusion
Codenvy is a very impressive tool, if we take into account that we are talking about a cloud browser base IDE. I don’t think that Codenvy is going to replace conventional IDE’s. Although it’s a great alternative to distribute your applications and give the chance for other developers to try them out with minimal effort. Again, here is the link for the JavaEE 7 with Angular application:
Today, I’m officially part of Tomitribe. I’m very excited to work with all the amazing tribers working hard to make TomEE a compelling Java EE server.
Some of my readers follow me because I work as a Freelancer and this move may confuse you. Let me assure you, that I’m not joining Tomitribe because I was unhappy with my Freelancer career. In fact, when I’ve started as a Freelancer a couple of year ago, I had no idea how things were going to turn out. Today, I can say that it was the right move. I had the chance to work on my own stuff, travel to conferences around the world, met a lot of different people and have fun in general.
In Tomitribe, I believe I will be able to do all the things that I love as a Freelancer and many more. Until now, I only developed or built applications that sit and run on a server. From now on, I have the chance to work on the server itself. Tomitribe is still a startup, a small one, but with incredible potential. After working for a few major corporations, I’m eager to help and grow something from the ground up. This was just an opportunity that I couldn’t refuse.
I have to say thank you to Decare Systems Ireland and Anthem for two wonderful years. They always understood my needs and gave me the freedom to explore my own things.
Moving forward, a big thanks to David and Amelia for believing in me and bringing me to Tomitribe. I hope I can full fill your expectations. Cheers!
I’m sorry you are missing Andy, but I couldn’t find a better picture 🙁
Anyway, you can expect me to keep the blog running with awesome and independent content. Thank you for reading!