Saturday, July 19, 2014

Cosmos - a spacetime odyssey...κάτι που πρέπει να δεις



Δεν ξέρω πόσοι από εσάς σαν παιδιά πρόλαβαν την σειρά Cosmos του Carl Sagan είτε την χρονιά που ξεκινήσε (1980) , είτε λίγα χρόνια μετά στις αμέτρτητες επαναλήψεις. Θυμάμαι να την βλέπω σαν παιδί στην τότε ΕΡΤ. Ο Carl Sagan μιλήσε για το σύμπαν, το διάστημα, τον πλανητη μας, τα διάφορα φυσικά φαινόμενα, υπήρξε σήμειο αναφοράς για πολλές γενιές αργότερα. Λοιπόν αν σας ξέφυγε η αρχική σειρά έχετε μια δεύτερη ευκαιρία, επίσης αν εχετε και παιδιά σε  ηλικία που μπορούν να καταλάβουν πράγματα τότε η δευτερη ευκαιρίας σας γινεται πιο σημαντική, είναι υποχρέωση να την δείτε μαζί τους.

Μόλις σήμερα κατάφερα να ολοκληρώσω το remake της σειράς (2014), από έναν μαθητή του Sagan τον Neal deGrasse Tyson, που ακούει στο όνομα Cosmos - a Spacetime Odyseey. Πάλι στο ίδιο μοτίβο ο Tyson μας εξηγεί την ιστορία του σύμπαντος, του πλανήτη μας, φανερώνει τις ιστορίες μεγάλων επιστημών, μας προηδοποιεί για τις κλιματικές αλλαγές και την καταστροφή στον πλανήτη μας. Τα 2 αγαπημένα μου επεισόδια ήταν η ιστορία του μολύβδου και η δουλειά/έρευνα του C.C Patterson κατά των εταιριών παρασκευής βενζίνης και το πως φτάσαμε στην χρήση της αμόλυβδης, και το προτελευταίο όπου ο Tyson δίνει πολυ απλά και χαρακτηριστικά παραδείγματα για την κατακόρυφη άυξηση CO2 στον πλανήτη μας κάθε χρόνο και πως αυτή μεταβάλλει το κλίμα και κάνει τη Γη αρκετά πιο θερμή χρόνο με το χρόνο.. 

Σε πολλά επεισόδια έχει επιλεχθεί η εξιστόρηση με χρήση cartoon κάτι το οποίο κάνει την σειρά πιο φιλική και σε μικρές ηλικίες, ενώ τα γραφικά και οι εικόνες από το διάστημα ή απο κόσμους που δεν έχουμε μπορέσει ακόμα να επισκεφθούμε θα σας καθηλώσουν.

Αξίζει τον χρόνο σας, αν είστε εκπαδευτικός μην διστάσετε με την πρώτη ευκαιρία να  κάνετε προβολές και στα σχετικά μαθήματα.


Tuesday, July 01, 2014

Java EE7 and Maven project for newbies - part 7 - #jpa2 and Unit testing using #arquillian / #WildflyAs 8.1 #maven #jboss - using a real Datasource (#postgresql)

Resuming from the previous parts

Part #1, Part #2, Part #3, Part #4, Part #5 , Part #6

In the previous post (num 6) we discovered how we can unit test our JPA2 domain model, using Arquillian and Wildfly 8.1 In the previous post we made a simple configuration decision, we used the internal H2 database that is bundled with Wildfly 8.1 and the already configured Datasource (called ExampleDS). But what about a real DBMS? In this post we are going to extend a bit the previous work, use the same principles and 
  • test towards a running PostgreSQL in our localhost
  • use some of the really nice features of the ShrinkWrap API, Arquillian Offers.

Pre-requisites

You need to install locally a PostgreSQL RBDMS, my example is based on a server running on localhost and the Database name is papodb.

Adding some more dependencies

Eventually we will need to add some more dependencies in our sample-parent (pom). Some of the arem related to Arquillian and specifically the ShrinkWrap Resolvers feature (more on this later).

So our we need to add to the parent pom. xml the following


Some notes on the above change:
  •  In order to avoid any potential conflicts between dependencies, make sure to define the ShrinkWrap BOM on top of Arquillian BOM
Now on the sample-services (pom.xml) , the project that hosts are simple tests, we need to reference some of these dependencies.


Restructuring our test code

In the previous example, our test was simple, we we only used a certain test configuration. That resulted to single test-persistence.xml file and no web.xml file, since we were packaging our test application as a jar. Now we will upgrade our testing archive to a war. War packaging in JavaEE7 has become a first level citizen when it comes to bundling and deploying an enterprise application. The main difference with the previous example is that we would like to keep both the previous settings, meaning test using the internal H2 on wildfly, and the new setting testing towards a real RDBMS server. So we need to maintain 2 set of configuration files, and making use of the Maven Profiles feature, package them accordingly depending our mode. If you are new to Maven make sure to look on the concepts of profiles.

Adding separate configurations per profiles

