Friday, January 27, 2017

log4j with maven and Java - Project Example


log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

This utility will help you to keep track of everything your code does (and the government will keep track of everything you do in your life, just kidding -__-). Well, I'm going to show a short but solid example on how to use log4j. In this project we are going to log everything into a text file.

For this exercise we are going to use:
  • Java 1.7
  • Maven
  • log4j
  • Eclipse IDE

First of all, let's create a Maven project in Eclipse. (if you don't know how to install Maven in your PC, please go here)



Select option "Create a simple project"





Then fill all the fields according the bellow image. Here we are putting the info needed for our pom.xml




When done with these settings, open your pom.xml file and add the next lines:



        <properties>
  <jdk.version>1.7</jdk.version>
  <log4j.version>1.2.17</log4j.version>
 </properties>

 <dependencies>

  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>${log4j.version}</version>
  </dependency>

 </dependencies> 



Your pom.xml should look like this:




What we just did, was add some dependencies to our project, so with the help of mighty Maven, we are going to install them. Right click on pom.xml and select "Maven install"... Maven will pull all the jars needed (to your .M2 reposotory), in this case log4j






You should get a "BUILD SUCCESS" message in your console:





Log4j needs some properties file in which we specify its configuration. Let's create a "log4j.properties" file and added to our project:






Once created, double click on it, and add the following code:



# Root logger option
log4j.rootLogger=ERROR, stdout, file
log4j.rootLogger=INFO, stdout, file
# Redirect log messages to console log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Rirect log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=D:\\log4j-application.log log4j.appender.file.MaxFileSize=5MB log4j.appender.file.MaxBackupIndex=10 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n


Here we are specifying how and where to append the logs. Take a look at those lines and change accordingly.


Now we are going to proceed to write our code. Create a class and let's name it "HelloExample.java", here is the code:



package com.rolandoFebrero;

import org.apache.log4j.Logger;

public class HelloExample {
 
 final static Logger logger = Logger.getLogger(HelloExample.class);

 public static void main(String[] args) {
  
  HelloExample obj = new HelloExample();
  obj.runMe("Log4j Example");
  
 }
 
 private void runMe(String parameter){
  
  if(logger.isDebugEnabled()){
   logger.debug("This will be logged as DEBUG : " + parameter);
  }
  
  if(logger.isInfoEnabled()){
   logger.info("This will be logged as INFO : " + parameter);
  }
  
  logger.warn("This will be logged as WARN : " + parameter);
  logger.error("This will be logged as ERROR : " + parameter);
  logger.fatal("This will be logged as FATAL: " + parameter);
  
 }
 
}



now, create another class, this time let's name it "HelloExampleException.java". Here is the code:


package com.rolandoFebrero;

import org.apache.log4j.Logger;

public class HelloExampleException {

final static Logger logger = Logger.getLogger(HelloExampleException.class);
 
 public static void main(String[] args) {
 
  HelloExampleException obj = new HelloExampleException();
  
  try{
   obj.divide();
  }catch(ArithmeticException ex){
   logger.error("Something wrong!!!, you are getting an exception ", ex);
  }
  
  
 }
 
 private void divide(){
  
  int i = 3 / 0;

 } 
}



If you look at the code, in our first class, HelloExample.java, we are just printing a message, but with different log levels, such as debug, info, warn, error, fatal. This helps us to identify the kind of message we want to log. On the other file, HelloExampleExceptio.java, we are performing some math operation. In the main method we are making a call to "divide()". This divide() method just performs one single math operation, which is "3 / 0". Obviously, we can't divide a number by zero, so we will get an exception, in this specific case, an Arithmetic exception. I did that on purpose so we can see how an exception is logged by log4j.

Now, just right click on either HelloExample.java or HelloExampleExceptio.java; you might see the message in you console. Then go to where "log4j-application.log" file is located (hint: specified in log4j.properties) and there you'll find all the logs...


If you want to download the code for this project just go to my github account:

https://github.com/rolando-febrero/log4j-maven-Example.git




Programming thought of the day:


  • If at first you don’t succeed; call it version 1.0.


Friday, January 20, 2017

JAX-RS, Rest Service in Java with Jersey


JAX-RS stands for JAVA API for RESTful Web Services. JAX-RS is a JAVA based programming language API and specification to provide support for created RESTful Webservices. JAX-RS makes heavy use of annotations available since Java SE 5 to simplify development of JAVA based web services creation and deployment. It also provides supports for creating clients for RESTful web services..

