Saturday, March 7, 2020

Java EE Micro Services Using Thorntail


The Software Development world is growing every minute and as it evolves the systems are becoming more and more normalised and refined into Micro Services, this has also inspired the likes of Spring Boot and many other frameworks.

I have realised that when it comes to enterprise software most Java developers are either using Spring or  Java EE / Jakarta EE. For the who are using Spring we have Spring Boot to help us with fast project kick off and development. More to that some use it for the Micro Service pattern or methodology. What happens when you you as a developer want to use JEE? Spring Boot may not be what you are looking for and perhaps you are looking for something along the lines of WildFly.

We will have a look at a baby project along these lines, a new sidecar project supporting WildFly to enable deconstructing the application server as is and pasting just enough of it back together with your application to create a self-contained executable jar. This project is formerly known as "Wildly Swarm" and now known, as Thorntail. I mentioned Spring Boot and now you can imagine where this is going. 

1. Assumptions

Now remember that we spoke about Micro Services so the best use case for this article would be something along the lines of some sort of API. In this case we will create a RESTFul API. We assume that you have an understanding of the following tech / tools.

  • WildFly AS
  • Java 8 
  • Maven
  • JAX-RS

2. Project Setup Using Maven

Let's set up the project using maven, as our automated build tool and also  grab some wildly fractions (dependencies) we need. Worry not we will cover a little bit about fractions as we go. We will start off with some basic properties settings. These can be anything you prefer so feel free to change these properties up. Just the important one is that since this will be a web application we want to package it as a ".war" file, so keep that one as is in the example.

<name>Thorntail REST API Showcase</name>

We have more of our properties, mainly for our versions in this case. We are also now telling maven that our Java version will be 8. At the moment [07-03-2020] the latest version of Thorntail is "2.6.0.Final". So we will be using the latest version. As of Jboss 7.x we no longer really need the ".web.xml" so the tag  <failOnMissingWebXml>  also is there to make sure that we don't get failures as we will not be using the ".web.xml" file in this example. 


Maven being a very good and strong dependency management tool, it also has a very cool feature using BOM (Bill Of Materials) which pretty much allows us to access a set collection of various dependencies and hand pick the ones we need for our app. So in this case we will import the Thorntail specific BOM and then choose the dependencies we need for our JAX-RS API.


There's a Thorntail Maven Plugin maven plugin available that can help us work on packing our application much easier. Let's set that up next.


Earlier in the article we mentioned "fractions". This is a concept based on Thorntail, a "fraction" can include all or none of the WildFly components. Ultimately a fraction contributes configuration or capabilities to a runtime system. This is simply because we are loading in smaller pieces that we need, thus, Fractions. Looking at our current topic, (Thorntail) these fractions are in the form of dependencies. Let's go shopping for some ingredients that make up our REST Service API.


That's it for the project set up with maven. As you can see, the fractions we are pulling in are very basic and specific, not need for the entire heavy WildFly. We have hand picked only Swaggertogether with its own web application piece, for the Web UI and finally the core piece we need for our REST API, JAX-RSAt this point we are now ready for some code implementation. 

3. Java REST Resource Implementation Using JAX-RS API

Let's now create a Rest resource implementation using Java and JAX-RS API. 

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Api( value = "/text", tags = "Text API")
@Produces( MediaType.APPLICATION_JSON)
public class SampleResource {

