Pages

Saturday, 16 March 2019

Creating you own TriFunction Interface in Java 8

Create your own TriFunctional Interface.


This Idea generally came when I was working with travel Domain Company, I used to have a lot of webservces to be merged into one which was nothing but output of the product.

So I have to comeup with API and Java 8 was into boom. and somehow I decided to extend a BiFunction and make my own TriFunction Interface.

@FunctionalInterface
public interface  TriFunction<W1,W2,WR> {
     /*
      * An Api method which will be used fro merginng two webservices and create new one
     */
    WR join(W1 w1, W2 w2);
}

Above example is just a short starter one which can be used in various APIS.

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.

Friday, 4 October 2013

Example of mail merge generating multiple files.

Example of mail merge (Dependency Example) generating different files using RTFTemplate library.


  • Add required JARS in class path.
  •  Required JARS:  
       
     rtftemplate-1.0.1-b14.jar  
     commons-collections-3.2.1.jar  
     spring-beans-3.2.2.RELEASE.jar  
     spring-context-3.2.2.RELEASE.jar  
     spring-context-support-3.2.2.RELEASE.jar  
     spring-core-3.2.2.RELEASE.jar  
     spring-expression-3.2.2.RELEASE.jar  
     spring-jdbc-3.2.2.RELEASE.jar  
     spring-orm-3.2.2.RELEASE.jar  
     spring-tx-3.2.2.RELEASE.jar  
     xml-apis-1.0.b2.jar  
     commons-logging-1.1.1.jar  
     freemarker.jar  
     commons-digester-2.1.jar  
     org.apache.commons.beanutils.jar  
     velocity-1.7.jar  
     commons-lang.jar  
    


References:-
http://sourceforge.net/projects/rtftemplate/

http://rtftemplate.sourceforge.net/