In this case, we are going to use build a RESTful Web Service in Java using Jersey. But, what is Jersey? According to Jersey website (https://jersey.java.net/): 

"Jersey framework is more than the JAX-RS Reference Implementation. Jersey provides it’s own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs.

Goals of Jersey project can be summarized in the following points:
  • Track the JAX-RS API and provide regular releases of production quality Reference Implementations that ships with GlassFish;
  • Provide APIs to extend Jersey & Build a community of users and developers; and finally
  • Make it easy to build RESTful Web services utilising Java and the Java Virtual Machine."
For this exercise we are going to use:
  • Java 1.7
  • Tomcat 7
  • Jersey
  • Eclipse IDE
Open Eclipse and create a Web Dynamic Project. Let's name the project JAX-RS-Jersey. We don't need to create an EAR to our project, so unselect that option.



then, click next and on Web Module screen, select "Generate web.xml deployment descriptor" option 




Then, we need to place Jersey .jars in: Webcontent/WEB-INF/lib folder. You can get them @ https://jersey.java.net/ . Also I'll include all these files in my GitHub account, I'll explain at the end of this tutorial. Your workspace should show you the jars. If you don't see them, refresh the project



In Eclipse, go to Java Resources folder, right click on "src" and add a new package, let's name it: com.rolandoFebrero.rest (you can change these names to anything you want, this is a free country, don't worry). Now, create a new class inside that package: Hello.java

Again, create another package com.rolandoFebrero.restclient and inside that, create another java class, let's name it ClientTest.java. Your workspace should look like this:




In Hello.java, paste this code:



package com.rolandoFebrero.rest;

/**
 * Created by: Carlos Rolando Febrero
 * 
 */

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/hello")
public class Hello {
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello there, RESTfull service with Jersey (plain)";
  }
  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello there, RESTfull service with Jersey (xml)" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello there, RESTfull service with Jersey (html)" + "</h1></body>" + "</html> ";
  }

} 


In ClientTest.java, paste this code:



package com.rolandoFebrero.restClient;

/**
 * Created by: Carlos Rolando Febrero
 * 
 */

import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;

public class ClientTest {
  public static void main(String[] args) {
    ClientConfig config = new ClientConfig();
    Client client = ClientBuilder.newClient(config);
    WebTarget target = client.target(getBaseURI());

    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_PLAIN).get(String.class));
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_XML).get(String.class));
    System.out.println(target.path("rest").path("hello").request().accept(MediaType.TEXT_HTML).get(String.class));
  }

  private static URI getBaseURI() {
    return UriBuilder.fromUri("http://localhost:8080/restfuljersey").build();
  }
} 


In web.xml paste this code:



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
     <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.rolandoFebrero.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app> 



Now, let's add Tomcat server to our workspace. Click in Server tab and add a new server...





Select Apache Tomcat server:




Select you Tomcat path and click Finish:



Now, that you have added Tomcat, right click and start:



Go to Console tab and make sure there are no error, if so, deelete Tomcat and added it again. After that add our project to Tomcat and restart the server:




At this point, if everything is OK, you should be able to go to: http://localhost:8080/JAX-RS-Jersey/rest/hello

You'll get the following:




Want to download the code for this project?

go to my github account: https://github.com/rolando-febrero/JAX-RS-Jersey





Programming thought of the day:


  • Unix is user friendly. It’s just selective about who its friends are.


Monday, January 9, 2017

What is the difference between Data Analytics, Data Analysis, Data Mining, Data Science, Machine Learning, and Big Data?


Lately, I've been doing some research on Machine Learning, which seems to be very interesting and impressive from my point of view. Creating software was always interesting, but coding to "educate" your software in a way that it can learn from previous experiences makes this even more interesting and more impressive. However, if you try to find information about Machine Learning, you will see some other topics that are closely related to it, which are: Data Analytics, Data Analysis, Data Mining, Data Science, and Big Data but, How do they differ from each other?

Here are some core concepts:

Data AnalyticsAnalytics is about applying a mechanical or algorithmic process to derive the insights for example running through various data sets looking for meaningful correlations between them. 

Data AnalysisAnalysis is really a heuristic activity, where scanning through all the data the analyst gains some insight

Data Miningthis term was most widely used in the late 90's and early 00's when a business consolidated all of its data into an Enterprise Data Warehouse. All of that data was brought together to discover previously unknown trends, anomalies and correlations such as the famed 'beer and diapers' correlation (Diapers, Beer, and data science in retail).

