Sunday, October 16, 2016

RMI(Remote Method Invocation)

The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.
The RMI provides remote communication between the applications using two objects stub and skeleton.

/*Program of RMI application to display a hello world string*/

1) Creating Interface:-

//HelloInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface HelloInterface extends Remote
{
    String greeting() throws RemoteException;
}

2) Implementation of Interface:-

//HelloImpl.java

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class HelloImpl extends UnicastRemoteObject implements HelloInterface

{
    public HelloImpl() throws RemoteException
{
    }

    public String greeting() throws RemoteException
 {
        return "greeting";
    }

}
  
3) Create Server:-

//HelloServer.java

import java.rmi.registry.LocateRegistry;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;

public class HelloServer
{

public static void main(String[]args)throws RemoteException,                                       MalformedURLException
  {
    HelloInterface hello = new HelloImpl();
    Naming.rebind("rmi://127.0.0.1:1099/HelloService", hello);
    System.out.println("server.RMI Server is ready.");
  }
}

4) Create Client:-

//HelloClient.java

import java.rmi.RemoteException;
import java.rmi.NotBoundException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.net.MalformedURLException;
import java.rmi.Naming;
public class HelloClient

{
public static void main(String[] args) throws RemoteException,                       MalformedURLException, NotBoundException
  {
   
HelloInterface hello =   HelloInterface)Naming.lookup("//127.0.0.1:1099/HelloService");
System.out.println(“Hello world…!”);
 
  }
}

Output:

Compile all the source files and start the rmiregistry:-


Rmiregistry start:-


Execute HelloClient.java on another cmd_window:-

Example No.2

/*Program of RMI application to display addition of two number*/

1) Creating Interface:-

// CalculatorInterface.java

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface CalculatorInterface extends Remote

{
  public long add(long a, long b)throws RemoteException;
}

2) Implementing Interface:-

// CalculatorImpl.java


import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class CalculatorImpl extends UnicastRemoteObject                                                       implements CalculatorInterface
{
  protected CalculatorImpl() throws RemoteException
{
  super();
}
 public long add(long a, long b)throws RemoteException
{
  return a+b;
}
}

3) Create Server:-

// CalculatorServer.java

import java.rmi.Naming;

public class CalculatorServer
{
  CalculatorServer()
{
try
{
CalculatorInterface cal=new CalculatorImpl();
Naming.rebind("rmi://127.0.0.1:1099/CalculatorService",cal);
}
catch(Exception e)
{
  //System.out.println(e);
e.printStackTrace();
}
}
public static void main(String args[])
{
  new CalculatorServer();
}
}

4) Create Client:-

// CalculatorClient.java

import java.rmi.Naming;

public class CalculatorClient
{
  public static void main(String args[])
{
  try
{
CalculatorInterface cal=(CalculatorInterface)Naming.lookup("//127.0.0.1:1099/CalculatorService");
System.out.println("addition:"+cal.add(10,20));
}
catch(Exception e)
{
  System.out.println(e);
}
}
}
  
Output:-
Compile all the source files and start the rmiregistry:-


Rmiregistry start:


Execute CalculatorClient.java on another cmd_window:-


/*Program of RMI application to display a Factorial of given number*/

Thursday, September 15, 2016

Features of the ProcessAPI Java9

The primary goal of the developers is, to make it easier for you to manage processes on your operating system. They start with giving you the ability to enumerate the system processes via the updated API (no hacky solutions anymore). That means, you can get different properties for each process, e.g. the PID, the process name, the user who started it, the path of the process, resource usage, etc. It is also planned, to make it easier to deal with process trees, especially destructing entire process trees and managing processes with over 100 sub-processes. To do so, it is planned to multiplex their output streams into one output stream, so you do no longer have one thread per monitored process listening for its output stream
How would it look like
We have two examples for you. In the first example, one tries to get the process-id of the current process. In the second example, one tries to get a list of all processes.
So let’s see, how retrieving the process-ID of the current process looks like, before the ProcessAPI-Update.
private static int getProcessOwnID() {
    // pName is equal to PID@COMPUTER_NAME
    String pName = ManagementFactory.getRuntimeMXBean().getName();
    return Integer.parseInt(pName.split("@")[0]);
}

Monday, September 12, 2016

Eclipse shortcuts for java developers.

1) Ctrl + Shift + T for finding class even from jar

