Tuesday, March 4, 2014

Maven - Deploy war in Tomcat 7 & 8

This article will explain on how to deploy a war fine in to Tomcat 7 through maven build.

Note : I have tested same settings for Tomcat 8 as well. So below settings can be apply for both versions.

  • Configure tomcat7-maven-plugin in pom.xml
   <!-- Tomcat plugin -->
   <plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.2</version>
    <configuration>
     <path>/${project.build.finalName}</path>
     <update>true</update>
     <url>http:// localhost:8080/manager/text</url>
     <username>tomcat</username>
     <password>tomcatuser</password>
    </configuration>
   </plugin>

  • Configure user rights in the tomcat-user.xml which is in the tomcat conf file.
   <role rolename="tomcat" />
   <role rolename="manager-gui" />
   <role rolename="manager-script" />
   <role rolename="admin-gui" />
   <user username="tomcat" password="tomcatuser"
    roles="tomcat,manager-gui,admin-gui,manager-script" />
  •  Once above steps are followed, You can deploy the war by running following maven goal
mvn tomcat7:redeploy

More details of the Apache Tomcat Maven Plugin refer maven-plugin-2.2


Monday, February 17, 2014

Java EE 7 is here! What’s New in Java EE 7

          The most important goal of the Java EE 7 platform is to simplify development by providing a common foundation for the various kinds of components in the Java EE platform. Developers benefit from productivity improvements with more annotations and less XML configuration, more Plain Old Java Objects (POJOs), and simplified packaging. The Java EE 7 platform includes the following new features,

The main features of the new specifications :

  • Java API for WebSocket - WebSocket is an application protocol that provides full-duplex communications between two peers over TCP. The Java API for WebSocket enables Java EE applications to create endpoints using annotations that specify the configuration parameters of the endpoint and designate its lifecycle callback methods.
  • Java API for JSON Processing - JSON is a text-based data exchange format derived from JavaScript that is used in web services and other connected applications. The Java API for JSON Processing (JSON-P) enables Java EE applications to parse, transform, and query JSON data using the object model or the streaming model.JSON-P is new to the Java EE 7 platform. The Java EE 7 platform requires JSON-P 1.0.
  • Batch Applications for Java Platform - Allows for description of a Batch Job using Job Specification Language defined by an XML schema. It defines a complete execution sequence of the jobs.
  • Concurrency Utilities for Java EE - The Concurrency Utilities for Java EE is a standard API for providing asynchronous capabilities to Java EE application components through the following types of objects: managed executor service, managed scheduled executor service, managed thread factory, and context service.

The main features of the updated specifications :

  • Java API for RESTful Web Services - The Java API for RESTful Web Services (JAX-RS) defines APIs for the development of web services built according to the Representational State Transfer (REST) architectural style. A JAX-RS application is a web application that consists of classes packaged as a servlet in a WAR file along with required libraries.
  • Java Message Service - The Java Message Service (JMS) API is a messaging standard that allows Java EE application components to create, send, receive, and read messages. It enables distributed communication that is loosely coupled, reliable, and asynchronous.
  • Expression Language - This chapter introduces the Expression Language (also referred to as the EL), which provides an important mechanism for enabling the presentation layer (web pages) to communicate with the application logic (managed beans). The EL is used by both JavaServer Faces technology and JavaServer Pages (JSP) technology. The EL represents a union of the expression languages offered by JavaServer Faces technology and JSP technology.
  • Enterprise JavaBeans - Support for EJB 2.1, EJB QL, and JAX-RPC-based Web Service endpoints and client view is now optional.
  • Servlets - Defines a standard mechanism to upgrade existing HTTP connection to a different protocol using HttpUpgradeHandler, Offers nonblocking request and response processing for async servlets, Defines rules for which HTTP methods are covered by <security-constraint>
  • JavaServer Faces - Faces Flow provides an encapsulation of related views/pages with application defined entry and exit points.
  • Java Persistence - The Java Persistence API provides Java developers with an object/relational mapping facility for managing relational data in Java applications.
  • Interceptors - Interceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events. Common uses of interceptors are logging, auditing, and profiling.
  • Contexts and Dependency Injection - Contexts and Dependency Injection for Java EE (CDI) is one of several Java EE features that help to knit together the web tier and the transactional tier of the Java EE platform.
  • Bean Validation - Validating input received from the user to maintain data integrity is an important part of application logic.
  • Java Transaction - 
  • @Transactional provides the application to declaratively control transaction boundaries on CDI-managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method-level annotations override those at the class level.
  • JavaMail - @MailSessionDefinition and @MailSessionDefintions defines MailSession to be registered with JNDI
  • Java EE Connector Architecture - Provides @AdministeredObjectDefinition , @AdministeredObjectDefintions , @ConnectorFactoryDefinition , and @ConnectorFactoryDefinitions to define a connector-administered object and factory to be registered in JNDI

         You can find the Java EE7 sample in Java GitHub site. Go to javaee7-samples

Sunday, February 2, 2014

Install nodejs and Grunt in Ubuntu 13.10

You can install the nodejs, bower and Gruntjs using below commands.


  • Install nodejs
      sudo apt-get install npm
  • Install Grunt 
     npm install -g grunt-cli
  • Install bower
 npm install -g bower