Data Sciencea combination of mathematics, statistics, programming, the context of the problem being solved, ingenious ways of capturing data that may not be being captured right now plus the ability to look at things 'differently' (like this Why UPS Trucks Don't Turn Left ) and of course the significant and necessary activity of cleansing, preparing and aligning the data.

Machine Learningthis is one of the tools used by data scientist, where a model is created that mathematically describes a certain process and its outcomes, then the model provides recommendations and monitors the results once those recommendations are implemented and uses the results to improve the model

In addition, I found a discussion about this topic and I wanted to share some thoughts which I consider it can help to clarify:

"The way I see it, machine learning is concerned with algorithms whose performance at some task improves as it gains experience at that task, while data mining is concerned with analysing data for the purpose of discovering unforeseen patterns or properties.

So the similarities are obvious, they both look at data, and hope to extract something of value from it. As I see it, the main difference is whether the goal is to reproduce known knowledge (I know that some of these pictures are cats, and some are dogs, now can some algorithm learn that?), or if the goal is to discover unknown knowledge (is there any interesting structure in this data set?). The two are, unsurprisingly, intertwined, as many of the properties or structure one may be searching for in data mining can be identified by machine learning algorithms. For instance, in data mining, one might be interested in determining if clusters of a certain form appear in the data, and could use a machine learning algorithm like k-means. K-means is a learning algorithm, in that if data has a known structure, it can learn it (under specific conditions, blah blah blah).

So data mining is exploratory, machine learning is focused on solving specific tasks well. That's my take on it, anyway." (by: Jordan Frank)


The following graphic nicely summarizes what all is involved in data science.










Programming thought of the day:


  • The truth is out there. Anybody got the URL?

Tuesday, December 20, 2016

Java SOAP WebService and WebService Client with Apache Axis



Creating and deploying a simple WebService is not as hard as you think. For this example we are going to work with Apache Axis WebService. Axis is essentially a SOAP engine ,a framework for constructing SOAP processors such as clients, servers, gateways, etc.
Apache Axis is a SOAP toolkit that makes it easy to create, deploy, and consume web services. By using Axis, you will be able to quickly convert existing Java functionality into web services, deploy those services, and be able to communicate with them remotely via the Internet, usually through firewalls (if necessary).

More precisely, Axis is an implementation of SOAP. Most people like to think of it as a toolkit. From the programmer's point of view, it's like an API. It makes it easy for you to communicate - with SOAP - to a remote object, without having to worry about the details associated with the protocol. In practice, it is a lot like using RPC, CORBA, RMI, or any of the other distributed computing technologies you may have experience with. The process is largely the same: you describe the target of your communication, invoke the remote method and marshall your parameters, and then demarshall the return values. Since AXIS makes this process simpler than ever, development is a breeze.

Well, let's start coding...
For this example we are going to use:
  • Java (here I'm using JDK 1.7, Click here if you need help installing Java JDK in Windows)
  • Apache Tomcat v7.xx
  • Eclipse Luna

Install Apache Tomcat and add it to Eclipse... File --> New --> Other. Type in server, and follow the wizard.

Open Eclipse and create a "Dynamic Web Project"
For project Name: JavaApacheAxisWebService




On your Eclipse project, expand Java Resources and under /src create a 
new class: MyJavaWebServiceHelloWorld 
and for Package: rolandofebrero.com.web.service



                                 


Now, double click on MyJavaWebServiceHelloWorld.java and add the following code:



Right click on MyJavaWebServiceHelloWorld.java --> Web Services --> Create Web Service


                                   



On the next screen, make sure you have selected Tomcat and Apache Axis in both service and client. Please see the image bellow:



If after clicking finish you see any error regarding to you server, make sure you can start Tomcat with no errors. If not, delete your server, and add it again. Server should start with no errors before creating WebService.

If everything was OK, your project should look like this:



If the server started correctly, JavaApacheAxisWebService and JavaApacheAxisWebServiceClient are automatically deployed to Tomcat server.

By default Eclipse provides a Web service Test Client Window in which we can consume our service



If we click on getEndpoint() link, it will provide us with the endpoint of our service, which is:

        http://localhost:14823/JavaApacheAxisWebService/services/MyJavaWebServiceHelloWorld

put that endpoint in your browser and add ?WSDL at the end. We will see the WSDL created. If you want to consume this service, you might need the WSDL, or just use SoapUI and create a soap project and start testing this new service....    
     


You can download the code for this project from my GitHub account:

https://github.com/rolando-febrero/JavaApacheAxisWebService




Programming thought of the day:


  • My software never has bugs. It just develops random features.

Monday, December 5, 2016

Installing and Running Node.js applications on IIS – Windows



Go to: https://nodejs.org/en/#download and download Node.js for Windows. For this exercise, we are using v6.9.1 LTS version.
Place it in C:\node 



 Also, we need to install iisnode. Go to: https://github.com/tjanczuk/iisnode/releases

Download and install the version Windows OS requires (32 or 64 bits). For this exercise, we are using iisnode-full-v0.2.21-x64.msi





Install URL rewrite module for IIS. Go to: http://www.iis.net/downloads/microsoft/url-rewrite
URL Rewrite allows Web administrators to easily build powerful rules using rewrite providers written in .NET, regular expression pattern matching, and wildcard mapping to examine information in both URLs and other HTTP headers and IIS server variables. Rules can be written to generate URLs that can be easier for users to remember, simple for search engines to index, and allow URLs to follow a consistent and canonical host name format. URL Rewrite further simplifies the rule creation process with support for content rewriting, rule templates, rewrite maps, rule validation, and import of existing mod_rewrite rules.

For testing purposes, create a Default site in IIS to test our Node.js installation. Follow the next steps:

Go to Internet Information Services Manager (IIS). Right click on Sites and click on Add Web Site…


   


Add Web Site dialog box will prompt for some input. Fill out this dialog with:
·        Site Name: Default Web Site
·        Application pool: choose DefaultAppPool
·        Physical path: C:\Program Files\iisnode\www

Make sure to Test your settings before clicking OK.

  


To set up samples, from the administrative command prompt call:

%programfiles%\iisnode\setupsamples.bat   (it is recommended to run it as Administrator)

Or go to: C:\Program Files\iisnode and run setupsamples.bat from there

The setupsamples.bat will:
·        Unregister existing "iisnode" global module from your installation of IIS if such registration exists
·        Register iisnode as a native module with your installation of IIS
·        Install configuration schema for the "iisnode" module
·        Remove existing "iisnode" section from system.webServer section group in applicationHost.config
·        Add the "iisnode" section within the system.webServer section group in applicationHost.config
·        Delete the iisnode web application if it exists
·        Add a new site iisnode to IIS



At this point your site should look like:



And if you click on Modules for this Site in IIS7 you should see iisnode as a native module:


                      

If everything went well, you should be able to hit:  http://localhost/node




Navigate to: http://localhost/node/helloworld/hello.js and if you are able to see this:





It is because the installation was successful and your Node.js app is running within IIS on Windows!








Programming thought of the day:

  • Michael Sinz: “Programming is like sex, one mistake and you have to support it for the rest of your life.”

Node.js & Express - Hello world example



Node.js is an open-source, cross-platform JavaScript runtime environment for developing a diverse variety of tools and applications. Although Node.js is not a JavaScript framework, many of its basic modules are written in JavaScript, and developers can write new modules in JavaScript. The runtime environment interprets JavaScript using Google's V8 JavaScript engine.

I use Node.js at work, and find it to be very powerful. Forced to choose one word to describe Node.js, I'd say "interesting" (which is not a purely positive adjective). The community is vibrant and growing. JavaScript, despite its oddities can be a great language to code in. And you will daily rethink your own understanding of "best practice" and the patterns of well-structured code. There's an enormous energy of ideas flowing into Node.js right now, and working in it exposes you to all this thinking - great mental weightlifting.


Pros / Cons:
  • Pro: For a server guy, writing JavaScript on the backend has been a "gateway drug" to learning modern UI patterns. I no longer dread writing client code.
  • Pro: Tends to encourage proper error checking (err is returned by virtually all callbacks, nagging the programmer to handle it; also, async.js and other libraries handle the "fail if any of these subtasks fails" paradigm much better than typical synchronous code)
  • Pro: Some interesting and normally hard tasks become trivial - like getting status on tasks in flight, communicating between workers, or sharing cache state
  • Pro: Huge community and tons of great libraries based on a solid package manager (npm)
  • Con: JavaScript has no standard library. You get so used to importing functionality that it feels weird when you use JSON.parse or some other build in method that doesn't require adding an npm module. This means that there are five versions of everything. Even the modules included in the Node.js "core" have five more variants should you be unhappy with the default implementation. This leads to rapid evolution, but also some level of confusion.

Now, let's install Node.js and see how it working. Go to https://nodejs.org/en/ and download the version you need (in this case we are using v6.9.1 LTS)




Create a folder and let's name it: helloNode
Launch the Command Prompt and navigate to helloNode folder. Run: npm init
This will prompt you for some information about your node app and create package.json file....see the image bellow:





So you are in \helloNode folder, in Command Promt type: npm install express --save  
This command will add Express as dependency for our project. If you don't know what Express is, take a look here: Express .

After adding Express dependecies, a new folder (node_modules) is added to \helloNode. Now create a new file in \helloNode and let's name it index.js

In index.js add the following code:

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})

 The app starts a server and listens on port 3000 for connections. The app responds with “Hello World!” for requests to the root URL (/) or route. For every other path, it will respond with a 404 Not Found.

At this point, your \helloNode should look like this:




Now, it's time to see our lil monster works! In Command Prompt type: node index.js
Go to: http://localhost:3000/
If you are able to see the "Hello World" message is because everything went as planned. If not, you might be missing something.   

You can see and download the code for this project from my git account:
https://github.com/rolando-febrero/helloNode





Programming thought of the day:


  • Hey! It compiles! Ship it!