This keyboard shortcut in Eclipse is my most used and favorite shortcut. While working with a high-speed trading system which has a complex code, I often need to find classes with the just blink of the eye and this eclipse keyboard shortcut is just made for that. No matter whether you have class in your application or inside any JAR, this shortcut will find it.

2)      Ctrl + Shift + R for finding any resource (file) including config xml files

This is similar to above Eclipse shortcut with only difference that it can find out not only Java files but any files including XML, configs, and many others, but this eclipse shortcut only finds files from your workspace and doesn’t dig at jar level.

3)      Ctrl + 1 for quick fix

This is another beautiful Eclipse shortcut which can fix up any error for you in Eclipse. Whether it’s missing declaration, missing semi-colon or any import related error this eclipse shortcut will help you to quickly sort that out.

4)      Ctrl + Shift + o for organize imports

Another Eclipse keyboard shortcut for fixing missing imports. Particularly helpful if you copy some code from other file and what to import all dependencies.

Eclipse Shortcut for Quick Navigation
In this section, we will see some eclipse keyboard shortcut which helps to quickly navigate within the file and between file while reading and writing code in Eclipse

7) Ctrl + o for quick outline going quickly to method
9) Alt + right and Alt + left for going back and forth while editing.
12) Alt + Shift + W for show in package explorer
13) Ctrl + Shift + Up and down for navigating from member to member (variables and methods)
15) Ctrl + k and Ctrl + Shift +K for find next/previous
24) Go to a type declaration: F3, This Eclipse shortcut is very useful to see function definition very quickly

Eclipse Shortcut for Editing Code
These Eclipse shortcuts are very helpful for editing code in Eclipse.

5) Ctrl + / for commenting, uncommenting lines and blocks, see here for live example.
6) Ctrl + Shift + / for commenting, uncommenting lines with block comment, see here for example.
8) Selecting class and pressing F4 to see its Type hierarchy
10) Ctrl + F4 or Ctrl + w for closing current file
11) Ctrl+Shirt+W for closing all files.
14) Ctrl + l go to line
16) Select text and press Ctrl + Shift + F for formatting.
17) Ctrl + F for find, find/replace
18) Ctrl + D to delete a line
19) Ctrl + Q for going to last edited place

Miscellaneous Eclipse Shortcuts
These are different Eclipse keyboard shortcuts which doesn’t fit on any category but quite helpful and make life very easy while working in Eclipse.

20) Ctrl + T for toggling between supertype and subtype
21) Go to other open editors: Ctrl + E.
22) Move to one problem (i.e.: error, warning) to the next (or previous) in a file: Ctrl +. For next, and Ctrl +, for the previous problem
23) Hop back and forth through the files you have visited: Alt + ← and Alt + →, respectively.
25) CTRL+Shift+G, which searches the workspace for references to the selected method or variable
26) Ctrl+Shift+L to view listing for all Eclipse keyboard shortcuts.
27) Alt + Shift + j to add Javadoc at any place in java source file.
28) CTRL+SHIFT+P to find closing brace. Place the cursor at the opening brace and use this.
29) Alt+Shift+X, Q to run Ant build file using keyboard shortcuts in Eclipse.
30) Ctrl + Shift +F for Autoformatting

Saturday, September 10, 2016

Top 18 Java Tools

Java Tools

You’ll probably know many of the tools on this list of 18 choices, but others may be new to you. And odds are you haven’t tried them all yet