            value = "Gets a text message",
            notes = "Returns the message as HTTP Resposne",
            response = Response.class)
    @Produces( MediaType.APPLICATION_JSON)
    public Response get() {
        return Response.status(Response.Status.OK)
                .entity( "Hey, there, you requested for text?")

According to the JAX-RS standards, the next step is to include a class extending to define the rest @ApplicationPath and also register our rest resources. 

public class ResourceConfig extends Application {

We will be adding the second last piece to our implementation before we can test it out. We will include a class implementing the ContainerResponseFilter interface, which works like a filter for the ContainerResponse extension point on the server side, in order to filter the response message after the invocation has executed:

public class CORSFilter implements ContainerResponseFilter {
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        responseContext.getHeaders().add("Access-Control-Allow-Origin", "*");
        responseContext.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        responseContext.getHeaders().add("Access-Control-Max-Age", "-1");
        responseContext.getHeaders().add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

Let's configure our Wildfly port and context-root just because port 8080 is so over rated, plus we will be looking at an alternative to setting up, similar to the spring boot way with the or .yml file. In your source code resources folder, add a new file project-defaults.yml

Add the following piece of yml in your file. 

    port: 8881
    path: that-service

We are now all set, it's time to build our app and then try to run it and see. Let's talk to maven to see if it can help us bootstrap the rest service.

4. Run The Project Using Maven

Build the app. Make sure you are in the root project folder where you can run your pom file.

$ mvn clean install

Run the application, remember there's no standalone application server. So we are actually going to just execute our ".jar" file as mentioned in the beginning of this article. Let's get on with it then. 

$ java -jar target/thorntail-api-thorntail.jar

NB: At this point it will boot up very quick and you should see something along these lines. Just click the image below to enlarge it.

5. Swagger Checkout

So let's check the swagger out for some basic testing and checks. Now head over to the link http://localhost:8881/swagger-uiand you should see a swagger screen with the default API loaded. 
NB: A quick look at the default swagger home page once you launch it. Just click the image below to enlarge it.

You probably want to check your API, so while you are on that swagger screen just insert the following URL into the top text box. http://localhost:8881/that-service/swagger.json and then press enter. This will load your API in the web app and you should be able to see your API as shown in the image below.
NB: Web view of what it looks like once you have pointed it to your API. Just click the image below to enlarge it.


So there you go. A standalone service using Wildfly and some Java EE, similar way you would have it using Spring Boot. We have learned how to build one and the nice part was that fractions section which allowed us to hand pick which bits of wildfly we would want. Sometimes working with a full fledged application server is necessarily bloated and heavy. Incase you wondered how you can work with Java EE in Micro Service then this is another way you can do it.  
Note: Postman example of our API test, incase you were wondering. 

You may also find the GitHub Code Base incase you want to double check our exercise. I hope this has been helpful, leave comments in the section below. 

Wednesday, January 22, 2020

Setting Up Custom PowerShell Scripts On Terminal Load



So I am used to having my own shell functions load up automatically when I open my terminal window to perform various operations on a daily basis. In other words I prefer having my own short cuts as functions or aliases for commands that I need and use on a daily basis. For those who want to know more about the reason for this you may refer to this older article

One day a landed in an organization where the team preferred using Windows OS over others. If you have read the article above you will understand that this was a different ball game for me, and obviously I wanted to have the same effect on Windows OS. Yes with the latest Windows OS 10 one can set up bash, but then how would I ever learn something new, in this case PowerShell.

Oh well ... 

Let's Get Started

We will be looking at a step by step process of setting up  your own custom PowerShell functions which will load automatically every time you open your PowerShell terminal. This will simplify working with those long complex commands which you probably need to use every day.


1. Locate Or Create The Default Folder

  • Open up Windows Explorer and head over to the "Documents" folder.
  • Look for a folder with the name "WindowsPowerShell". If the folder is not there then create it. This folder is the home / default directory for your PowerShell profile. Make sure you use that specific name.
  • Open the folder. Just note that if you did not have to create this folder and it was already there then it will probably contain a have a sub folder named "Scripts", which you can ignore right now.

2. Creating Your Custom PowerShell Script / Function

  • While you're still in that "WindowsPowerShell" folder, create a new PowerShell script file i.e I created mine with the name and extension Message-Printer.ps1 and please note that you can name it whatever you want, just keep the extension ".ps1" and that's it.
  • Open up the file in your favorite text editor or IDE, I prefer VS Code for most of my scripting. I also think that in this case it works out because it's a Microsoft IDE which means there's good support for PowerShell out of the box. So go ahead and open that file to start editing the as follows: 

function showThisMessage() {
    # Print out the message
    Write-Host "Welcome to PowerShell automation!"

  • Ultimately the following image shows how things should be on you machine at this point. Have a look to confirm and then we can carry on.

3. Configuring Auto Load Of Your Script  

For each time you open your powershell terminal, there's a session that gets created and that session has some sort of profile that it's running with it. When a user does not have any custom profiles setup then they can work with the default one. For example we are going to work with a default one which will be activated when we open up the PowerShell terminal. 

NB: You may click the following link for more info about PowerShell Profiles. Let's get to the work...

  • Create a new file inside your "WindowsPowerShell" folder with the following name, Microsoft.PowerShell_profile.ps1, keep the name of file exactly like that since that's the default profile naming.
  • Open up the file in your favourite text editor or IDE.

 # This code may show as two lines but, please write it in one line.
 Get-ChildItem -Path $PSScriptRoot\Message-Printer.ps1 | Foreach-Object { . $_.FullName } 

  • For more validaton you may refer to the image below: 

  • So what's happening in that line is basically a search for our new custom script, "Message-Printer.ps1". This search will then import this script into your PowerSehll session by default when a new PowerShell terminal tab is opened. This is the PowerShell sytax and way of loading other scripts. It's achieved through the power of the Get-ChildItem commandlet which is used to get the specified file locations on the file system. 
  • The actual path is picked up dynamically with the $PSScriptRoot part of the script and then later we can suffix with our file and its extension.
  • And finally the Foreach-Object commandlet used to perform the same operation on a collection of files.

4. Running The Custom Script

Great! So now make sure you have saved all your files from your editor or IDE and then give your script a test.
  • Open up the PowerShell terminal.
  • Immediately start typing the function you created earlier. So just type showThisMessage in your terminal tab followed by the "return" or "enter" key ... and BOOM! Your text show be displaying as follows: 




So there you go! You now know to automatically load up your custom PowerShell scripts. Hoping it was helpful and many of you will try and use this method. 

Wednesday, June 22, 2016

My Transition To UNIX From DOS: Working With Aliases


It's been fun playing on a Windows OS machine as a Software Engineer. I always find it interesting and fun to explore other things. Funny enough though, I have always been scared of using commands because I always felt that I would mess up something on my machine and have to do a factory reset of some sort. It's always a drag when one has to do that on their work machine. Finally I landed at a place where the recommended OSes were the unix based ones, preferably, Linux Ubuntu or Macintosh OS. Ultimately I went for the Macintosh OS, just because... 

From my first day there I had to learn some basic and common unix commands from the likes of: tail -f /file/path/here up to, ssh At some point I got a bit fed up with typing a long and same old command everyday and to a point, every hour even. Even though it was great practice, something had to be done and that's when I learned about Unix aliases. Aliases are pretty much abbreviations of FUCs "Frequently-Used Commands" 

For example one can shorten a command like: defaults write AppleShowAllFiles NO, which toggles hidden files & folders off, in Mac OS's finder, to something like show-hidden-files which will essentially be the name "Alias" of that command. 

Now to the "Nuts & Bolts", we will look at the following:
  1. Creating / Adding a new alias
  2. Referencing an external file for aliases.
We will also need some "shell scripting skills", nothing major, very few lines of code so that's all fun and well

1. Creating / Adding a new alias

Perhaps this may be slightly different on other unix platforms, so this is more pertinent to Mac OS. Let's go...!
  • Open finder and head to your home directory. (Unless you are in your home directory by default) 
  • Look for a file named, ".bash_profile". Notice the dot before the file name. If you cannot see hidden files then use the command (in your terminal)  from the example above and just change that "NO" to "YES". Then hold down the "alt" button and then "alt-click (right-click)"  on the finder icon and then "left click"  the option "Relaunch".
  • Go back to looking for our file and then open it in any of your favourite text editor... (Sublime Text, Atom, even VI on your terminal etc ...)
  • Let's add a new line, this will be a new alias for any command you want to add, for example:  

  alias show_files_no="defaults write AppleShowAllFiles NO"

  • To delineate things a bit more, "alias" is a reserved key word for shell, so you are letting shell know that you want to add a new alias, the next word "show_files_no" can be anything you want, this is the actual name or alias of your command. Then last part "defaults write AppleShowAllFiles NO" is the actual shell command that you would normally type and execute on the terminal. We are almost done. 
  • If you were editing the file using the terminal with VI, VIM, Nano etc... The next thing is to reload the ".bash_profile"  by either closing and opening the terminal again or typing the command "bash -l". I believe there may be more commands out there that one can use. 
  • Finally open terminal if you closed it. If you reloaded the ".bach_profile"  using the command then just type the alias you added recently, in our case just type "show_files_no". This will execute the command associated with that alias. To see if this made an effect just follow the steps about relaunching finder above. That's it!

2. Referencing an external file for aliases.

I have recently been playing around with externalising some bash profile stuff because I have a lot of aliases and my bash profile artifact was just getting too congested. So main things to note here is that
... you don't have to create your new external file inside the same directory as your bash profile and the name of your file does not have to start with the word "bash"...
I just named it that way for the sake of naming it that way! So let's get to it:
  • Create a new file as follows, "~/bashes/.whatever_file_name", of which in my case, is inside a new folder that I created, "bashes", in my home directory and named it ".bash_aliases". Keep in mind that your folder can be named anything you want.

  • Now go back to your main file, ".bash_profile" and then replace your alias with the following shell scrip code.

  #Referencing path to file containing the aliases
  if [ -f $aliasesPath ]; then 
     source $aliasesPath

  • Something along those lines should help you out. It's basically a shell if statement that checks if that file path exists and if so we then reference it from the main ".bash_profile" so next thing you should try is to now reload like we did earlier if you did all this using terminal or just close and then open terminal.  
So now you have externalised the aliases and you can try with other stuff like your environment variable profiles can also be externalised and so forth. Like I said this is the first main thing I learned when moving to UNIX, I hope it helps someone out there. I would really love to learn from you on how I can improve this post and some feedback, Cheers! 

Tuesday, February 11, 2014

Starting with Acitiviti (BPM Engine)


I am busy working on a project which has a very small workflow or business process component to it. One of the requirements was that we had to use Activiti to manage the business processes. My first port of call was the "Activiti in Action" book. When I felt that had enough ammunition to take on Activiti, I gave it a go.

There were a few bumps along the road especially because I was trying to incorporate Acitiviti into an existing Spring Maven application. In this blog I am going to show you how to get your Activiti code running in a Maven Spring application.

Activiti Architecture

The Basic Activiti architecture is pretty simple. There is a database which is used to maintain and persist state. You can manipulate the state of objects within the database by using the Activiti engine or API. This can be the Activiti libraries or dependencies in your application or you can also interrogate the database using the Rest services.

The Activiti Explorer is a web application which uses the API to view and change objects housed in the database. The Activiti Explorer also has a tool (Activiti Modeler) which can be used to model BPMN processes. You can model your process using this tool and then export the configuration into an xml file. You can then use this xml file in your application without having to model the process manually. 


You will need to download Tomcat and the latest Activiti Release. The only 2 artefacts of concern to you for this tutorial can be located in the wars directory:
  • activiti-rest.war
  • activiti-explorer.war 
You can deploy these 2 war files on your Tomcat server. To be really specific, you only need the activiti-explorer. You might need to add the H2 database Driver to your Tomcat server.

Do I need a database?

Activiti does use a database but you do not need one explicitly. Activiti is packaged with an embedded H2 database. This means that when you run the application, it will access this embedded database automatically. So even though you are not specifying a database, there is one being used.

Add these dependencies to your Maven POM file

     <!-- Activiti Stuff-->  
 <!-- Spring Stuff-->  
 <!-- H2 Driver-->  

Add this repository to your Artefact repository or your Maven POM file

       <id>Alfresco Maven Repository</id>  

Design a Simple Activiti Workflow process using Activiti Modeler

This is the sample process that I designed. It is simple and self explanatory.
Here is the exported xml file contents (SampleActivitiProcess.bpmn20.xml):
 <?xml version="1.0" encoding="UTF-8"?>  
 <definitions xmlns="" xmlns:xsi="" xmlns:activiti="" xmlns:bpmndi="" xmlns:omgdc="" xmlns:omgdi="" typeLanguage="" expressionLanguage="" targetNamespace="">  
  <process id="SampleActivitiProcess" name="SampleActiviti" isExecutable="true">  
   <startEvent id="start" name="start"></startEvent>  
   <userTask id="SomeUserTask" name="SomeUserTask"></userTask>  
   <endEvent id="end" name="end"></endEvent>  
   <sequenceFlow id="flowFromStart" name="flowFromStart" sourceRef="start" targetRef="SomeUserTask"></sequenceFlow>  
   <sequenceFlow id="flowToEnd" name="flowToEnd" sourceRef="SomeUserTask" targetRef="end"></sequenceFlow>  
  <bpmndi:BPMNDiagram id="BPMNDiagram_SampleActivitiProcess">  
   <bpmndi:BPMNPlane bpmnElement="SampleActivitiProcess" id="BPMNPlane_SampleActivitiProcess">  
    <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">  
     <omgdc:Bounds height="30.0" width="30.0" x="165.0" y="210.0"></omgdc:Bounds>  
    <bpmndi:BPMNShape bpmnElement="SomeUserTask" id="BPMNShape_SomeUserTask">  
     <omgdc:Bounds height="80.0" width="100.0" x="301.0" y="185.0"></omgdc:Bounds>  
    <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">  
     <omgdc:Bounds height="28.0" width="28.0" x="495.0" y="211.0"></omgdc:Bounds>  
    <bpmndi:BPMNEdge bpmnElement="flowFromStart" id="BPMNEdge_flowFromStart">  
     <omgdi:waypoint x="195.0" y="225.0"></omgdi:waypoint>  
     <omgdi:waypoint x="301.0" y="225.0"></omgdi:waypoint>  
    <bpmndi:BPMNEdge bpmnElement="flowToEnd" id="BPMNEdge_flowToEnd">  
     <omgdi:waypoint x="401.0" y="225.0"></omgdi:waypoint>  
     <omgdi:waypoint x="495.0" y="225.0"></omgdi:waypoint>  
I would advise you to always change the ID's of all of the elements and also provide them with descriptive names. This helps you out tremendously when developing against your BPMN process.

Let us start Coding

The general process of running an Activiti process is as follows:

Design BPMN Process

You can use Activiti Modeler from Activiti Explorer, the Eclipse Activiti Plugin or manually build the xml representing your business process flow.

Deploy Process

You can use Activiti Explorer to deploy or programatically deploy your business process flow. Doing it programatically, you have 2 options:

  • The Spring way
  • The Standard java way

Run Process Instance

You reference a process which is deployed and start a new process instance.

Create Activiti process engine

The are 3 ways to achieve this:

In Memory

 ProcessEngine processEngine = ProcessEngineConfiguration  

This is the easiest and fastest way to get up and running. It references the embedded database and uses the default configuration.

File based

 ProcessEngine processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();  
The database connection settings are referenced from the activiti.cfg.xml

Here is a sample file refencing the embedded default H2 database:
 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns=""  
   <!--<bean id="processEngineConfiguration"  
   <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">  
     <property name="jdbcUrl"  
          value="jdbc:h2:/tmp/activiti;AUTO_SERVER=TRUE" />  
     <property name="jdbcDriver" value="org.h2.Driver" />  
     <property name="jdbcUsername" value="sa" />  
     <property name="jdbcPassword" value="" />  
     <!-- Database configurations -->  
     <property name="databaseSchemaUpdate" value="true" />  
     <!-- job executor configurations -->  
     <property name="jobExecutorActivate" value="false" />  
     <!-- mail server configurations -->  
     <property name="mailServerPort" value="5025" />  
     <property name="history" value="full" />  

Basic Spring Style

     ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Service-core-application-context.xml");  
     ProcessEngine processEngine = (ProcessEngine) ctx.getBean("processEngine");  

Here is the Spring configuration required:
   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
     <property name="driverClass" value="org.h2.Driver" />  
     <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />  
     <property name="username" value="sa" />  
     <property name="password" value="" />  
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
     <property name="dataSource" ref="dataSource" />  
   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
     <property name="databaseType" value="mysql" />  
     <property name="dataSource" ref="dataSource" />  
     <property name="transactionManager" ref="transactionManager" />  
     <property name="databaseSchemaUpdate" value="true" />  
     <property name="deploymentResources"  
          value="classpath*:SampleActivitiProcess.bpmn20.xml" />  
     <property name="jobExecutorActivate" value="false" />  
   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
     <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />  
You can see in this way, Spring handles deploying the BPMN Process for you.

It is important to know that BPMN Process xml files can only be of 2 types and have to end in the following extensions for the API to process / deploy the files:

  1. .bpmn20.xml (Activiti Modeler)
  2. .bpmn (Eclipse Activit Plugin)
The 2 services that we require is the RuntimeService and the RepositoryService:
 RepositoryService repositoryService = processEngine.getRepositoryService();  
 RuntimeService runtimeService = processEngine.getRuntimeService();  

Remember that if you are using the Spring way, the respective beans already exist.
We use the RepositoryService to deploy the process, if it has not already been done (the spring way or via the Activiti Explorer console):

We then start a process using the RuntimeService:
 String procId = runtimeService.startProcessInstanceByKey("SampleActivitiProcess").getId();  

A process is considered to be active if it is not in an end state or not suspended. You can use the Task Service to manipulate active tasks:
 TaskService taskService = processEngine.getTaskService();  
 List<Task> tasks = taskService.createTaskQuery().active().list();  
Remember that the TaskService bean already exists if you are using the Spring way.

You can use the TaskService to query, claim and complete tasks:
 taskService.claim(task.getId(), "sampleUser");  

Once the Process in a Final state, you can't use the TaskService for these processes but need to use the HistoryService:
 HistoryService historyService = processEngine.getHistoryService();  
     HistoricProcessInstance historicProcessInstance =  
     System.out.println("Process instance end time: " + historicProcessInstance.getEndTime());  
Remember that the HistoryService bean already exists if you are using the Spring way.


This should be enough information to get you up and running with Activiti. Remember to start off small and build on incrementally. This way you can understand the process in much greater detail.
Activiti is a great tool and what is even more amazing is that this piece of software is open source and free to use.

As always, I would love to hear your comments or questions. You can also follow me on Twitter or Google plus. I have added shortcuts to my profile in the About me link.