So our test resources (watch out these are under src/test/resources) are now as illustrated below.


There are differences in both cases. The test-persistence.xml of h2 is pointing to the ExampleDS datasource, where the one on postgre is pointing to a new datasource that we have defined in the web.xml! Please have a look on the actual code, from the git link down below.

This how we define a datasource in web.xml


Notes on the above
  • the standard naming in the JNDI name java:jboss/datasources/datasourceName
  • the application server, once it reads the contents of the web.xml file, will automatically deploy and configure a new Datasource.
This our persistence.xml


Notes on the above
  • Make sure the 2 JNDI entries are the same both in the datasource definition and in the persistence.xml
  • Of course the Hibernate Dialect used for postGresql is different
  • The line that is highlighted is a special setting that is required for Wildfly 8.1 in cases that you want to deploy with one go, the datasource, the jdbc driver and the code. It hints the application server to initialize and configure first the datasource and then initialize the EntityManager. In cases that you have already deployed /configured the datasource this setting is not needed.

Define the profiles in our pom

In the sample-services pom.xml we add the following section. This our profile definition.


Depending on the profile actived, we instruct Maven to include and work with the xml files under a specific subfolder. So if we apply the following command

mvn clean test -Pdb2

Then maven will include the persistence.xml and web.xml under the resource-h2 folder and our tests will make use of the interall H2 DB. If we issue though

mvn clean test -Ppostgre 

Then our test web archive will be packaged with data source definition specific to our local postgresql server.

Writting a simple test

Eventually our new JUnit test is not very different from the previous one. Here is a screenshot indicating some key points.


Some notes on the code above:

  • The Junit test and basic annotations are the same with the previous post.
  • The init() method is again the same, we just create and persist a new SimpleUser Entity
  • The first major different is the use of ShrinkWrap Api, that makes use of our test dependencies in our pom, and we can locate the JBDC driver as a jar. Once located ShrinkWrap makes sure to package it along with the rest of resources and code in our test.war. 
  • Packaging only the jdbc driver though is NOT enough, in order this to work, we need a datasource to be present (configured) in the server. We would like this to be automatic, meaning we dont want to preconfigure anything on our test Wildfly Server. We make use of the feature to define a datasource on web.xml. (open it up in the code). 

  • The application server, once it scans the web.xml will pick up the entry and will configure a datasource under the java:jboss/datasources/testpostgre name. 
  • So we have bundled the driver, the datasource definition, we have a persistence.xml pointing to the correct datasourc. we are ready to test
  • Our test method is similar with the previous one.
We have modified a bit the resources for the H2 profile so that we package the same war structure every time. That means if we run the test using the -Ph2 profile, the web.xml included is empty, because we actually we don't need to define a datasource there, since the datasource is already deployed by Wildfly. The persistence.xml though is different, because in one case the dialect defined is specific to H2 and in the other is specific to Postgre.

You can follow the same principle and add a new resource subfolder, configure a Datasource for another RDBMS eg MySQL, add the appropriate code to fetch the the driver and package it along. 

You can get the code for this post on this bitbucket repo-tag.

Reference


Saturday, June 28, 2014

The JBoss/RedHat stack... a dream for many javaee developers :( #redhat #jboss #wildfly #arquillian

(postd as is in LinkedIn as well)

I strongly believe that the JavaEE spec and the JavaEE stack is going through an excellent phase. It is the summer of JavaEE as a fellow friend suggests. So in summer you always feel e bit more enthusiastic and happy etc. I have been experimenting a lot lately with JavaEE7 (and 6) technologies, especially with all the goodies that come from the RedHat /JBoss stack. Application server, Hibernate 4, Infinispan, Rest-Easy, Arquillian & ShrinkWrap, OpenShift. It's been a long time since I felt that I actually have tools and technologies in my hands that actually work, are mature and I can rely on.

But...then here comes the reality. This is an old and long running whine of me, from the early start of my career. I always wanted to work and build solutions using 'things' that came out of the JBoss house, but 9 to 10 times, I got the reply, 'the client has a big contract with this shop and the other shop' so we have to go with 'their' tool. And most of the times their tool was 2-4 years behind in terms of maturity on spec implementation. And most of the times we had to re-invent the wheel or cannibalize actually the application because their tool was not delivering, making a worse mess that using right from the beginning something that is actually working.

The good news? These recent years, most of the technologies that I was dreaming of to have in my deployment path :), became a standard! No I don't have to use your 5 year old, left over JPA implementation, i need to make your server work with the standard. I don't have to use this outdated REST implementation of yours, I need to wire something that is working. No I don't have to use your specific plugin, in order to deploy something that is considered standard, I want to use Maven or Gradle and I will be expecting to work.

Unfortunately, I still have to work and develop with tools, picked by others , due to some contract . Unfortunately still my stack of choice ...will be 9 out of 10 times, out of the scope.
I guess the only time this will change is when I will be able to control what tools to be used, using my developer experience from the past, in my own company :)