Gradle (http://www.gradle.org): Build tool. Automates the building, testing, publishing, deployment, and more of software as well as generating static websites or documentation.

Eclipse (http://www.eclipse.org): Opensource integrated development environment (IDE). If you could have just one tool for Java development, Eclipse would be a good choice.

IntelliJ (http://www.jetbrains.com/idea/): IDE made by JetBrains, available in an
Apache 2licensed community edition and a commercial edition. Intel iJ provides similar features to Eclipse, with a smooth, developer friendly experience.

YourKit (http://www.yourkit.com): Java profiler. Combines powerful analysis
capabilities, ondemand profiling during both development and production, free embedding into production, and seamless IDE and application server integration.

Friday, September 2, 2016

Eclipse Versions

The various versions are:

  • Eclipse 1.0 (November 7, 2001): based on an earlier Java IDE called VisualAge from IBM.
  • Eclipse 2.0 (June 28, 2002)
  • Eclipse 2.1 (March 28, 2003)
  • Eclipse 3.0 (June 25, 2004)

Thursday, September 1, 2016

How to do static import in Eclipse - Java

Do you know what is shortcut of doing static import in Eclipse? Well I didn't know before, but today I come to know that shortcut Ctrl+Shift+M (Source > Add Import) can not only be used to add missing imports but It can also help with static import in Java program.  Suppose you are using lots of static variable from a utility class e.g. TimeUnit by referring them with class name, just like we refer static variable. In Eclipse IDE, you can select the whole reference variable and press Ctrl+Shift+M and it will automatically import that static element using static import in Java.

Tuesday, August 30, 2016

JBoss Data Virtualization


JBoss Data Virtualization is a data integration solution that sits in front of multiple data sources and allows them to be treated as a single source, delivering the right data, in the required form, at the right time to any application and/or user.
AGILE DEVELOPMENT
Rapid ways to deliver reusable and loosely coupled unified data views and data services using graphical tools. Hide data integration complexity from data consuming application developers and business users. Decouple consuming applications and data sources. Allow reuse and extension of logical data models, views and services.

Monday, August 29, 2016

How to Make Executable JAR file in Eclipse IDE - Java


If you are Java programmer then you know what is the purpose of JAR file, but for those who are unaware, JAR file is deliverables of Java application. Just like C and C++  application produce EXE files, Java produces JAR files. 
In other words, A JAR (Java Archive) file is a ZIP format file that bundles Java classes into a single unit, it may contain all the resource needed by Java application as well. There are mainly two types of JAR file in Java:  
Library JAR (normal JAR) files: JARs which are reusable libraries e.g. Apache commons JAR file, guava.jar itself or even JDBC drivers e.g. ojdbc6_g.jar. 
Executable JAR files: JARs which can be executed as a standalone Java applications. The main

Using Dependency Injection with Java EE

Using Dependency Injection with Java EE
  by samdhukate11@gmail.com
When Java EE 6 introduced the Contexts and Dependency Injection (CDI) specification, I was amazed that Java EE finally included dependency injection (DI) horizontally. In other words, any component can be injected into any other component, regardless of whether it's an EJB, a web service, or a POJO. Thus, you can inject a component from a Java EE container using the @Inject annotation, since the file named beans.xml is present in the WEB-INF/META-INF directory to indicate to the container which classes should be scanned.

Saturday, August 27, 2016

JUnit cheat sheet


This post continues our series of the most useful and beautiful cheat sheets from which software engineers can learn the most frequently used commands, idioms, and best practices on various topics.
Today, we’re talking about unit tests and JUnit, one of the most popular unit test libraries in the JVM ecosystem, including some of the really useful updates in JUnit 5, which brings the library up to speed with the new features in Java 8. Now, it’s very tempting to just grab the image, print it out and start looking for a place on the wall to pin it up. By all means, do it! But make sure you stay with us, and continue reading this post, as we’ll explain the content of the cheat sheet in much more depth. The cheat sheet will then serve as a visual hint for your future testing.
JUnit cheat sheet
GET THE JUNIT CHEAT SHEET!

Building modern JavaScript applications on top of #JavaEE backends

Build modern JavaScript applications on top of Java EE backends. The Oracle JavaScript Extension Toolkit (JET) enables you to create complex enterprise JavaScript applications. But how do you connect those applications to a backend and can Java play a role?

Threads synchronization.

Different options #Java provides to deal with threads synchronization.

HTTP/2 Client – Java 9

HTTP/2 Client – Java 9
The web has changed a lot since the 90’s, so in May 2015, a new version of the HTTP-Protocol was presented by the Internet Engineering Task Force (IETF). With Java 9, the developers also updated the HTTP-API in the JDK and came up with an entirely new API for HTTP/2 and Websocket. This new API will replace the old HTTPURLConnection API, which is as old as HTTP/1.1 itself.
Problems with HTTP/1.1
With HTTP/1.1, you had many Request-Response cycles which increased the latency and loading times of webpages. Additionally, you had the problem of head-line-blocking, which means that the line of packets is held up by the first packet. Because of that, an often used approach on optimizing was to open several TCP-Connections to the server and request the elements equally distributed over the connections and use workarounds like image sprites. But these multiple TCP-Connections put some additional load on the web servers and are pretty inefficient, because TCP-Connections are expensive. The goal of HTTP/2 is, to lower latency, loading times and server load, but still being backwards compatible to HTTP/1.1.
What is HTTP/2 about?