Pages

Showing posts with label jbossas. Show all posts
Showing posts with label jbossas. Show all posts

Wednesday, May 18, 2016

Hello World with JBoss Modules


JBoss Modules is quite an interesting project that powers JBoss application server and some other projects in JBoss ecosystem. However, I was surprised to find out that there isn't much you can find about Modules on the webs. Documentation is... bad half-done, not that many tutorials, no good examples of how you could use this awesome library in your project. The best you can find is the description on how to apply JBoss Modules within the application server. (sad panda)

I was looking for the simplest "Hello World" example and couldn't find it. Well, why not create one myself then? 


Downloading JBoss Modules

A surprising fact is that you won't find JBoss Modules in the list of upstream projects at jboss.org. The first option is to download the jboss-modules.jar from Bintray or Maven Central. And the second option is to build it from sources. 

Oh, ok, one more option (not the best one) is to download the application server that includes jboss-modules.jar, e.g. WildFly.


Hello World

Ahh, the good old "Hello World" :) The main application class is as follows:

public class Main {
  public static void main(String[] args) {
     new Hello().say();
  }
}

So we have a dependency, the Hello class, that will reside in a different module:

public class Hello {
  public void say(){
    System.out.println("Hello!");
  }
}

So to mimic the modules we first have to compile both classes and assemble corresponding JARs. Plus, a proper directory layout is expected by JBoss Modules to resolve the artefacts.


Main class belongs to 'app' module, and Hello class belongs to 'hello' module. Each module requires module.xml descriptor. This part is somewhat documented actually. Also the 'main' directory that you see within each module's directory structure is actually a version (!). 
A version slot identifier is an arbitrary string; thus one can use just about any system they wish for organization.  If not otherwise specified, the version slot identifier defaults to "main".
Here's the module.xml for the app module:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.5" name="app">
  <main-class name="Main"/>

  <resources>
    <resource-root path="main.jar"/>
  </resources>

  <dependencies>
        <module name="hello"/>
  </dependencies>
</module>

It specifies the main class (i.e. Main), the reference to the actual JAR that will be used in this module's classpath, and a dependency - the 'hello' module.

Same module.xml for the 'hello' module:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.5" name="hello">
  <resources>
    <resource-root path="hello.jar"/>
  </resources>
</module>

Voila! Now we can execute our brand new modular "Hello World" app:

> java -jar jboss-modules-1.5.1.Final.jar -mp mods app
Hello! 

The format for the command is as follows. First, java -jar jboss-modules.jar is used to bootstrap the environment; -mp mods, and the 'app' parameter is the name of the application module that should be executed.

This example isn't really practical, but at least it gives a hint on how to get started with JBoss Modules. Hopefully, one day, the documentation for this awesome project will be complete and there will be a bit more tutorials for different the use cases.



Thursday, October 30, 2014

Deploying Spring Petclinic demo application to JBoss/WildFly

Spring Petclinic is a very good demo application to experiment with - it is simple enough, yet demonstrates quite a good number of features. Usually I deploy it to Tomcat and obviously don't have any issues with it - it just deploys, runs, and works as expected.

Recently, however, for demonstration purposes, I needed to deploy this application to JBoss (or WildFly). And this is not as straightforward as one might expect.

First I tried with JBossAS 7.1.1.Final. The deployment fails with the following exception:

11:42:38,247 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/petclinic]] (MSC service thread 1-3) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring/business-config.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.JoinColumn.foreignKey()Ljavax/persistence/ForeignKey;
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) [spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
...

The latest Petclinic application (as of October 2014) uses JPA 2.1 and JBoss 7.1.1 bundles JPA 2.0 APIs. So I decided that it should probably work out of the box on WildFly since it comes with JPA 2.1 jars. And it did - the application deployed just fine, but then there's another library that prevents the application from operating properly - Dandelion:

Exception starting filter dandelionFilter: com.github.dandelion.core.DandelionException: The protocol vfs is not supported. 
        at com.github.dandelion.core.utils.ResourceScanner.scanForResourcePaths(ResourceScanner.java:204) [classes:] 
        at com.github.dandelion.core.utils.ResourceScanner.findResourcePaths(ResourceScanner.java:138) [classes:] 
        at com.github.dandelion.core.bundle.loader.spi.AbstractBundleLoader.loadBundles(AbstractBundleLoader.java:89) [dandelion-core-0.10.0.jar:] 

The issue was reported but at the time of writing this post the fix wasn't published yet. So hopefully Dandelion v0.11.0 will be capable to be deployed on WildFly.

So to overcome this and deploy Spring Petclinic (at the state of October 2014) to WildFly 8.1 one would have to get rid of the dependency on Dandelion, and rewrite some of the JSPs not to use Dandelion taglibs. Then the application deploys and works just fine.

So after making all the fixes I got Spring Petclinic running deployed to WildFly and could monitor it with XRebel:



Thursday, December 16, 2010

Apples vs Oranges. Java Container Startup Times

Just recently with @toomasr we've studied how fast are the Java application servers at the boot time. The boot time for an application server is an integral part of what we call the developers' productivity.

The most surprising this thing about the results was that the JBoss 7 average startup times where extremely fast compared to the previous JBoss versions.



At the beginning we couldn't believe this improvement is for real and assumed that JBoss 7 is an OSGi container. But just a bit later some JBossAS developers commented on the blog entry, that actually JBoss 7 has been completely redesigned for classloading performance.

Andy Miller said:
The startup times really don't have anything do with the OSGi stuff that is in there. It's based on a new services architecture underneath, which can start services and load classes in parallel (there are some JVM limits in class loading that make it serial, but we hope that will disappear), and take advantage of the fact that there are multiple cores on virtually every machine someone would use today.

To learn about the new features in JBoss 7, the highly recommended listening can be found at the JBoss Asylum podcasts page.

Thumbs up for the JBoss engineers, really! At least some AS vendors do care about the developers productivity! :)

Disqus for Code Impossible