Pages

Sunday, June 10, 2012

Asynchronous communication with JMS(Java Messaging Service) - Part 1

Note: Don't forget to set the quality to HD in the video player




The JMS is a wide topic but that it is very important for a java developer to understand. The use of asynchronous processing in software development is very important and for many systems it is a must.

In this video i wanted to give some brief introduction to JMS and also do some simple to understand practical example that shows how different processes can be performed without the need of having both client and the server waiting for each other until a task is finished.

java, JMS, Queue, Java Messaging System, Asynchronous Processing, Tutorial

Wednesday, May 30, 2012

How many lines of java code does your workspace contain

Did you ever wonder how many lines of java code does your workspace have? Here a little program that can help you find out:

Class 1:

1:  package sample;  
2:  import java.io.File;  
3:  import java.util.Scanner;  
4:  public class LinesOfCodeCounter {  
5:    private long linesOfCode;  
6:    private int scannedProjects;  
7:    public void analizeFolder(File file) {  
8:  //Check if the current file is a file or a folder  
9:      if (file.isDirectory()) {  
10:  //If it is a folder get the inner files  
11:        File[] filesInFolder = file.listFiles();  
12:  //For each of the inner files check if it is a folder or a file(Recursion)  
13:        for (File innerFile : filesInFolder) {  
14:          analizeFolder(innerFile);  
15:        }  
16:      } else if (file.isFile()) {  
17:  //When you find a file that is not a folder, check if it contains java code  
18:        if (file.getName().endsWith(".java")) {  
19:  //Scan the file  
20:          scanFile(file);  
21:        }  
22:      }  
23:    }  
24:    public void scanFile(File file) {  
25:      try {  
26:        Scanner fileScanner = new Scanner(file);  
27:  //Read each line of code  
28:        String tempDataHolder = fileScanner.nextLine();  
29:  //While the file contain lines of code count them  
30:        while (fileScanner.hasNext()) {  
31:          linesOfCode++;  
32:          tempDataHolder = fileScanner.nextLine();  
33:        }  
34:      } catch (Exception ex) {  
35:        ex.printStackTrace();  
36:      }  
37:    }  
38:    public long getLinesOfCode() {  
39:      return linesOfCode;  
40:    }  
41:  }  



Class 2:
1:  package sample;  
2:  import java.io.File;  
3:  public class Main {  
4:    public static void main(String[] args) {  
5:      //The workspace root  
6:      final File workSpaceRoot = new File("D:\\JavaWorkspace");  
7:      //Check if the root contains files inside its self  
8:      LinesOfCodeCounter counter = new LinesOfCodeCounter();  
9:      counter.analizeFolder(workSpaceRoot);  
10:      System.out.print(counter.getLinesOfCode());  
11:    }    
12:  }  

Saturday, May 26, 2012

Clustering with Glassfish 3.1

                  

 

 Here I use the command line to create a cluster in the application server Glassfish 3.1 and add 2 instances to it.
Once the cluster is created I deploy an enterprise application and run it in both instances.

Tuesday, May 1, 2012

File realm & role based security with Glassfish 3.1






Here in this video you can see how i create a simple security realm that uses a file in a safe place in the app server to store the credentials. One thing i forgot was to show you how to logout :)
Don't worry is not very difficult, for that you can implement your own logout servlet to invalidate the session.
Here is some example of code for logging out:


1:  @WebServlet(name = "LogoutServlet", urlPatterns = {"/logout"})  
2:   public class LogoutServlet extends HttpServlet {  
3:   @Override  
4:   protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
5:    //Invalidates the session  
6:    request.getSession(false).invalidate();  
7:    // Redirects back to the initial page.  
8:    response.sendRedirect(request.getContextPath());  
9:   }  
10:   }  

Also here there is an image to show where can you enable HTTPS for the admin panel:


Note: The SSL certificate that comes with glassfish is expired that is the reason why you will see a warning message before you login.

Saturday, April 7, 2012

Using a fun chalk board to explain how a JEE6 application that uses web services should be organized

I have been studying JAX-RS the last 2 months and i found it very interesting. I wanted to create an interesting video where i explain one of the ways how the .ear application can be organized when using web services. I didn't know that Entities can behave also as JAXB objects. That make things a lot easier.
I think you will like this video, is interesting. It was to big to upload it at once so i divided.
This is the second part of the video. Here i am just showing the code, i am not programming because otherwise the recording would be too long.

I would like to know about what do you think of this architecture? What other alternatives do exist? What are the best practices? Please feel free to comment, i am very interested in reading your comments.

Wednesday, April 4, 2012

The use of marker interfaces

When programming, we often use interfaces, to hide the realization. But in some occasions, we want just want to group classes. We call marker interfaces, to empty interfaces(No methods at all). 

A very simple example is the Serializabe interface: 
It has no methods at all, but all classes that whant to be writen into some kind of file, or network stream, need to implement Serializable. 

In the same way, you could make a Program with classes like Dog,Bird,Cow... and you dont need to add them any functionality extra, just to mark them, because somewhere in the code there will be a method that could look like this : String method(Flyer); So you need to create a marker interface called Flyer, to mark the animals that can fly(So you dont need to have unnecesary variables boolean like isFlyer, in classes Dog and Cow). 




maybe i should give a more pragmatical example, so it is easier to understand. Lets try:

See the folowing code:

1:  //Marking interface flyer  
2:  public interface Flyer {  
3:  //If this is a marker interface, there is no methods in it  
4:  }  
5:  public class Mammal {  
6:  private String someVariable;  
7:  }  
8:  public class Cow extends Mammal {  
9:  //Some code specific to cows  
10:  }  
11:  public class Bat extends Mammal implements Flyer {  
12:  //Some code specific to Mammal  
13:  //Note that the Bat is marked as a Flyer  
14:  }  



I think this example more or less explains my point. Bats are mammals and can fly,so why would you have a boolean variable that can be either true or false, when you can just mark the class. Another reason why i think this is a good approach, i because if the future we need to upgrade the program, we can just mark the classes that can fly with the interface.

Imagine somewhere in the program there is a method like this:

1:  public class AirportControl {  
2:  public void testFly(Animal a) {  
3:  if(a instanceof Flyer) {  
4:  //Perform a test fly  
5:  }  
6:  public void testFly(Vehicle v) {  
7:  if(a instanceof Flyer) {  
8:  //Perform a test fly  
9:  }  
10:  }  


I think this approach is pretty flexible,when creating your domain model.

Monday, April 2, 2012

What is JAXP and what tools does it provide?


I had this video in my hard disk where i explain what XML parsers are and what java offer us by default to perform XML parsing.
I thought it may be handy if someone wants to get a brief  theoretical introduction to the world of  XML parsing using Java. There are lots of other tools out there for doing parsing with java, the video just mentions the ones that are included in the SDK.
If you want to know more, check out the resources:


Share with your friends