Pages

Wednesday 18 June 2014

Some features of Java 8.

Some Java 8 features.

Although the java has launched its new version Java 8 with various new features, some of them are as. Because of this release there will be a drastic change in the way of coding using Lambda expressions, Streams, default interfaces, etc.

  • Streams.
  • Functional Interfaces.
  • Lambda.
  • Java Time API.
  • Accumulators.

Streams


Streams are not the streams you are thinking about (InputStream, OutputStream) this stream is all about collections, well this stream is not for replacing the List, ArrayList or any collection. There are generally two types of stream in the collections.
  1. Sequential Stream.
  2. Parallel Stream.
Sequential Stream: - When the stream is traversed sequentially it traverses one by one.

Example:-

List<Address> addresses = list.getStream.collect(Collectors.toList());



Parallel Stream: - When the stream is traversed parallely it breaks into many parts and then gets traversed and each of which is processed individually on a different thread. The results are consolidated output is formed.

Example:-

List<Address> addresses = list.getStream.parallel().collect(Collectors.toList());

Functional Interfaces

The interface which has default method, which can give a default behaviour, which can give a default functionality, this type of interfaces are the functional interface.

Functional interface cannot have more than one abstract method but it can have more than one default methods. Default methods are introduced in Java 8, to add new methods to interface without disturbing the implemented classes.

Example:-

interface FunctionalInterface{
  void show();
  default void display(){
     System.out.println("Default method in functional interface can have body..!");
  }

}

Lambda

Lambda expression is nothing but a method without any declaration, access modifiers, return value and name. It’s a kind of expression which is unspecified function. By the use of this we can directly write the logic without extracting as a method that saves our effort.

Lambda expression looks like:-

(Argument) -> {Body}
(Type argument1, Type argument2...) -> {Body}

Examples:-

(Integer x, Integer y) -> {return x * Y;}

(String s) -> {System.out.println(s);}


Java Time API


Well Java date and Time API is very old and it is continuously getting developed. Finally replacing the confusing, slow, and difficult Date and Calendar APIs. Although all the java Time classes are immutable and thread safe. They are based on the ISO 8601 calendar system, the de facto world calendar following the proleptic Gregorian rules

This time with use of Java Time API packages it will be quite easy. Though it contains some sub-packages java.time.format that provides classes to print and parse dates and times and java.time.zone provides support for time-zones and their rules.
The new Time API prefers enums over integer constants for months and days of the week. One of the useful class is DateTimeFormatter for converting datetime objects to strings

Accumulators

An accumulator is a variable that the program uses to calculate a sum or product of a series of values. Accumulator variable is used to maintain a single count, sum... which is updated by many threads. Therefore some new classes were generated for it.

·         DoubleAccumulator
·         DoubleAdder
·         LongAccumulator
·         LongAdder

Generally these classes are used when the variable is to accessed by many threads.
Both the DoubleAdder and LongAdder classes can be seen as specific subsets of the DoubleAccumulator and LongAccumulator functionality.

The call new DoubleAdder() is equivalent to
new DoubleAccumulator((x, y) -> x + y, 0.0).
The call new LongAdder() is equivalent to
new LongAccumulator((x, y) -> x + y, 0L).

Example:-

DoubleAccumulator da = new DoubleAccumulator((x,y) -> x + y, 0.0);
List<Double> doubles = Arrays.asList(1.0, 2.0, 3.0, 4.0, 10.0);
doubles.forEach(da::accumulate);

System.out.println("Result: " + da.doubleValue());

Result:-
Result: 24

LongAdder la = new LongAdder();
List<long> longs = Arrays.asList(10, 20, 30, 40, 100);
longs.forEach(la::accumulate);

System.out.println("Result: " + la.longValue());

Result:-

Result: 200

Thursday 10 April 2014

Logging in a clustered environment using Log4j

Logging using Socket Appender.

The logs should be stored in a centralized machine or sever when the application is stored in a clustered environment. Let’s get a scenario where every information level of logs goes in a clustered machine and error level log go in a server or centralized machine. So every cluster will have an information level log and if an error occurs it will show in a centralized machine.


There are four classes (Class1, Class2, Class3, Class4) these classes behave as a clustered application and ClassMain which contain the main method behave as a manager of this cluster. This cluster means class 1 2 3 4 will store the info level logs through RolingFileAppender, show the message in the console using ConsoleAppender and send the error level logs to a centralized machine using SocketAppender.

log4j.xml is the file which is defined in the application; every cluster will have the same property file and log4j-server.xml will be deployed in a centralized machine.

The command for starting socket server:-

java -classpath {Jar Path}\jars\log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer
4712 {property file path}\log4j-server.properties 

References

Saturday 1 March 2014

Steps for installation of Windows 2008 R2 Server

Installation of Windows 2008 R2 Server

  • Insert the appropriate Windows Server 2008 installation media into your DVD drive and reboot the computer and complete installation process.

  • When prompted for an installation language and other regional options, make your selection and press Next.

  • Next. press Install Now to begin the installation process.

  • Enter your Product ID in the next window, click Next.

  • Select the Full version of the right Windows version you're prompted, and click Next.

  • Read and accept the license terms by clicking to select the checkbox and pressing Next.

  • In the "Which type of installation do you want? " window, click the only available option –Custom (Advanced).

  • In the "Where do you want to install Windows?", if you're installing the server on a regular IDE hard disk, click to select the first disk, usually Disk 0, and click Next.

  • The installation process will reboot your computer, so make sure you remove it before going to lunch.
  • Once the install is finished, we’re prompted to change our password before logging in.

  • Windows requires that you have a strong password, seven characters long with atleast three of the four following: uppercase letter, lowercase letter, numeral, or symbol. You’ll want to make sure you write it down somewhere for now, because if you forget it later, the entire install will have to be re-done.

  • Finished! That’s all there is to doing a base install of Windows Server 2008 R2.