After install above libraries run the grunt command.  If you see an error you can run the below commands to overcome in ubuntu 13.10.
sudo apt-get install nodejs 
sudo ln -s /usr/bin/nodejs /usr/bin/node 
curl https://npmjs.org/install.sh | sudo sh

Thursday, January 16, 2014

MySQL Commands

Important MySQL Commands.


  • How to check if multiple instance are running on the Server
sudo netstat -tap | grep mysql
  • Kill mySql processors
pkill mysqld
  • Start , Stop or Restart MySQL
/etc/init.d/mysql start|stop|restart|reload|force-reload|status
  • Backup MySQL Database
mysqldump -u root -p DB > database_dump.txt
  • Restore MySQL Database
mysql -u [uname] -p[pass] [db_to_restore] < [backupfile.sql]
  • Back up MySQL Database with Compress
mysqldump -u [uname] -p[pass] [dbname] | gzip -9 > [backupfile.sql.gz]
  • Restore compressed backup files
gunzip < [backupfile.sql.gz] | mysql -u [uname] -p[pass] [dbname]
  • Restore a database that already exists
mysqlimport -u [uname] -p[pass] [dbname] [backupfile.sql]

Tuesday, November 12, 2013

Google Gson

Google gson is one of the powerful tool which you can convert your Java Object to the JSON and vice versa. Other toll is jackson which you can convert your Java Object to the JSON as well.

I have done a small program which has been done with Google Gson.

Main Gson Implementation Class
package rezg.camel.client.util;

import com.google.gson.Gson;

public class JsonConvertor <t> {

 private T obj;

//This method will convert a passing Object to a Json Strcuture
public String convertToJson(T myObj) {
  Gson gson = new Gson();
  String retVal = gson.toJson(myObj);
  return retVal;
 }

//This method will convert a Json String to an passing Object Type
 public T convertToObject(String jsonString, Class<t> retVal) {
  Gson gson = new Gson();
  obj = gson.fromJson(jsonString, retVal);
  return obj;
 }
}
 
Convert Json String to a Java Object
 private static Customer getObject(String jsonStr) {

  JsonConvertor<Customer> cusJsonConvertor = new JsonConvertor<Customer>();

  Customer myCustomer = cusJsonConvertor.convertToObject(jsonStr,
    Customer.class);

  System.out.println("myCustomer.toString() " + myCustomer.toString());

  return myCustomer;
 }
Convert Java Object to a Json String
 private static String getJson() {

  Customer customer = new Customer();
  customer.setAddress("My Company Address");
  customer.setName("My Comppany");
  customer.setAge(45);

  List&ltString> employees = new ArrayList&ltString>();
  employees.add("Adam");
  employees.add("Jhon");
  employees.add("Anne");
  customer.setEmployees(employees);

  List&ltInventory> invetories = new ArrayList&ltInventory>();
  Inventory inv = new Inventory();
  inv.setAmount("2500");
  invetories.add(inv);

  inv = new Inventory();
  inv.setAmount("3000");
  invetories.add(inv);

  inv = new Inventory();
  inv.setAmount("4000");
  invetories.add(inv);

  customer.setInventories(invetories);

  JsonConvertor&ltCustomer> cusJsonConvertor = new JsonConvertor&ltCustomer>();
  String jsonStr = cusJsonConvertor.convertToJson(customer);

  return jsonStr;
 }

Monday, October 7, 2013

ActiveMQ - How to create multiple instances on the same server

Follow the below steps to create multiple instances on the same server.

Go to your activemq directory bin folder and run the below commands
Create the Instance 1
cd /apache-activemq-5.8.0/bin
./activemq create instance1
./activemq setup ~/.activemqrc-instance-instance1
ln -s /home/[yourHomeDir]/.activemqrc-instance-instance1
Create the Instance 2
./activemq create instance2
./activemq setup ~/.activemqrc-instance-instance2
ln -s /home/[yourHomeDir]/.activemqrc-instance-instance2
Once above commands are executed, go to the instance2 conf and change the default port for the openwire, amqp in the activemq.xml and also change the Connector in jetty.xml.
You can start each instance as below.
cd apache-activemq-5.8.0/bin/instance1/bin
./instance1 console
Open a new Tab
cd apache-activemq-5.8.0/bin/instance2/bin
./instance2 console
Instance 1 - Web Console
Instance 2 - Web Console


Thursday, August 22, 2013

Maven - How to merge files


You can use maven-merge-properties-plugin to merger property files.

<plugin>
    <groupId>org.beardedgeeks</groupId>
    <artifactId>maven-merge-properties-plugin</artifactId>
    <version>0.1.1</version>
    <configuration>
     <merges>
      <merge>
       <targetFile>${Target file name with the src path}</targetFile>
       <propertiesFiles>
        <propertiesFile>${property file 1 with the src path}</propertiesFile>
        <propertiesFile>${property file 2 with the src path}</propertiesFile>
        <propertiesFile>${property file 3 with the src path}</propertiesFile>
        <propertiesFile>${property file 4 with the src path}</propertiesFile>
       </propertiesFiles>
      </merge>          
     </merges>
    </configuration>
    <executions>
     <execution>
      <phase>compile</phase>
      <goals>
       <goal>merge</goal>
      </goals>
     </execution>
    </executions>
   </plugin>