Friday, December 9, 2016

Red Hat JBoss FUSE ActiveMQ simple example

In this post I'm going to show how to develop simple ActiveMQ queue producer and consumer. Then I will explain step by step guide to deploy it to the JBoss FUSE server. If you do not have JBoss Fuse setup in your local PC go through in my previous blog post related to setup the JBoss Fuse. While I'm learning the stuff it gave me little bit frustration due to the lack of resources to learn this technology as a beginner. Then I though to put it as blog post so in future any one can refer the guide line.

Prerequisites 
  1. You should have install java 1.8 to support latest jboss-fuse-6.3.0
  2. You should have install JBoss Developer studio
  3. Your PC should setup Maven installed and configured.

01) Setup the ActiveMQ queue in FUSE.

First we have to start the fuse server using fuse.bat in the "bin" folder inside the installation directory on Fuse. (In my previous post I have explain how to setup the Fuse server correctly. If you need to setup go through previous post before you continue)



Once it successfully started you can see similar interface to above figure. Then you have to type following command to configure the fabric.

fabric:create
Then you can see similar figure to below.



 Then go to yellow colored link "http://localhost:8181"(which showed in above figure) it will automatically redirect to "http://localhost:8181/hawtio/login". Then provide the user name and password which you should set when you setup the Fuse. 




Then Fuse will create queue for you and you can see generated queue under queue tag as show in below figure.



Now we have created the queue we need to demonstration. Then type following command on the Fuse command interface to get the ActiveMQ URI.

fabric:cluster-list
 Then you will see similar output to below.




Then you need to select the default URI related to root (Since our queue setup under root)

tcp://DESKTOP-3KNHDOV:61617

02) Develop Camel application to send data to ActiveMQ queue and read them in FUSE.

Then go to Eclipse with Fuse integration and go to File -> New -> Fuse Integration Project. Then provide name for the project. I'll provided it as "DemoActiveMq"  then click next and select the JBoss Fuse run-time environment then click next. Next Interface select Start with empty project and Spring DSL as show on below figure.


Then Go to the POM.xml file and update following two tags as show on below.

<groupId>com.amq.demo</groupId>
<artifactId>amq</artifactId>
These two will important when install the build jar in to Fuse if you use different names you may need to change the command according to your change names in installing the jar to Fuse server.

Remove the content inside the <beans> tag and add following content in to that.

<camelContext id="_camelContext1" xmlns="http://camel.apache.org/schema/spring">
        <route id="amq_test">
            <from id="_from1" uri="activemq:queue:DemoQueue"/>
            <log id="_log1" message="${body}"/>
            <log id="_log2" message="Finished"/>
        </route>
        <route id="_route1">
            <from id="_from2" uri="file:///c:/work/activemq"/>
            <to id="_to1" uri="activemq:queue:DemoQueue"/>
        </route>
    </camelContext>
    <bean class="org.apache.activemq.camel.component.ActiveMQComponent" id="activemq">
        <property name="brokerURL" value="tcp://DESKTOP-3KNHDOV:61617"/>
        <property name="userName" value="admin"/>
        <property name="password" value="admin"/>

    </bean>


Note that brokerURL is the URL which we retrieved previous stage and password is the password setup in Fuse server.  Final camel context will be similar to below.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
    <camelContext id="_camelContext1" xmlns="http://camel.apache.org/schema/spring">
        <route id="amq_test">
            <from id="_from1" uri="activemq:queue:DemoQueue"/>
            <log id="_log1" message="${body}"/>
            <log id="_log2" message="Finished"/>
        </route>
        <route id="_route1">
            <from id="_from2" uri="file:///c:/work/activemq"/>
            <to id="_to1" uri="activemq:queue:DemoQueue"/>
        </route>
    </camelContext>
    <bean class="org.apache.activemq.camel.component.ActiveMQComponent" id="activemq">
        <property name="brokerURL" value="tcp://DESKTOP-3KNHDOV:61617"/>
        <property name="userName" value="admin"/>
        <property name="password" value="admin"/>
    </bean>

</beans>

Then go to the project folder and give the following command to build and install the project as usual Fuse integration projects.


mvn clean install
Then you should receive similar output to below.



Then go to the Fuse command line and enter following command to install the build jar in to Fuse server. (Please note that this is based on the names I have updated in the pom.xml)


osgi:install -s mvn:com.amq.demo/amq/1.0.0-SNAPSHOT

After successful installation if you log in to the "http://localhost:8181/hawtio/" then you will be able to notice that new section added under our queue in UI. refer the below fegure.



Now we can check the flow by dropping txt file in to "C:\work\activemq" location with some sample content. I'll added below content in to the text file and drop the file in to folder.

<a>
<name>Nirmal</name>
<country>Sri Lanka</country>

</a>

Then you can see the following output as expected. 


We have completed our first ActiveMQ demo. You can access the project code on following github location.

Friday, November 18, 2016

Read excel file from Apache POI (XLS or XLSX format)

In this post I'm going to demonstrate how to read excel file content from Apache POI library. When consider about Microsoft Excel there are two main versions in excel files. One is XLS format which results excel files save on Excel 1997-2003 format and other one is XLSX format which results excel files save Excel workbook format. To read these two formats we need to implement it in two different ways. I'll explain both ways in this post.

Prerequisites 
  1. You should have install java 1.7.
  2. You should have Eclipse installed in your PC.
  3. Your PC should setup Maven installed and configured.

First lets create maven project in Eclipse. Go to File -> New -> Maven Project then tick the check fox for the Create Simple Project (Skip archetype selection) as show on below figure and then click next. On next screen just provide some name for Group ID and Artifact ID then click finished.



First of all we need to add Apache POI dependencies in to project. (https://poi.apache.org/download.html from this link you can get latest version details. For this post I'll use current latest version which is 3.15)  Then add following dependencies for your POM.xml file. This should be based on the file format you are going to read as mentioned in below.

For excel format XLS (1997-2003)
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
</dependency>

For excel format XLSX(Workbook)

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.15</version>
</dependency>

Following main class can be used to read a excel file specified path.


package com.sample;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReader {

public static void main(String[] args) {
try {
String excelFilePath = "src/main/java/com/sample/Orders.xlsx";  
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
System.out.println(excelFilePath);
Workbook workbook = getRelevantWorkbook(inputStream, excelFilePath);
Sheet firstSheet = workbook.getSheetAt(0);
       Iterator<Row> iterator = firstSheet.iterator();
        
       while (iterator.hasNext()) {
           Row nextRow = iterator.next();
           Iterator<Cell> cellIterator = nextRow.cellIterator();
           while (cellIterator.hasNext()) {
               Cell cell = cellIterator.next();
               switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
default:
break;
}
               System.out.print(" ");
           }
           System.out.println();
       }
        
       workbook.close();
       inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static Workbook getRelevantWorkbook(FileInputStream inputStream, String excelFilePath) throws IOException
{
   Workbook workbook = null;
 
   if (excelFilePath.endsWith("xls")) {
       workbook = new HSSFWorkbook(inputStream);
   } else if (excelFilePath.endsWith("xlsx")) {
       workbook = new XSSFWorkbook(inputStream);
   } else {
       throw new IllegalArgumentException("Incorrect file format");
   }
 
   return workbook;
}

}

I will create excel file with following contents to read from our application.



When you run the main class you will be able to get similar output to below figure,



You can access sample project from following GIT hub location.